Excel VBA Worksheet_Change 值范围

Excel VBA Worksheet_Change for a Range of values

我有一个关于 VBA 的问题,我需要使用工作表更改事件来拾取从 AI28 到 AI30 的单元格值,并将它们移到 V28 到 V30。这是我目前所做的

Private Sub Worksheet_Change(ByVal Target As Range)

If IsNumeric(Target) And Not (Target = "") Then
  If Target.Address = Range("AI28:AI30").Address Then
   Range("V28:V30").Value = Range("AH28:AH30").Value
   
   Else
      If Target.Cells.Value <> Empty Then Exit Sub
   Exit Sub
   
   End If

End If
    
End Sub

它只适用于一个范围,例如 AI28 和 V28,所以我错过了什么?循环什么的?

使用循环和Intersect:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Set rng = Intersect(Target, Me.Range("AI28:AI30"))

    If rng Is Nothing Then Exit Sub

    On Error GoTo SafeExit
    Application.EnableEvents = False

    Dim cell As Range
    For Each cell In rng
        If IsNumeric(cell.Value) And Not IsEmpty(cell.Value) Then
           Me.Range("V" & cell.Row).Value = cell.Value
        End If
    Next

SafeExit:
    Application.EnableEvents = True

End Sub