'Range.Find'和'Range.FindNext'只在第一场比赛中循环
我使用VBA Excel循环访问一组数据。 我正在尝试使用Range.Find
和Range.FindNext
方法的组合来查找某个string。 我很难找出将这些语句放在哪里,因为我需要它们在对方的范围内,但是同时我不希望第一个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
我怎么能把这些陈述放在一起呢?