Vba 如何循环执行If/Then语句

Vba 如何循环执行If/Then语句,vba,excel,Vba,Excel,我在列I中有一个父下拉框,在列J中有一个子下拉框,该下拉框根据列I进行更改 如果列I更改,我希望列j重置,而不是保留其原始值 我的第一行有这个代码 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address(0, 0) = "I2" Then Range("J2").ClearContents End Sub 我怎样才能让它在每一行中循环 非常感谢,, James您的意思是,每当I中的某个值被更改时,您希望

我在列I中有一个父下拉框,在列J中有一个子下拉框,该下拉框根据列I进行更改

如果列I更改,我希望列j重置,而不是保留其原始值

我的第一行有这个代码

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "I2" Then Range("J2").ClearContents
End Sub
我怎样才能让它在每一行中循环

非常感谢,,
James

您的意思是,每当I中的某个值被更改时,您希望J中相应的值被清除吗?最好将I/J限制在一个特定的范围内-这将完成整个列

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.column = 9 Then target.offset(,1).ClearContents
End Sub

你的意思是,每当I中的值被更改时,你希望J中的相应值被清除吗?最好将I/J限制在一个特定的范围内-这将完成整个列

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.column = 9 Then target.offset(,1).ClearContents
End Sub

我要检查
目标
范围是否与列I相交,然后循环遍历更改范围中的所有单元格。可以使用循环中的
.Row
属性清除相应的单元格:

Private Sub Worksheet_Change(ByVal Target As Range)
    'Did something change in column I?
    If Intersect(Target, Me.Range("I:I")) Is Nothing Then
        Exit Sub
    End If

    Dim test As Range
    'Loop through all of the changed cells.
    For Each test In Target
        'If the cell is in column I... 
        If test.Column = 9 Then
            '...clear column J in that row.
            Me.Cells(test.Row, 10).ClearContents
        End If
    Next
End Sub

我要检查
目标
范围是否与列I相交,然后循环遍历更改范围中的所有单元格。可以使用循环中的
.Row
属性清除相应的单元格:

Private Sub Worksheet_Change(ByVal Target As Range)
    'Did something change in column I?
    If Intersect(Target, Me.Range("I:I")) Is Nothing Then
        Exit Sub
    End If

    Dim test As Range
    'Loop through all of the changed cells.
    For Each test In Target
        'If the cell is in column I... 
        If test.Column = 9 Then
            '...clear column J in that row.
            Me.Cells(test.Row, 10).ClearContents
        End If
    Next
End Sub

我要补充的是,共产国际的解决方案与我的解决方案相比具有优势,因为它将检查多个单元格的更改。我要补充的是,共产国际的解决方案与我的解决方案相比具有优势,因为它将检查多个单元格的更改。