Need clarification on setting Range in excel from R1c1 to range("a1") type...

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
When i record a macro it shows...
"Sheet3!R1C1:R49C7"

I want it in like Sheets("Sheet3").Range("A1:E" & lr) etc.

Code:
[FONT=Courier New]       ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _[/FONT]
[FONT=Courier New]           PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _[/FONT]
[FONT=Courier New]           "path\[budget pivot table.xls]Sheet3!R1C1:R49C7" _[/FONT]
[FONT=Courier New]           , Version:=xlPivotTableVersion10)[/FONT]

I want to pick range till last row. Please help me out here...
This must be simple but stucked...


Thanks for helping.

I tried like this...

"path\[budget pivot table.xls]Sheet3!" & Range("A1:G49") & , Version:=xlPivotTableVersion10)

but still errors...
 
Last edited:
Okay, you need to open it to get the last row from that sheet.

See if this works for you - it's completely untested. I've added (somewhat pointless) comments to explain what the code is doing.
Code:
Sub foo()
    
    Dim wkbSourceData As Workbook
    Dim lngLastRow  As Long
    Dim ptcNew As PivotCache
    Dim rngSourceData As Range
    
    'turn off screen updating so the user doesn't know we opened another workbook
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    'open the other workbook so we can get the last used row
    Set wkbSourceData = Workbooks.Open("E:\Personal Files\Excel Files - Brian\budget pivot table\budget pivot table.xls")
    
    With wkbSourceData.Worksheets("Sheet3")
        lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row    'get the last used row in Sheet3!A:A
        Set rngSourceData = .Range("A1:G" & lngLastRow)         'now we have a reference to the new pivotcache range
    End With
    
    'create the new pivotcache
    Set ptcNew = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSourceData, Version:=xlPivotTableVersion10)
    
    'change the pivotcache of the Pivot_ped1 pivot table
    ThisWorkbook.Worksheets("Sheet1").PivotTables("Pivot_ped1").ChangePivotCache ptcNew
    
    wkbSourceData.Close SaveChanges:=False
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Collin, great!!! it is perfect...i'll go with this!

I appriciate you help.

Norie, thanks for the idea of adding lastrow vba to R1C1 method...


Thanks again guys!
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,504
Members
452,917
Latest member
MrsMSalt

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