Consolidate Range(Record Marco) need the Macro to be dynamic

Oberon70

Board Regular
Joined
Jan 21, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
I have the below data as an example. The amount of data can change with the statement I copy from. So, I was wondering how do I go about creating a dynamic range that will consolidate the data or how do I change the Marco below so that it uses dynamic ranges?

VBA Project - Dummy file.xlsm
ABCD
2295678410-$50.00-$38.50-$11.50
3394308240$50.00$38.50$11.50
4889035494$10.00$7.70$2.30
5939394939$2,020.00$1,555.40$464.60
6848593874$10.00$7.70$2.30
7239394838$25.00$19.25$5.75
838299999$20.00$15.40$4.60
9675768476$4.82$3.71$1.11
10545496854$424.96$327.22$97.74
11111111111$10.00$7.70$2.30
12545485848$10.00$7.70$2.30
13485848584$20.00$15.40$4.60
14340145338$500.00$385.00$115.00
15848593874$10.00$7.70$2.30
16222222222$5.00$3.85$1.15
17768576757$10.00$7.70$2.30
18545485848$10.00$7.70$2.30
19394308240$50.00$38.50$11.50
2099495848$100.00$77.00$23.00
21889035494$10.00$7.70$2.30
22848593874$10.00$7.70$2.30
23768576757$10.00$7.70$2.30
24239394838$25.00$19.25$5.75
2538299999$20.00$15.40$4.60
26545485848$10.00$7.70$2.30
27909945984$10.00$7.70$2.30
28359489584$3.02$2.32$0.70
29878574757$51.76$39.85$11.91
30848593874$10.00$7.70$2.30
31485848584$20.00$15.40$4.60
32222222222$5.00$3.85$1.15
33889035494$10.00$7.70$2.30
34545485848$10.00$7.70$2.30
35848593874$10.00$7.70$2.30
36394308240$50.00$38.50$11.50
Receipt
Cell Formulas
RangeFormula
B2:B36B2=VLOOKUP(A2,Statement_Data,6,FALSE)
C2:C36C2=VLOOKUP(A2,Statement_Data,7,FALSE) + VLOOKUP(A2,Statement_Data,9,FALSE)
D2:D36D2=B2-C2
Named Ranges
NameRefers ToCells
Statement_Data=data!$A$2:$V$36B2:C36


Below is the code I received from Record Marco

VBA Code:
Sub Consolidate()
'
' Consolidate Macro
'

'
    Range("I2").Select
    Application.CutCopyMode = False
    Selection.Consolidate Sources:= _
        "'C:\Downloads\[VBA Project - Dummy file.xlsm]Receipt'!R2C1:R36C4", Function _
        :=xlSum, TopRow:=False, LeftColumn:=True, CreateLinks:=False
End Sub

And below is the end result

VBA Project - Dummy file.xlsm
IJKL
2295678410-$50.00-$38.50-$11.50
3394308240$150.00$115.50$34.50
4889035494$30.00$23.10$6.90
5939394939$2,020.00$1,555.40$464.60
6848593874$50.00$38.50$11.50
7239394838$50.00$38.50$11.50
838299999$40.00$30.80$9.20
9675768476$4.82$3.71$1.11
10545496854$424.96$327.22$97.74
11111111111$10.00$7.70$2.30
12545485848$40.00$30.80$9.20
13485848584$40.00$30.80$9.20
14340145338$500.00$385.00$115.00
15222222222$10.00$7.70$2.30
16768576757$20.00$15.40$4.60
1799495848$100.00$77.00$23.00
18909945984$10.00$7.70$2.30
19359489584$3.02$2.32$0.70
20878574757$51.76$39.85$11.91
Receipt
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I have found the below code on this message board. I was running it so, I could go step by step to see if it could be modified to my needs. However, I get an error when I try to run the Macro

VBA Code:
Sub Consolidate_Data()
Dim lr As Long, lr2 As Long
lr = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
lr2 = Sheet3.Cells(Rows.Count, 2).End(xlUp).Row
Sheets(1).Range("A2").Consolidate Array("Sheet2!R2C1:R" & lr & "C1", "Sheet3!R2C2:R" & lr2 & "C2"), xlSum
End Sub

I receive the below error message:

1643152500510.png


and the below is higlighted in the marcro:

1643152772815.png
 

Attachments

  • 1643152638191.png
    1643152638191.png
    11 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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