我有一個vba腳本,通過一組數據就像下面的一個。它挑出用戶名,在這種情況下是「管理員」,然後轉到另一個電子表格並搜索該用戶名。一旦找到它,它就會回到其他電子表格並下到一個單元格,然後在另一個電子表格的該單元格內搜索該文本。在與列(包含用戶名)和行(包含另一組數據)相交的單元格中,它將標記爲「X」。然後它循環遍歷這組數據,對該用戶名下的每條數據執行此操作。然後它移動到下一組數據,這些數據是相同的格式,並重新執行相同的過程。或者至少它應該是。關於這個腳本的所有內容除了到達第一組數據的底部之外都是有效的,這是一個空單元格,它沒有設置範圍以查找下一個匹配。出於某種原因,FindNext
調用找到「Users,Builtin」,它是第一組中的最後一條數據。我真的很困惑,爲什麼會發生這種情況。我有一種感覺,它可能與我如何使用ActiveCell
有關,但我真的不確定。任何有關這個問題的幫助將不勝感激!FindNext在vba不能繼續
腳本:
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
數據:
C:\Documents and Settings\Administrator.CHESCONETAD>dsquery user -name "Administrator" | dsget user -memberof -expand
Administrators,Builtin
Domain Admins,Users
Domain Users,Users
EMLibrary Users,Users
Enterprise Admins,Users
Exchange Organization Administrators,OU=Microsoft Exchange Security Groups
Exchange Public Folder Administrators,OU=Microsoft Exchange Security Groups
Exchange Recipient Administrators,OU=Microsoft Exchange Security Groups
Exchange View-Only Administrators,OU=Microsoft Exchange Security Groups
Group Policy Creator Owners,Users
Schema Admins,Users
SophosAdministrator,Users
Users,Builtin
爲什麼找到A:CH然後第二次找到A:A?結合兩者? – findwindow
因爲一個在組表單上,另一個在成員表上 – Will