Excel VBA自动大写粘贴时导致错误

时间:2018-12-17 15:31:55

标签: excel vba

当我将信息粘贴到工作表上时,下面的代码导致错误。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A1 As Range
    Set A1 = Range("A:I")
    If Not Intersect(Target, A1) Is Nothing Then
        Application.EnableEvents = False
            Target.Value = UCase(Target.Value)
        Application.EnableEvents = True
    End If
End Sub

该代码特定于工作表(右键单击选项卡上的查看代码)。

请问如何解决此问题?

谢谢。

2 个答案:

答案 0 :(得分:3)

如果存在交叉点,请对其进行循环:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A1 As Range, intr As Range, r As Range
    Set A1 = Range("A:I")
    Set intr = Intersect(A1, Target)
    If Not intr Is Nothing Then
        Application.EnableEvents = False
            For Each r In intr
                r.Value = UCase(r.Value)
            Next r
        Application.EnableEvents = True
    End If
End Sub

答案 1 :(得分:0)

错误是因为#shave off the first point because we need to add it back in to close poly firstVal = elem.text.split() firstVal = firstVal[0].split(',') for i in elem.text.split(): smallList = [] for j in i.split(','): smallList.append(float(j)) finalVals.append(smallList) smallList=[] smallList.append(float(firstVal[0])) smallList.append(float(firstVal[1])) finalVals.append(smallList) print finalVals 不适合多个单元格。因此需要一个循环:

Target.Value = UCase(Target.Value)

如果出现某种意外错误,则使用错误处理程序来重置Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo WorksheetChange_Error Application.EnableEvents = False Dim A1 As Range Set A1 = Range("A:I") If Not Intersect(Target, A1) Is Nothing Then Dim myCell As Range For Each myCell In Target.Cells myCell = UCase(myCell) Next End If Application.EnableEvents = True Exit Sub WorksheetChange_Error: Application.EnableEvents = True MsgBox Err.Description End Sub