.FindNext不会在.Find(覆盖/覆盖)中搜索相同的条件

时间:2018-03-18 18:37:18

标签: excel excel-vba vba

我已尝试在多个网站上搜索以获得解决方案但无法找到。

我在一张表中有多个表,并使用findnext循环在每个表中搜索“Ven1”地址。 但同时想要重新排列特定表中的行和列,因此使用.find函数。

现在在1个表的操作结束时,.findnext在另一个表中找不到“Ven1”的地址但是考虑了.find的最后结果(用于重新排列)

我认为.find用于操作是覆盖/覆盖.find搜索“Ven1”位置。

请告诉我是否有办法在一个子目录中使用两者,或者创建功能会有帮助

以下是代码

WS.Select

    With Worksheets(WS.Name)
    xyz = "Ven1"
        '(Not helping) - Set dr = Cells.Find(What:=xyz, After:=ActiveCell, LookIn:=xlValues, Lookat:=xlWhole, searchdirection:=xlNext, MatchCase:=False, SearchOrder:=xlByRows, searchformat:=False)

        Set dr = Cells.Find(What:=xyz, LookIn:=xlValues, Lookat:=xlWhole)
        If Not dr Is Nothing Then
            FirstAddress = dr.Address

            Do
                r = dr.Row
                c = dr.Column

                checkrow = InStr(1, Cells(r + 1, c).Value, "Ven")
                checkcol = InStr(1, Cells(r, c + 1).Value, "Ven") Or Cells(r, c + 1).Value = ""
                If checkrow = 1 Then
                counter = r
                End If

                If checkcol = 1 Then
                counter = c
                End If

                For new_index = LBound(new_row_order) To UBound(new_row_order)

                    If checkrow = 1 Then

                        Set found = Range(Cells(r, c), Cells(lrow1 + r - 1, c)).Find(new_row_order(new_index), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)

                        If Not found Is Nothing Then

                            If found.Row <> counter Then
                                x = found.Row
                                y = found.Column
                                CN = Cells(x, y).End(xlToRight).Column
                                Range(Cells(x, y), Cells(x, CN)).Cut
                                Cells(counter, y).Insert Shift:=xlDown
                                Application.CutCopyMode = False
                            End If
                            counter = counter + 1
                        End If

                    Else
                        If checkcol = 1 Then

                            Set found = Range(Cells(r, c), Cells(r, lrow1 + c - 1)).Find(new_row_order(new_index), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
                            If Not found Is Nothing Then

                                If found.Column <> counter Then
                                    x = found.Row
                                    y = found.Column
                                    RN = Cells(x, y).End(xlDown).Row
                                    Range(Cells(x, y), Cells(RN, y)).Cut
                                    Cells(x, counter).Insert Shift:=xlToRight
                                    Application.CutCopyMode = False
                                End If
                                counter = counter + 1
                            End If
                        End If
                    End If

                Next new_index

            Set dr = Cells.FindNext(dr)

   'The above Set dr = cells.findnext(dr) is not setting "Ven1" rather its setting the dr as last value in the list of rearrangement operation

            Loop While Not dr Is Nothing And dr.Address <> FirstAddress

        End If
    End With
End If
Next WS

0 个答案:

没有答案