Link to home
Start Free TrialLog in
Avatar of RGuillermo
RGuillermoFlag for United States of America

asked on

Excell Automation... how to select correct range for a pivot table.

Hello Experts,
I have this visual basic code that opens an excell workbook generated by a database.
This workbook have always same amount of columns but different amount of lines.

The pivot table is to be created programatically
So my code opens the workbook but it selects a fixed range.

HOw can I make it select the correct range programatically?

Regards,
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

>>So my code opens the workbook but it selects a fixed range.
can you tell us how the fixed range was being selected? via programming or it was manually set in the Excel file?
Avatar of RGuillermo

ASKER

Hi Ryan Chong
Here is part of the code I am copying from an excell macro

    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "a04!R1C1:R19C32", Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:="Sheet4!R3C1", TableName:="PivotTable4", DefaultVersion _
        :=xlPivotTableVersion10
    Sheets("Sheet4").Select
    Cells(3, 1).Select
    Sheets("Sheet4").Select

As you can see the macro puts a fixed range.....
Thank you
so i guess the part:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "a04!R1C1:R19C32", Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:="Sheet4!R3C1", TableName:="PivotTable4", DefaultVersion _
        :=xlPivotTableVersion10

need to be updated? if yes, you can try use the the line below to get the last Row of the worksheet so that it can be placed later into the code above, try this:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "a04!R1C1:R19C" & LastRow, Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:="Sheet4!R3C1", TableName:="PivotTable4", DefaultVersion _
        :=xlPivotTableVersion10
    Sheets("Sheet4").Select
    Cells(3, 1).Select
    Sheets("Sheet4").Select

Open in new window

see if this works?
Ryan,
Sorry my visual basic 2013 source does not recognize commads like CELLS,RANGE, XLUP...etc.
Do i need a special imports ?
What are you using ?
Can you send me a complete example of your test source
Regards
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore 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
Thank you  very much!!
Works perfect!