Select flexible range in VBA

Posted on 2007-08-01
Last Modified: 2013-11-27

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?


The following is the sample code:

Sub Macro2()
Dim pc As PivotCache
Dim pt As PivotTable
    Set pc = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    Set pt = pc.CreatePivotTable(TableDestination:=Range("B5"), _
        With pt
        .PivotFields("month").Orientation = xlColumnField
        .PivotFields("region").Orientation = xlRowField
        .PivotFields("sales").Orientation = xlDataField
        End With
End Sub
Question by:ResourcefulDB
    LVL 81

    Assisted Solution

    by:zorvek (Kevin Jones)
    CurrentRegion should work as long as there are no values in any adjacent columns or rows. If there are then insert new empty columns and rows and then hide them.

    LVL 38

    Accepted Solution

    Hi RDB,

    Kevin's right about CurrentRegion but to use it as your PivotTable SourceData you need to do something like this:

    Set ws = Worksheets("Sheet1")

    Set pc = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=ws.Range("C3").CurrentRegion)

    or this:

    Set ws = Worksheets("Sheet1")

    Set pc = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        ws.Name & "!" & ws.Range("C3").CurrentRegion.Address(ReferenceStyle:=xlR1C1))


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
    This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
    This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now