Creating pivot table from text file in VBA

justforgroups

New Member
Joined
Mar 10, 2010
Messages
26
Hi,
I have this VBAwhich works fine for creating a pivot table from a sheet:

Code:
    Set rngData = Worksheets(gs_sheet).Cells(gl_head_row, 1).Resize(gl_last_row, gl_last_col)

    ActiveSheet.PivotTableWizard SourceType:=xlExternal, SourceData:= _
                                rngData, TableDestination:=Worksheets(lv_pivot).Cells(1, 1), TableName:="PivotTable1"

However I need to change the source to an external file which is a tab-delimited text file.

Any help please? Thanks.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Do a 'record macro' while manually importing the data to excel (use 'get external data') then put this code in front of the code you're using to make the pivot table. I don't think you can make the pivot without importing the data first, but if this could cause issues, you could always hide the sheet the data is in.

HTH
 
Upvote 0
Hi,
I'm counting on being able to read in the data without it going into sheets :eek:
Anyway I now have this coded but the ODBC select data source dialog appears at PivotTableWizard. The lv_src value is valid. Any ideas? Thanks.

Code:
ConnectString = "ODBC; Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & lv_src & "; Extensions=asc,csv,tab,txt; UID=;;"
    QArray = Array(ConnectString, "SELECT * FROM InvSales.txt")
    ActiveSheet.PivotTableWizard SourceType:=xlExternal, SourceData:= _
                                QArray, TableDestination:=Worksheets(lv_pivot).Cells(1, 1), TableName:="PivotTable1"
 
Upvote 0
I'd imagine you could read the data without it going into a sheet, but if you can produce a pivot table from a query in this way then you know more about it than me. I'll keep tabs on this thread!
 
Upvote 0
My knowledge of VBA is very limited!
Do you really think it is not possible to create a pivot from an ODBC source without saving it to a sheet first?
All the ODBC examples I've read, take data from a database table as the source of the pivot.
 
Upvote 0

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top