vba中的FindNext沒有繼續前進

[英]FindNext in vba not moving on


I have a vba script that goes over a set of data just like the one below. It picks out the user name, in this case "Administrator" and then goes over to another spreadsheet and searches for that user name. Once it finds it, it comes back to the other spreadsheet and goes down one cell, and searches for the text within that cell in the other spreadsheet. In the cell that intersects the column (containing the user name) and the row (containing the other set of data) it marks an "X". It then loops over this set of data, doing this for each piece of data under that user name. Then it moves onto the next set of data, which is in the same format, and does the same process all over again. Or at least it is supposed to. Everything about this script works except after it gets to the bottom of the first set of data, which is an empty cell, it doesn't set the range right to find the next match. For some reason the FindNext call finds "Users,Builtin" which is the last piece of data in the first set. I am really confused as to why this is happening. I have a feeling it might have to do with how I am using the ActiveCell but I am really not sure. Any help with this issue would be greatly appreciated!

我有一個vba腳本,它遍歷一組數據,就像下面的那樣。它會選擇用戶名,在本例中為“Administrator”,然后轉到另一個電子表格並搜索該用戶名。一旦找到它,它將返回到另一個電子表格並向下移動一個單元格,並在另一個電子表格中搜索該單元格中的文本。在與列(包含用戶名)和行(包含另一組數據)相交的單元格中,它標記為“X”。然后循環遍歷這組數據,為該用戶名下的每個數據執行此操作。然后它移動到下一組數據,這些數據采用相同的格式,並重新進行相同的處理。或者至少它應該是。關於這個腳本的所有內容都有效,除非它到達第一組數據的底部,這是一個空單元格,它沒有設置正確的范圍來查找下一個匹配。由於某種原因,FindNext調用找到“Users,Builtin”,它是第一組中的最后一條數據。我真的很困惑為什么會這樣。我有一種感覺,這可能與我如何使用ActiveCell有關,但我真的不確定。任何有關此問題的幫助將不勝感激!

Script:

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

Data:

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

1 个解决方案

#1


1  

You are correct in your suspicion that 'ActiveCellis causing your problems. It's best to avoidActiveCellandActivateandSelect` statements, unless you absolutely need them. It's better to work directly with the objects.

你懷疑'ActiveCellis導致你的問題是正確的。除非你絕對需要它們,否則最好避免使用ActiveCellandActivate和Select`語句。最好直接使用對象。

I think if you place the Until statement next to the Do in the second Do Loop instead of next to the Loop it will work better as well.

我想如果你在第二個Do循環中的Do旁邊放置Until語句而不是循環旁邊,它也可以更好地工作。

Lastly, because you are using multiple finds in the code the FindNext command is using the last thing you entered in the Find dialogue, so you need to adjust that statement as well to do exactly what you want.

最后,因為您在代碼中使用了多個查找,所以FindNext命令使用您在“查找”對話框中輸入的最后一項內容,因此您還需要調整該語句以完全按照您的意願執行操作。

See the code I refactored below and let me know if it does not solve the problem. Notice how I qualified all the variables, changed the Until placement in the second loop, and adjust the .FindNext line.

請參閱下面重構的代碼,讓我知道它是否解決了問題。注意我如何限定所有變量,更改了第二個循環中的直到放置,並調整.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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/10/30/c5d167fc8b358bf937e8938558a3c4ac.html



 
  © 2014-2022 ITdaan.com 联系我们: