运行 VBA caps/replace 多行粘贴数据的脚本

Running VBA caps/replace script on multiple lines of pasted data

我正在 运行 编写一个 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

试试这个

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