Change "CurrentRegion" to "CurrentRegion+1 Row"

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I have this code below that selects the CurrentRegion in a pivot table/cache.

How can I add one row to the current region in the below code?


Rich (BB code):
 ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveSheet.PivotTables("PivotTable2").ChangePivotCache ActiveWorkbook. PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=Worksheets("SDB").Range("A1").CurrentRegion) '<<< change to "CurrentRegion" PLUS ONE ROW


Thanks much
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
See if this works for you:
VBA Code:
    Dim cr As Range
    Set cr = Worksheets("SDB").Range("A1").CurrentRegion
    
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveSheet.PivotTables("PivotTable2").ChangePivotCache ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:=cr.Resize(cr.Rows.Count + 1, cr.Columns.Count))
 
Upvote 0
Solution
Thank you very much for that code!

It works and I can use it, one minor caveat to it is that it removes groupings/filters (by year) I had, resulting in me having to re-group by year and then re-filter out the years I don't want to see.

Not a huge deal though and I can workaround it by re-grouping/re-filtering.

If you have some magical piece of code that I can insert to prevent the un-grouping/un-filtering to happen, that would be great.

If not, I am good and again thank you much!
 
Upvote 0
I may be wrong, but I think that maybe because you are changing the size of the original range that the filters were applied to, it automatically removes the old filters.
I think the way around that is to just reapply them. You may be able to get the VBA code required to do that using the Macro Recorder.
 
Upvote 0
Ok, yes, I believe that you are correct and that's what I will do.

Thanks again
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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