对于If循环多次超过相同范围

时间:2018-01-08 18:20:36

标签: excel vba excel-vba

上下文:我有一系列单元格,其中一些单元格具有相同的内容。我试图遍历单元格并根据值找到匹配的次数(最多7次)采取特定操作。如果它有帮助,我想根据找到的匹配将某些信息范围移动到不同的行。

我做了什么:我让它在范围内循环,但问题是它会多次达到相同的范围。

我想要的:如果可能的话,我希望在匹配值结束后继续For循环。

For i = 1 To lastRow
        If Cells(i, 4).Value = Cells(i + 1, 4).Value Then                   'Checks first and second levels
            Debug.Print "First and second levels ["; Cells(i, 4).Address & "," & Cells(i + 1, 4).Address & "]"

            If Cells(i, 4).Value = Cells(i + 2, 4).Value Then           'Checks second and third levels
                Debug.Print "Second and third levels ["; Cells(i, 4).Address & "," & Cells(i + 2, 4).Address & "]"

                If Cells(i, 4).Value = Cells(i + 3, 4).Value Then       'Checks third and fourth levels
                    Debug.Print "Third and fourth levels ["; Cells(i, 4).Address & "," & Cells(i + 3, 4).Address & "]"

                Else
                    'If Cells(i, 14).Value = "No" Then                        'Only first & second level procedures

                    Debug.Print "Only first and second levels ["; Cells(i, 4).Address & "," & Cells(i + 2, 4).Address & "]"
                End If

            Else
                If Cells(i, 14).Value = "No" Then                           'Only first & second level procedures


                End If
            End If
        Else
            Debug.Print "No match [" & Cells(i, 4).Address & "," & Cells(i + 1, 4).Address & "]"
        End If
     Next i
'End With
End Sub

这是当前Debug.Print输出的样子(D8:D11全部匹配):

第一和第二级[$ D $ 8,$ D $ 9]

第二和第三级[$ D $ 8,$ D $ 10]

第三和第四级[$ D $ 8,$ D $ 11]

第一和第二级[$ D $ 9,$ D $ 10]

第二和第三级[$ D $ 9,$ D $ 11]

只有第一和第二级[$ D $ 9,$ D $ 11]

2 个答案:

答案 0 :(得分:0)

好的,修复很简单:根据匹配的值来更改 i 整数值。代码

For i = 2 To lastRow
        If Cells(i, 4).Value = Cells(i + 1, 4).Value Then                   'Checks first and second levels
            Debug.Print "First and second levels ["; Cells(i, 4).Address & "," & Cells(i + 1, 4).Address & "]"

            If Cells(i, 4).Value = Cells(i + 2, 4).Value Then           'Checks second and third levels
                Debug.Print "Second and third levels ["; Cells(i, 4).Address & "," & Cells(i + 2, 4).Address & "]"

                If Cells(i, 4).Value = Cells(i + 3, 4).Value Then       'Checks third and fourth levels
                    Debug.Print "Third and fourth levels ["; Cells(i, 4).Address & "," & Cells(i + 3, 4).Address & "]"

                Else
                     i = i + 2             '<~~~CHANGE
                    If Cells(i, 14).Value = "No" Then                        'Only first & second level procedures

                        Debug.Print "Only first and second levels ["; Cells(i, 4).Address & "," & Cells(i + 2, 4).Address & "]"
                    End If
                End If

            Else
                i = i + 1                     '<~~~CHANGE
                If Cells(i, 14).Value = "No" Then                           'Only first & second level procedures

                End If
            End If
        Else
            Debug.Print "No match [" & Cells(i, 4).Address & "," & Cells(i + 1, 4).Address & "]"
        End If
     Next i
End Sub

答案 1 :(得分:0)

  1. 打开新的工作表2以保存数据。
  2. 浏览sheet1中的第4列。
  3. 如果您发现与sheet2中的任何列匹配,则在该列下添加地址。否则,在sheet2中创建一个具有此值的新列,并在其下添加地址。