删除导致错误/宏Excel Microsoft Visual Basic崩溃

pgvzfuti  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(178)
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not (Application.Intersect(Target, Range("C1:C3,G1:G3,L1:L3,P1:P3")) _
      Is Nothing) Then
        With Target
            If Not .HasFormula Then
                Application.EnableEvents = False
                .Value = UCase(.Value)
                Application.EnableEvents = True
            End If
        End With
    End If
End Sub

几乎所有的程序都是自动为我想要的区域大写的。但是,如果我按删除键,说如果我输入错误,宏就会出错,但是退格操作非常好,没有任何问题

htrmnn0y

htrmnn0y1#

这是一种更安全的方式来处理您想要做的事情-它可以确保如果您尝试更新多个单元格,它不会崩溃:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range, rng As Range
    
    Set rng = Application.Intersect(Target, _
                  Me.Range("C1:C3,G1:G3,L1:L3,P1:P3"))
    
    If Not rng Is Nothing Then 'any cells of interest updated?
        Application.EnableEvents = False
        For Each c In rng.Cells 'check each cell
            If Not c.HasFormula Then
                c.Value = UCase(c.Value)
            End If
        Next c
        Application.EnableEvents = True
    End If
End Sub

顺便说一句,我没有看到任何错误与您张贴的代码时,选择一个单元格,并按下删除...

相关问题