VBA Code To Loop Through One Worksheet and Paste Certain Values Into Another

Jiraya_00

New Member
Joined
Oct 18, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I need help writing a VBA code that would allow me to loop through a used range of a worksheet and look for the words "No Match" within in a particular column (column I with header name "Match"). If it finds such a value, then I would like to copy data from columns A through H within that row and paste those values onto another worksheet (at the last row of the used range that would keep expanding as more data is being pasted to that worksheet) within the same workbook.

Any help or guidance is much appreciated!

Screenshot 174501.png

Screenshot 174915.png


Book5 - Test.xlsx
ABCDEFGHI
1NumNameRep AssignedRep SupervisorDBD Rep Original Amt.Paid AmountCMA Inv. AmountMatch
2576916Customer A - Test CompanyRep ARep ABNo Rep1,723.601,723.601,723.60Customer A - Test Company
3578107Customer URep ABRep ABNo Rep2,752.802,752.802,752.80Customer U
4580390Customer ARep ABRep ABNo Rep1,016.801,016.801,016.80Customer A
5582249Customer ARep ARep ABNo Rep2,418.002,418.002,418.00Customer A
6583044Customer ARep ABRep ABNo Rep1,822.801,822.801,822.80Customer A
7586326Customer URep ABRep ABNo Rep2,021.202,021.202,021.20Customer U
8587795Customer ARep ABRep ABNo Rep3,822.452,777.602,777.60Customer A
9587856Customer ARep ABRep ABNo Rep248.00248.00248.00Customer A
10589448Customer ARep ABRep ABNo Rep1,488.001,488.001,488.00Customer A
11591246Customer ARep ARep ABNo Rep930.00930.00930.00Customer A
12594666Customer BRep FRep ABNo Rep919.80919.80919.80Customer B
13595592Customer BRep FRep ABNo Rep1,209.601,209.601,209.60NO MATCH
14596598Customer BRep FRep ABNo Rep415.80415.80415.80Customer B
15602919Customer C - Test CompanyNo RepNo RepRep BC882.00882.00882.00Customer C - Test Company
16605254Customer CNo RepNo RepRep BC1,373.401,373.401,373.40Customer C
17607582Customer DRep FRep ABRep BC0.000.004,158.00Customer D
18606435Customer DRep FRep ABRep BC0.000.001,197.00Customer D
19607693Customer FRep DRep ABNo Rep1,312.001,312.001,512.00Customer F
20607388Customer IRep ARep ABNo Rep982.80982.80982.80Customer I
21608791Customer GNo RepNo RepRep BC529.20529.20529.20NO MATCH
22609018Customer ERep DRep ABRep BC2,935.802,935.802,935.80Customer E
23612210Customer ARep CRep ABNo Rep1,423.801,423.801,423.80Customer A
24610144Customer JRep DRep BCNo Rep529.20529.20529.20Customer J
25610154Customer JRep BCRep BCNo Rep970.20970.20970.20Customer J
26610856Customer E - Test CompanyRep DRep ABRep BC2,079.002,079.002,079.00NO MATCH
27610418Customer KRep DRep ABRep CD997.60997.60997.60Customer K
28611763Customer YRep FRep BCRep CD5,424.151,373.401,373.40Customer Y
29613097Customer HRep ERep ABRep CD1,839.601,839.601,839.60Customer H
30611574Customer XRep DRep ABNo Rep1,751.401,751.401,751.40Customer X
31612467Customer DRep FRep ABRep BC1,260.001,260.001,260.00Customer D
32613731Customer YRep BCRep BCNo Rep1,096.201,096.201,096.20Customer Y
33612406Customer HRep ERep ABRep CD365.40365.40365.40NO MATCH
34613633Customer CNo RepNo RepRep BC1,814.401,814.401,814.40Customer C
35614567Customer XRep BCRep BCNo Rep378.00378.00378.00Customer X
36612504Customer DNo RepNo RepRep BC1,436.401,436.401,436.40Customer D
37614447Customer XRep DRep ABNo Rep508.40508.40508.40Customer X
38612224Customer HRep ERep ABRep CD2,595.602,595.602,595.60Customer H
39613209Customer HRep ERep ABRep CD1,713.601,713.601,713.60NO MATCH
40614454Customer LRep FRep BCNo Rep529.20529.20529.20Customer L
41613226Customer TNo RepNo RepRep CD0.000.002,381.40Customer T
42613033Customer TRep CRep ABNo Rep2,709.002,709.002,709.00Customer T
43613648Customer SRep CRep ABNo Rep1,131.201,131.201,131.20Customer S
44612981Customer ZRep BCRep BCNo Rep604.80604.80604.80Customer Z
45613452Customer HRep ERep ABRep CD2,091.602,091.602,091.60Customer H
46613856Customer MRep CRep ABRep CD359.60359.60359.60Customer M
47615717Customer LRep DRep ABNo Rep1,486.801,486.801,486.80Customer L
48613765Customer ZNo RepNo RepRep BC831.60831.60831.60Customer Z
49613862Customer HRep ERep ABRep CD1,096.201,096.201,096.20NO MATCH
50615199Customer TRep DRep ABRep BC458.80458.80458.80Customer T
51613368Customer SRep BCRep BCNo Rep1,020.601,020.601,020.60Customer S
52615523Customer FRep CRep ABNo Rep2,308.402,308.402,308.40Customer F
53616648Customer ERep DRep ABRep BC1,927.801,927.801,927.80Customer E
54615343Customer S - Test CompanyRep DRep ABRep BC1,289.601,289.601,289.60Customer S - Test Company
55613450Customer GNo RepNo RepRep BC781.20781.20781.20Customer G
56612849Customer PRep DRep ABNo Rep1,449.001,449.001,449.00Customer P
57618209Customer QNo RepNo RepRep CD1,524.601,524.601,524.60Customer Q
58613924Customer ZRep BCRep BCNo Rep1,562.401,562.401,562.40Customer Z
59616360Customer HRep ERep ABRep CD2,066.402,066.402,066.40Customer H
60615589Customer LRep BRep ABNo Rep2,394.002,394.002,394.00Customer L
Source_Data


Book5 - Test.xlsx
ABCDEFGHIJ
1Report Submission DateInvoice #Account NameRep AssignedRep SupervisorDBD RepERP Original Amt.ERP Paid TotalCMA Inv. AmountERP - CMA Variance
27/27/2023576916Customer A - Test CompanyRep ARep ABNo Rep1,723.601,723.601,723.600.00
38/3/2023578107Customer URep ABRep ABNo Rep2,752.802,752.802,752.800.00
48/28/2023580390Customer ARep ABRep ABNo Rep1,448.421,016.801,016.800.00
59/12/2023582249Customer ARep ARep ABNo Rep2,418.002,418.002,418.000.00
69/20/2023583044Customer ARep ABRep ABNo Rep1,822.801,822.801,822.800.00
710/17/2023586326Customer URep ABRep ABNo Rep2,021.202,021.202,021.200.00
810/31/2023587795Customer ARep ABRep ABNo Rep3,822.452,777.602,777.600.00
911/1/2023587856Customer ARep ABRep ABNo Rep248.00248.00248.000.00
1011/14/2023589448Customer ARep ABRep ABNo Rep1,488.001,488.001,488.000.00
1112/7/2023591246Customer ARep ARep ABNo Rep930.00930.00930.000.00
121/5/2024594666Customer BRep FRep ABNo Rep919.80919.80919.800.00
131/22/2024595592Customer BRep FRep ABNo Rep1,209.601,209.601,209.600.00
141/27/2024596598Customer BRep FRep ABNo Rep415.80415.80415.800.00
153/20/2024602919Customer C - Test CompanyNo RepNo RepRep BC882.00882.00882.000.00
164/2/2024605254Customer CNo RepNo RepRep BC1,373.401,373.401,373.400.00
174/17/2024607582Customer DRep FRep ABRep BC0.000.004,158.00(4,158.00)
184/13/2024606435Customer DRep FRep ABRep BC0.000.001,197.00(1,197.00)
195/2/2024607693Customer FRep DRep ABNo Rep1,312.001,312.001,512.00(200.00)
204/30/2024607388Customer IRep ARep ABNo Rep982.80982.80982.800.00
214/30/2024608791Customer GNo RepNo RepRep BC529.20529.20529.200.00
225/13/2024609018Customer ERep DRep ABRep BC2,935.802,935.802,935.800.00
235/26/2024612210Customer ARep CRep ABNo Rep1,423.801,423.801,423.800.00
245/21/2024610144Customer JRep DRep BCNo Rep529.20529.20529.200.00
255/23/2024610154Customer JRep BCRep BCNo Rep970.20970.20970.200.00
265/14/2024610856Customer E - Test CompanyRep DRep ABRep BC2,079.002,079.002,079.000.00
275/27/2024610418Customer KRep DRep ABRep CD1,016.55997.60997.600.00
285/21/2024611763Customer YRep FRep BCRep CD5,424.151,373.401,373.400.00
296/4/2024613097Customer HRep ERep ABRep CD1,839.601,839.601,839.600.00
305/22/2024611574Customer XRep DRep ABNo Rep1,751.401,751.401,751.400.00
315/28/2024612467Customer DRep FRep ABRep BC3,360.001,260.001,260.000.00
326/14/2024613731Customer YRep BCRep BCNo Rep1,096.201,096.201,096.200.00
336/12/2024612406Customer HRep ERep ABRep CD788.77365.40365.400.00
346/10/2024613633Customer CNo RepNo RepRep BC1,814.401,814.401,814.400.00
356/21/2024614567Customer XRep BCRep BCNo Rep378.00378.00378.000.00
365/29/2024612504Customer DNo RepNo RepRep BC1,436.401,436.401,436.400.00
376/13/2024614447Customer XRep DRep ABNo Rep902.87508.40508.400.00
386/12/2024612224Customer HRep ERep ABRep CD4,695.602,595.602,595.600.00
396/5/2024613209Customer HRep ERep ABRep CD2,014.141,713.601,713.600.00
406/17/2024614454Customer LRep FRep BCNo Rep824.69529.20529.200.00
416/19/2024613226Customer TNo RepNo RepRep CD0.000.002,381.40(2,381.40)
426/3/2024613033Customer TRep CRep ABNo Rep2,709.002,709.002,709.000.00
436/8/2024613648Customer SRep CRep ABNo Rep1,131.201,131.201,131.200.00
446/3/2024612981Customer ZRep BCRep BCNo Rep604.80604.80604.800.00
456/10/2024613452Customer HRep ERep ABRep CD2,091.602,091.602,091.600.00
466/28/2024613856Customer MRep CRep ABRep CD359.60359.60359.600.00
476/21/2024615717Customer LRep DRep ABNo Rep1,486.801,486.801,486.800.00
486/10/2024613765Customer ZNo RepNo RepRep BC831.60831.60831.600.00
496/28/2024613862Customer HRep ERep ABRep CD1,096.201,096.201,096.200.00
506/27/2024615199Customer TRep DRep ABRep BC855.02458.80458.800.00
51610856Customer E - Test CompanyRep DRep ABRep BC2,079.002,079.002,079.000.00
Reconciliation
Cell Formulas
RangeFormula
J2:J51J2=H2-I2
 

Attachments

  • Screenshot 174501.png
    Screenshot 174501.png
    137.8 KB · Views: 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
This does not loop through the used range but should transfer all the relevant rows at once.
Test with a copy of your workbook.

VBA Code:
Sub No_Match()
  Application.ScreenUpdating = False
  With Sheets("Source_Data")
    .AutoFilterMode = False
    With .Range("A1", .Range("I" & Rows.Count).End(xlUp))
      .AutoFilter Field:=9, Criteria1:="NO MATCH"
      If .Columns(1).SpecialCells(xlVisible).Count > 1 Then
        .Offset(1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy Destination:=Sheets("Reconciliation").Range("B" & Rows.Count).End(xlUp).Offset(1)
      End If
    End With
    .AutoFilterMode = False
  End With
  Application.ScreenUpdating = True
End Sub
 
  • Like
Reactions: Zot
Upvote 0
Solution
Thank you Peter! This code works exactly as intended. I do have one question regarding this specific line of code here.

May I ask what does Offset (1) achieve here?

VBA Code:
.Offset(1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy Destination:=Sheets("Reconciliation").Range("B" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
Thank you Peter! This code works exactly as intended.
You are welcome. Thanks for the confirmation. :)

May I ask what does Offset (1) achieve here?
Do not know which Offset(1) you mean as there are two of them in that line of code. ;)

.Offset(1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy Destination:=Sheets("Reconciliation").Range("B" & Rows.Count).End(xlUp).Offset(1)

In relation to the purple one:
This moves down one row from the heading row in the source worksheet before copying so that the headings are not copied across to the destination worksheet each time the code is run.

In relation to the red one:
The blue part of the formula locates the last cell with data in column B of the destination worksheet.
If we did not have the red part then the pasted data would over-write the last row of data already in the destination worksheet.
Offset(1) moves down one row (ie to the first available blank row)
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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