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,
RGuillermoProject ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>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?
RGuillermoProject ManagerAuthor Commented:
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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

RGuillermoProject ManagerAuthor Commented:
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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
sorry, as I just made my guess as your scripts look similar to VBA codes so I wrote it in macro environment.

perhaps you can make referencing to Activesheet object if you manage to refer to it? like:
LastRow = Activesheet.Cells(Activesheet.Rows.Count, "A").End(Excel.XlDirection.xlUp).Row

Open in new window

Get The Last Used Excel Cell - Visual Basic .NET
http://www.visual-basic-tutorials.com/LastUsedCell.htm

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RGuillermoProject ManagerAuthor Commented:
Thank you  very much!!
RGuillermoProject ManagerAuthor Commented:
Works perfect!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.