ResourcefulDB
asked on
Select flexible range in VBA
Experts,
I have a question in VBA. How do we select a flexible range of cells?
For example, the following code will let me create a pivot table from the source data R2C2:R11C5. Now that if we want that range to be flexible. The only thing we know about the data range is all the data surround cell "C3". So, I tried to use
SourceData := Range("C3").currentRegion. address
to replace
SourceData:= "Sheet1!R2C2:R11C5"
But it does not seem to work. Anyone knows a better way to do it?
Thanks,
RDB
The following is the sample code:
Sub Macro2()
Dim pc As PivotCache
Dim pt As PivotTable
Set pc = ActiveWorkbook.PivotCaches .Add(Sourc eType:=xlD atabase, SourceData:= _
"Sheet1!R2C2:R11C5")
Set pt = pc.CreatePivotTable(TableD estination :=Range("B 5"), _
TableName:="PivotTable1")
With pt
.PivotFields("month").Orie ntation = xlColumnField
.PivotFields("region").Ori entation = xlRowField
.PivotFields("sales").Orie ntation = xlDataField
End With
End Sub
I have a question in VBA. How do we select a flexible range of cells?
For example, the following code will let me create a pivot table from the source data R2C2:R11C5. Now that if we want that range to be flexible. The only thing we know about the data range is all the data surround cell "C3". So, I tried to use
SourceData := Range("C3").currentRegion.
to replace
SourceData:= "Sheet1!R2C2:R11C5"
But it does not seem to work. Anyone knows a better way to do it?
Thanks,
RDB
The following is the sample code:
Sub Macro2()
Dim pc As PivotCache
Dim pt As PivotTable
Set pc = ActiveWorkbook.PivotCaches
"Sheet1!R2C2:R11C5")
Set pt = pc.CreatePivotTable(TableD
TableName:="PivotTable1")
With pt
.PivotFields("month").Orie
.PivotFields("region").Ori
.PivotFields("sales").Orie
End With
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.