简体   繁体   中英

Visual Basic, VBA array loop

I used http://www.homeandlearn.org/arrays_and_loops.html to help me with this.

I have data connections that filter to one table. Unfortunately one of the sources randomly places incorrect data (usually a date) in the wrong column (Client column) when the program exports the file to Excel. What I'd like to do is something similar to an index/match function. I'd like to check each reservation number in this master table (A) against another table (B) within the same workbook. If the reservation number from the other sheet (B) matches the reservation number in the master table (A), I would like to have the correct Client value entered into the master table (A). I'm still pretty new to VBA so any help is appreciated. I've tried to modify my code here and there but to no avail. Also, I was originally running this as a practice without the real data so I didn't mess up my original file. I tried to add the appropriate syntax to refer to other sheets and whatnot so I suppose that could be entered incorrectly as well. Here's the closest original code I came up with:

Sub TransferData()
Dim MyArray(1 To 19) As Single

MyArray(1) = 81899
MyArray(2) = 87172
MyArray(3) = 87275
MyArray(4) = 87394
MyArray(5) = 87446
MyArray(6) = 87496
MyArray(7) = 87621
MyArray(8) = 87631
MyArray(9) = 87726
MyArray(10) = 87822
MyArray(11) = 87858
MyArray(12) = 88041
MyArray(13) = 88097
MyArray(14) = 88127
MyArray(15) = 88160
MyArray(16) = 88191
MyArray(17) = 88359
MyArray(18) = 88487
MyArray(19) = 88545

For i = 1 To 19
    If Worksheets("Sheet1").Range("B" & i).Value = MyArray(i) Then
       Worksheets("Sheet2").Range("P" & i).Value = _ 
       Worksheets("Sheet1").Range("E" & i).Value
    End If
Next i

End Sub

I don't recall the error because the code wasn't exactly as above but close to it. I believe the issue it was running into was that when the i variable went above 19, the system couldn't find arrays > 19. I need VBA to check 19 arrays in an ever changing number of rows that's currently at 3k+. I attempted to add another variable thinking if I kept the variables separate, I could have VBA check the 19 arrays against all the rows. Here's the code I came up with for that....

Sub TransferData()
Dim MyArray(1 To 19) As Single

MyArray(1) = 81899
MyArray(2) = 87172
MyArray(3) = 87275
MyArray(4) = 87394
MyArray(5) = 87446
MyArray(6) = 87496
MyArray(7) = 87621
MyArray(8) = 87631
MyArray(9) = 87726
MyArray(10) = 87822
MyArray(11) = 87858
MyArray(12) = 88041
MyArray(13) = 88097
MyArray(14) = 88127
MyArray(15) = 88160
MyArray(16) = 88191
MyArray(17) = 88359
MyArray(18) = 88487
MyArray(19) = 88545

For i = 1 To 5000
For j = 1 To 19
If Worksheets("Sheet1").Range("B" & i).Value = MyArray(j) Then
    Worksheets("Sheet2").Range(i, 16).Value = Worksheets("Sheet1"). _ 
    Range(i,5).Value
    Next j
End If
Next i

End Sub

With this code I get compile error: Next without For. In searching online I found it might be because I have 2 "For"s, an if statement, "next" statement within the if statement, then another "next" statement outside of the loop. I was thinking it had to be done this way so that each cell in the B column gets checked against all the array possibilities.

See pictures below. I need the value of Column P (Actual Billing Name) from sheet: TMRtoSPIde to be entered into Column D (Billing Name) on sheet: RawData when the Reservation # in Column K from sheet: TMRtoSPIde matches the reservation in sheet: RawData. You'll notice the sheet: RawData has an erroneous 5 digit serial date in the Billing Name column. These are what I'm trying to replace.

在此处输入图片说明

在此处输入图片说明

Dictionaries and Collections are ideal for matching unique values. In this example I use a Scripting.Dictionary to store Unique ID's and references to the EntireRow that they are found.

Note: Range().Range() will return a reference that is relative to the first range object (eg Range("A10").EntireRow.Range("ZZ1").Address returns $ZZ$10 ).

It would have simpler to store just the needed value, I just wanted to demonstrate that you can store Objects references in a Dictionary. It is important to note that you can store Objects as both keys and/or values in a Dictionary. A common mistake people make is to try and store range references as keys dictionary.Add Cells(1,1), Cells(1,2) will store a reference to Cells(1,1) as a key and Cells(1,2) as it's value. The problem with this is that Dictionaries don't know how to compare cells and you will not be able to look up your values based on there key relationships. dictionary.Add Cells(1,1).Value, Cells(1,2) is the correct syntax.

Sub TransferData()
    Dim r As Range, Source As Range
    Dim d As Object
    Set d = CreateObject("Scripting.Dictionary")
    With Worksheets("TMRtoSPIde")
        For Each r In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
            If Not d.Exists(r.Value) Then d.Add r.Value, r.EntireRow
        Next
    End With

    With Worksheets("RawData")
        For Each r In .Range("B2", .Range("B" & .Rows.Count).End(xlUp))
            If d.Exists(r.Value) Then
                r.EntireRow.Range("K1").Value = d(r.Value).Range("P1").Value
            End If
        Next
    End With

End Sub

Your loop should probably be like this:

For i = 1 To 5000
    For j = 1 To 19
        If Worksheets("Sheet1").Cells(i, "B").Value = MyArray(j) Then
            Worksheets("Sheet2").Cells(i, "P").Value = Worksheets("Sheet1").Cells(i, "E").Value
            'Exit from the "For j" loop if we found a match
            Exit For
        End If
    Next j
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM