我正在使用VBA Excel循环访问一组数据。我试图使用Range.Find
和Range.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
我怎样才能放置这些语句,以便一个接一个地遍历所有匹配?