Link to home
Start Free TrialLog in
Avatar of ResourcefulDB
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(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R2C2:R11C5")
    Set pt = pc.CreatePivotTable(TableDestination:=Range("B5"), _
        TableName:="PivotTable1")
        With pt
        .PivotFields("month").Orientation = xlColumnField
        .PivotFields("region").Orientation = xlRowField
        .PivotFields("sales").Orientation = xlDataField
        End With
End Sub
SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial