Struggling with Loops

kevinh2320

Board Regular
Joined
May 13, 2016
Messages
61
I have two worksheets in my workbook. Sheet1 contains customer order information with column A holding their account number. Sheet2 contains customers name & address information with column A also holding their account number. I need VBA code to start at cell A2 of sheet1 and check the account number. Then move to Sheet2's column A to see if there is a matching account number. If there is a match copy cells "B" through "F" of that row, then return to that accounts row in Sheet1 and paste the Company Name, Address, City, State, ZipCode in cells F through J of that row. Code needs to loop through the entire column A of sheet1 until complete. If there are account numbers that don't match code should just move to the next row until complete.

Hope this make sense. Example sheets 1 & 2 shown below. Thanks for any help.

Sheet1 Example
<style type="text/css"><!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}--></style>[TABLE="width: 0"]
<colgroup><col style="width: 107px"><col width="100"><col width="100"><col width="100"><col width="100"></colgroup><tbody>[TR]
[TD]Acct#[/TD]
[TD]Part#[/TD]
[TD]Qty[/TD]
[TD]UnitPrice[/TD]
[TD]Ttl[/TD]
[/TR]
[TR]
[TD]123456[/TD]
[TD="align: right"]544[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"]$175.00[/TD]
[TD="align: right"]$2,625.00[/TD]
[/TR]
[TR]
[TD]133987[/TD]
[TD="align: right"]443[/TD]
[TD="align: right"]35[/TD]
[TD="align: right"]$100.00[/TD]
[TD="align: right"]$3,500.00[/TD]
[/TR]
[TR]
[TD]239395[/TD]
[TD="align: right"]434[/TD]
[TD="align: right"]66[/TD]
[TD="align: right"]$5.00[/TD]
[TD="align: right"]$330.00[/TD]
[/TR]
[TR]
[TD]102102[/TD]
[TD="align: right"]444[/TD]
[TD="align: right"]33[/TD]
[TD="align: right"]$55.00[/TD]
[TD="align: right"]$1,815.00[/TD]
[/TR]
[TR]
[TD]239395[/TD]
[TD="align: right"]544[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]$85.00[/TD]
[TD="align: right"]$765.00[/TD]
[/TR]
[TR]
[TD]433202[/TD]
[TD="align: right"]132[/TD]
[TD="align: right"]94[/TD]
[TD="align: right"]$35.00[/TD]
[TD="align: right"]$3,290.00[/TD]
[/TR]
[TR]
[TD]103111[/TD]
[TD="align: right"]444[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"]$55.00[/TD]
[TD="align: right"]$8,250.00[/TD]
[/TR]
[TR]
[TD]244365[/TD]
[TD="align: right"]333[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]$100.00[/TD]
[TD="align: right"]$5,000.00[/TD]
[/TR]
[TR]
[TD]411555[/TD]
[TD="align: right"]444[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]$55.00[/TD]
[TD="align: right"]$935.00[/TD]
[/TR]
[TR]
[TD]244987-A[/TD]
[TD="align: right"]233[/TD]
[TD="align: right"]1543[/TD]
[TD="align: right"]$1.50[/TD]
[TD="align: right"]$2,314.50[/TD]
[/TR]
</tbody>[/TABLE]

Sheet2 Example
<style type="text/css"><!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}--></style>[TABLE="width: 0"]
<colgroup><col style="width: 107px"><col width="163"><col width="132"><col width="100"><col width="100"><col width="100"></colgroup><tbody>[TR]
[TD]Acct#[/TD]
[TD]CompanyName[/TD]
[TD]Address[/TD]
[TD]City[/TD]
[TD]State[/TD]
[TD]ZipCode[/TD]
[/TR]
[TR]
[TD="align: right"]123456[/TD]
[TD]ABC Company[/TD]
[TD]123 Lost Ln[/TD]
[TD]Seatte[/TD]
[TD]WA[/TD]
[TD="align: right"]98555[/TD]
[/TR]
[TR]
[TD="align: right"]102102[/TD]
[TD]Hawk Industries[/TD]
[TD]3600 W. 2nd Ave[/TD]
[TD]Kent[/TD]
[TD]WA[/TD]
[TD="align: right"]98366[/TD]
[/TR]
[TR]
[TD="align: right"]103111[/TD]
[TD]J&J Co.[/TD]
[TD]P.O. Box 10[/TD]
[TD]Kirkland[/TD]
[TD]WA[/TD]
[TD="align: right"]98111[/TD]
[/TR]
[TR]
[TD="align: right"]133987[/TD]
[TD]Snip & Clip[/TD]
[TD]P.O. Box 25[/TD]
[TD]Tacoma[/TD]
[TD]WA[/TD]
[TD="align: right"]98232[/TD]
[/TR]
[TR]
[TD="align: right"]244365[/TD]
[TD]Performance Max[/TD]
[TD]434 Anywhere Pl[/TD]
[TD]Sometown[/TD]
[TD]WA[/TD]
[TD="align: right"]98765[/TD]
[/TR]
[TR]
[TD="align: right"]239395[/TD]
[TD]Joe Joe's[/TD]
[TD]765 Where St.[/TD]
[TD]Anytown [/TD]
[TD]WA[/TD]
[TD="align: right"]98767[/TD]
[/TR]
[TR]
[TD="align: right"]239395[/TD]
[TD]Automax West[/TD]
[TD]P.O. Box 15776[/TD]
[TD]Lost City[/TD]
[TD]CA[/TD]
[TD="align: right"]92888[/TD]
[/TR]
[TR]
[TD="align: right"]244987-A[/TD]
[TD]Tudle Row[/TD]
[TD]6434 Salem St.[/TD]
[TD]Tacoma[/TD]
[TD]WA[/TD]
[TD="align: right"]98232[/TD]
[/TR]
[TR]
[TD="align: right"]433202[/TD]
[TD]Brewmiesters Cache[/TD]
[TD]101 Whiskey Pl.[/TD]
[TD]Redmond[/TD]
[TD]WA[/TD]
[TD="align: right"]98664[/TD]
[/TR]
[TR]
[TD="align: right"]411555[/TD]
[TD]J. Co.[/TD]
[TD]888 Maple St.[/TD]
[TD]Tukwilla[/TD]
[TD]WA[/TD]
[TD="align: right"]98334[/TD]
[/TR]
[TR]
[TD="align: right"]388998[/TD]
[TD]BnB Traders[/TD]
[TD]P.O. Box 7[/TD]
[TD]Anchorage[/TD]
[TD]AK[/TD]
[TD="align: right"]99510[/TD]
[/TR]
[TR]
[TD="align: right"]325463[/TD]
[TD]CC Co.[/TD]
[TD]1243 Made Up Ln.[/TD]
[TD]Seatte[/TD]
[TD]WA[/TD]
[TD="align: right"]98765[/TD]
[/TR]
[TR]
[TD="align: right"]333321[/TD]
[TD]Music World[/TD]
[TD]555 5th Ave.[/TD]
[TD]Edmonds[/TD]
[TD]WA[/TD]
[TD="align: right"]98768[/TD]
[/TR]
[TR]
[TD="align: right"]383009[/TD]
[TD]Pets and Pamper[/TD]
[TD]4787 3rd St.[/TD]
[TD]Kent[/TD]
[TD]WA[/TD]
[TD="align: right"]98366[/TD]
[/TR]
[TR]
[TD="align: right"]520020[/TD]
[TD]Industrial Supply Inc.[/TD]
[TD]88457 54th Ave.[/TD]
[TD]Ballard[/TD]
[TD]WA[/TD]
[TD="align: right"]98444[/TD]
[/TR]
[TR]
[TD="align: right"]500011[/TD]
[TD]Recreational Adventures[/TD]
[TD]232 Easy St.[/TD]
[TD]Seatte[/TD]
[TD]WA[/TD]
[TD="align: right"]98437[/TD]
[/TR]
[TR]
[TD="align: right"]655837[/TD]
[TD]Zip Deliveries[/TD]
[TD]343 4th Ave[/TD]
[TD]Mt. Vernon[/TD]
[TD]WA[/TD]
[TD="align: right"]98876[/TD]
[/TR]
[TR]
[TD="align: right"]299534-B[/TD]
[TD]Green Thumb Landscaping[/TD]
[TD]400 Cherry Dr.[/TD]
[TD]Lynnwood[/TD]
[TD]WA[/TD]
[TD="align: right"]98554[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Code needs to loop through the entire column A of sheet1 until complete.
Is there a particular reason the code must use a loop procedure?
If not, you could try this with a copy of your workbook.

Code:
Sub Lookup_Data()
  Dim ws1 As Worksheet, ws2 As Worksheet
  Dim lr2 As Long
  
  Set ws1 = Sheets("Sheet1")
  Set ws2 = Sheets("Sheet2")
  lr2 = ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row
  With ws1.Range("F2:F" & ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row)
    .Formula = Replace(Replace("=IFERROR(INDEX('#'!B$2:B$%,MATCH($A2,'#'!$A$2:$A$%,0)),"""")", "#", ws2.Name), "%", lr2)
    .Copy Destination:=.Resize(, 5)
    .Resize(, 5).Value = .Resize(, 5).Value
  End With
End Sub
 
Upvote 0
Hello Kevin,

Here's another option:-
Code:
Sub Test()

Dim lr As Long
Dim fValue As Range, c As Range, x As Integer

lr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row

Application.ScreenUpdating = False

For Each c In Sheet1.Range("A2:A" & lr)
Set fValue = Sheet2.Columns("A:A").Find(c.Value)
           If fValue Is Nothing Then GoTo NextC
           If c.Value = fValue.Value Then
           x = fValue.Row
           Sheet2.Range(("B" & x), ("F" & x)).Copy Sheet1.Range("F" & Rows.Count).End(3)(2)
     End If
NextC:
Next c

Application.ScreenUpdating = True

End Sub

I've noticed that in your sample data you have the same account number for two different customers which will affect both of the supplied codes above. You may want to check this.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
I've noticed that in your sample data you have the same account number for two different customers
Good spotting, I hadn't noticed that.

My code will return the data from the first row in Sheet2 that meets the criteria. So for that repeated one it will return Joe Joe's on both occasions.
 
Upvote 0
Good afternoon Peter,

The same will happen with my code. I'm sure its just a typo on the OP's part.

I like your formula idea BTW.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,224,755
Messages
6,180,758
Members
452,996
Latest member
nelsonsix66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top