Consulting

Results 1 to 16 of 16

Thread: Excel VBA with Pivot Table

  1. #1

    Excel VBA with Pivot Table

    Hi guys,

    Need some pointers over here.

    I have this program that is able to generate a CSV file every month, with its columns the same.

    I need to create a excel spreadsheet, that have a Pivot Table with the values from this CSV file.

    Output: This excel spreadsheet is able to open out CSV file and create/refresh the current Pivot Table based on the new values from the CSV file each month.

    Can someone guide me or give me some pointers or how do I go about achieving this?


    Thanks in advance.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do it manually in Excel withy the macro recorder on, that will give you a good start.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    But in the future, those CSV files that are being generated, how to handle that? Need to record the macro again?

  4. #4
    I would do a template Excel Workbook with the VBA macro in then dump the info into a data sheet then get the VBA to generate the pivot table from that like below

    'creating data Range for Pivot Table
    Worksheets("Data").Select

    Dim datarange As Range
    Set datarange = Worksheets("Data").Range("A1", Cells(Range("A65000").End(xlUp).Row, Range("XX1").End(xlToLeft).Column))

    'Creat Pivot Table

    Sheets("Ptable").Select
    Application.CutCopyMode = False
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    datarange, Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="Ptable!R1C1", TableName:="Whatever", DefaultVersion _
    :=xlPivotTableVersion14
    Sheets("Ptable").Select
    With ActiveSheet.PivotTables("Whatever").PivotFields("Fieldname")
    .Orientation = xlRowField
    .Position = 1
    End With
    ActiveSheet.PivotTables("Whatever").AddDataField ActiveSheet.PivotTables( _
    "Whatever").PivotFields("Value"), "Sum of Value", xlSum
    you could of course change the pivot table it is a simple version

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by bryant_16 View Post
    But in the future, those CSV files that are being generated, how to handle that? Need to record the macro again?
    No, you would tidy-up the recorded code and turn it into a repeatable macro.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Quote Originally Posted by xld View Post
    No, you would tidy-up the recorded code and turn it into a repeatable macro.
    Can I check with you how to turn it into a repeatable macro?

  7. #7
    Quote Originally Posted by Jayfunk360 View Post
    I would do a template Excel Workbook with the VBA macro in then dump the info into a data sheet then get the VBA to generate the pivot table from that like below


    you could of course change the pivot table it is a simple version
    How do you dump the the info into a data sheet? Copy and paste in?

  8. #8
    you could c+p or at least copy it and when the workbook opens run a piece of code to dump it into the data sheet

  9. #9
    Quote Originally Posted by Jayfunk360 View Post
    you could c+p or at least copy it and when the workbook opens run a piece of code to dump it into the data sheet
    But I cannnot c+p, I would need to open the CSV file within the Excel Spreadsheet first then can run the codes to generate the Pivot Table.

  10. #10
    You could open it in a different workbook and c+p it or Import the data

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Add it to your Personal.xlsm workbook, or create a utility add-in.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Quote Originally Posted by Jayfunk360 View Post
    You could open it in a different workbook and c+p it or Import the data
    How do you import the data over through automation? Is there any pointers/examples which I can look at?

  13. #13
    Quote Originally Posted by xld View Post
    Add it to your Personal.xlsm workbook, or create a utility add-in.
    How do you go about creating an utility add-in? But each time the user opens up a new csv file, the utility add-in will be inside?

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not if you add it to Personal.xlsm or an addin mate, these will always be loaded into Excel.

    I would write the utility to get the user to navigate to a folder, and pick the csv file, open it and process it. You can add a button to the ribbon or the QAT to launch it.

    Here is a nice link on Personal.xlsm.

    And here is one about addins.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15
    Hi there, what you mean by writing the utility to get the user to pick the csv file, open it and process it?

    You mean write it inside the Excel?

  16. #16
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
     "" & Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1)) & _
    "!R1C33:R1048576C106", Version:=xlPivotTableVersion12). _
            CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
            , DefaultVersion:=xlPivotTableVersion12
    I have this, but it is always pointing to Sheet1, how do I make it in such a way, it is always a new sheet inside the spreadsheet?
    Last edited by SamT; 01-13-2014 at 10:14 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •