vba中的FindNext没有继续前进

时间:2015-10-30 16:34:08

标签: excel vba excel-vba

我有一个vba脚本,它遍历一组数据,就像下面的那样。它会选择用户名,在这种情况下"管理员"然后转到另一个电子表格并搜索该用户名。一旦找到它,它就会返回到另一个电子表格并向下移动一个单元格,并在另一个电子表格中搜索该单元格中的文本。在与列(包含用户名)和行(包含另一组数据)相交的单元格中,它标记为" X"。然后循环遍历这组数据,为该用户名下的每个数据执行此操作。然后它移动到下一组数据,这些数据采用相同的格式,并重新进行相同的处理。或者至少它应该是。关于这个脚本的一切都有效,除非它到达第一组数据的底部,这是一个空单元格,它没有设置正确的范围以找到下一个匹配。出于某种原因,conf/neo4j-server.properties调用找到" Users,Builtin"这是第一组中的最后一段数据。我真的很困惑为什么会这样。我觉得这可能与我使用FindNext的方式有关,但我真的不确定。任何有关此问题的帮助将不胜感激!

脚本:

ActiveCell

数据:

Sub AssignGroups()

    Dim membership As Worksheet
    Dim wb As Workbook
    Dim groups As Worksheet
    Dim nameRow As Long
    Dim fullNameString As String
    Dim nameRange As Range
    Dim groupRange As Range
    Dim nameRange2 As Range
    Dim nameIndex As Long
    Dim userNameString As String
    Dim barIndex As Long



    Set wb = ActiveWorkbook
    Set membership = Sheets("User Group Membership")
    Set groups = Sheets("User Assigned to Groups")
    Set nameRange = membership.Range("A:A").Find("user -name", LookAt:=xlPart)


    If Not nameRange Is Nothing Then

        firstAddress = nameRange.Address

    Do

    membership.Activate
    nameRow = nameRange.Row
    MsgBox (nameRow)
    fullNameString = membership.Cells(nameRow, "A").Value
    'MsgBox (fullNameString)
    nameIndex = InStr(fullNameString, "user -name")
    barIndex = InStr(fullNameString, "|")
    'MsgBox (nameIndex)
    'MsgBox (barIndex)
    userNameString = Mid(fullNameString, nameIndex + 12, ((barIndex - 4) - (nameIndex + 12)))

    groups.Activate
    Set nameRange2 = groups.Range("A:CH").Find(userNameString)
    nameColumn = nameRange2.Column



    membership.Activate
    membership.Cells(nameRow, "A").Activate

    Do
        ActiveCell.Offset(1).Activate

        If Not IsEmpty(ActiveCell.Value) Then

            cellValue = ActiveCell.Value
            groups.Activate
            Set groupRange = groups.Range("A:CH").Find(cellValue, , , LookAt:=xlWhole)
            groupRow = groupRange.Row
            groups.Cells(groupRow, nameColumn).Activate
            ActiveCell.Value = "X"
            membership.Activate

         End If



        Loop Until IsEmpty(ActiveCell.Value)

    Set nameRange = membership.Range("A:A").FindNext(nameRange)
    MsgBox (nameRange.Address)

    Loop While Not nameRange Is Nothing Or nameRange.Address <> firstAddress
    End If


End Sub

1 个答案:

答案 0 :(得分:1)

除非您绝对需要,否则您怀疑“ActiveCell is causing your problems. It's best to avoid ActiveCell and激活and选择”语句是正确的。最好直接使用这些对象。

我认为如果您将Until语句放在Do旁边的Do Loop旁边而不是Loop旁边,那么它也会更好用。

最后,因为您在代码中使用了多个查找,FindNext命令正在使用您在Find对话框中输入的最后一项内容,因此您需要调整该语句以完全执行你想要的。

请参阅下面重构的代码,如果它没有解决问题,请告诉我。请注意我如何限定所有变量,更改了第二个循环中的Until展示位置,并调整了.FindNext行。

Sub AssignGroups()

    Dim membership As Worksheet
    Dim wb As Workbook
    Dim groups As Worksheet
    Dim nameRow As Long
    Dim fullNameString As String
    Dim nameRange As Range
    Dim groupRange As Range
    Dim nameRange2 As Range
    Dim nameIndex As Long
    Dim userNameString As String
    Dim barIndex As Long

    Set wb = ActiveWorkbook
    Set membership = Sheets("User Group Membership")
    Set groups = Sheets("User Assigned to Groups")


    With membership

        Set nameRange = .Range("A:A").Find("user -name", LookAt:=xlPart)


        If Not nameRange Is Nothing Then

            firstAddress = nameRange.Address

            Do

                nameRow = nameRange.Row

                'MsgBox (nameRow)

                fullNameString = .Cells(nameRow, "A").Value

                'MsgBox (fullNameString)

                nameIndex = InStr(fullNameString, "user -name")
                barIndex = InStr(fullNameString, "|")

                'MsgBox (nameIndex)
                'MsgBox (barIndex)

                userNameString = Mid(fullNameString, nameIndex + 12, ((barIndex - 4) - (nameIndex + 12)))

            With groups

                Set nameRange2 = .Range("A:CH").Find(userNameString)
                nameColumn = nameRange2.Column

            End With


            Do Until IsEmpty(.Cells(nameRow, "A").Offset(1))

                cellValue = .Cells(nameRow, "A").Offset(1)

                With groups

                    Set groupRange = .Range("A:CH").Find(cellValue, , , LookAt:=xlWhole)

                    groupRow = groupRange.Row
                    .Cells(groupRow, nameColumn).Value = "X"

                End With

                nameRow = nameRow + 1

            Loop

            Set nameRange = .Range("A:A").Find("user -name", After:=nameRange, LookAt:=xlPart)

        MsgBox (nameRange.Address)

        Loop While Not nameRange Is Nothing Or nameRange.Address <> firstAddress

    End If

End Sub