2015-10-30 78 views
3

我有一個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 
+0

爲什麼找到A:CH然後第二次找到A:A?結合兩者? – findwindow

+0

因爲一個在組表單上,另一個在成員表上 – Will

回答

1

你在你的懷疑「ActiveCell is causing your problems. It's best to avoid ActiveCell and激活and Select`語句,除非你絕對需要他們是正確的。直接處理對象最好。

我認爲如果您將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 
+0

感謝您的快速響應。出於某種原因,它仍然沒有轉移到下一組數據上。在循環結束時,'nameRange'仍然像以前一樣被設置爲'Users,Builtin'。任何想法爲什麼會發生? – Will

+0

@請參閱我編輯的答案。 –

+0

工作正常!還有一件事,一旦完成了所有數據的處理,似乎並沒有從大循環中突破。如果我們在循環底部設置'nameRange',可能會在Loop檢查條件時弄亂地址嗎? – Will