'Range.Find'和'Range.FindNext'仅循环第一场比赛

时间:2015-10-23 15:40:18

标签: excel vba loops scope find

我正在使用VBA Excel循环访问一组数据。我试图使用Range.FindRange.FindNext方法的组合来查找某个字符串。我很难找出放置这些语句的位置,因为我需要它们在彼此的范围内,但同时我不希望第一个Find语句每次都继续执行,因此只有循环在第一场比赛。

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
        Set nameRange = membership.Range("A:A").Find("user -name", Lookat:=xlPart)
    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(ActiveCell)

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


End Sub  

我怎样才能放置这些语句,以便一个接一个地遍历所有匹配?

0 个答案:

没有答案