Changing Source Data Using Macro

therrma2

New Member
Joined
Dec 10, 2009
Messages
2
I have a file where I add new source data every week. The columns are always the same, but the number of rows changes everytime I update the data. I'm trying to create a Macro so that once I copy and paste the new data in, I can simply run the macro and the source data will expand to include the new rows. I'm fairly new to VBA.

Basically, I need the Macro to identify what the last row of data is, and then change the source data on the pivot table to include it.

Thanks for your help.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Assign a DYNAMIC Range Name to the pasted in data.

RangeName: MyData
RefersTo (box): =OFFSET(ANCHOR_CELL, 0, 0, COUNTA(RANGE),0)
sample =OFFSET($A$2, 0, 0, Counta(B:B)-1, 0) ' -1 allow for single-row header
 
Upvote 0
Mistake..
sample =OFFSET($A$2, 0, 0, Counta(B:B)-1, 0)

s/b
sample =OFFSET($A$2, 0, 0, Counta(B:B)-1, 7) << Allow for Columns A-G
 
Upvote 0
Thanks Jim. I was finally able to get it to work. For anyone else with a similar problem, here is my code:

Dim MyData As Range
Set MyData = Sheets("Data Table- Do Not Read").Range("OFFSET($A$7,0,0, COUNTA(D:D),15)")

'
Sheets("Division Summary ").Select
Range("D15").Select
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
MyData _
, Version:=xlPivotTableVersion10)
 
Upvote 0
These 2 lines will set your pasted in range dynamiclly, and name Mydata


ActiveSheet.Names.Add Name:="MyData", RefersTo:="=OFFSET($A$7,0,0,COUNTA(D:D),15)"
Set Mydata = Range("MyData")
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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