I am new to VBA and I am trying to write some code that selects gets the range of all of the cells in a different worksheet than the current one, prints it in a message box (for testing purposes), and returns to the original sheet. I intend to use this range to create a pivot table in the new sheet, and so In the end it needs to be of the form "R1C1:R929C25
" To do this, I used code that I found here:
How do you select the entire Excel sheet with Range using Macro in VBA?
This is the code that I have so far:
Sheets("My_Sheet_Name").Cells.Select
MsgBox (Str(Range(Cells.Address)))
Sheets(Sheets.Count).Select
However, this returns an error:
Run-time error '1004':
Select method of Range class failed.
Edit:
I have gotten the code to get the range, yet how do I make it into the form "R1C1:R929C25
"?
COMPLETED WORKING CODE:
Dim rng As Range
Set rng = Sheets("My_Sheet").UsedRange
Dim rc_range
rc_range = "R" & Trim(Str(rng.Rows.Count)) & "C" & Trim(Str(rng.Columns.Count))
MsgBox (rc_range)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"My_Sheet!R1C1:" & rc_range, Version:=xlPivotTableVersion10). _
CreatePivotTable TableDestination:=ActiveSheet.Name + "!R4C4",
TableName:=inputValue + "_PivotTable" _ , DefaultVersion:=xlPivotTableVersion10