【问题标题】:Running VBA caps/replace script on multiple lines of pasted data在多行粘贴数据上运行 VBA caps/replace 脚本
【发布时间】:2016-10-24 15:02:58
【问题描述】:

我正在运行一个 VBA 脚本来自动大写和删除粘贴数据到 Excel 中的连字符。此脚本适用于单行粘贴(单单元格),但如果粘贴多行数据,则不会运行(不会更改数据)。以下是我的代码:

Private Sub worksheet_change(ByVal target As Range)
    Application.EnableEvents = False
        With target
        On Error Resume Next
        Dim rng As Range
        Set rng = Range("A:U")

        If Not Intersect(target, rng) Is Nothing Then
            If Not .HasFormula Then
                .Value = UCase(.Value)
                .Value = Replace(.Value, "-", "")
            End If
        End If
    End With
    Application.EnableEvents = True
End Sub

【问题讨论】:

    标签: vba excel multiple-columns paste


    【解决方案1】:

    试试这个

    Private Sub worksheet_change(ByVal target As Range)
        Application.EnableEvents = False
        With target
            On Error Resume Next
            Dim rng As Range
            Dim cell As Range
            Set rng = Range("A:U")
    
            If Not Intersect(target, rng) Is Nothing Then
                For Each cell in target
                    If Not cell.HasFormula Then
                        cell.Value = UCase(cell.Value)
                        cell.Value = Replace(cell.Value, "-", "")
                    End If
                next cell
            End If
        End With
        Application.EnableEvents = True
    End Sub
    

    【讨论】:

    • @ElleryHiggins 我刚刚进行了新的编辑,请看一下;忘记更改运算符的赋值端
    • 完美运行!!非常感谢!这样可以节省很多时间!
    • 很高兴我能帮上忙。如果它对您有用,请将其标记为正确以关闭您的问题作为已回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多