Pivot from CSV

Vishesh's picture

Attached is small utility to create a Pivot table from a CSV file. There is a button provided on the click of which you are asked to choose the csv file and then the target cell/range where you want to place your pivot.

This is the macro code working behind the scene...


Sub CreatePivotTableFromCSV()

 

    Dim strFileName     As String

    Dim strFilePath     As String

    Dim rngTarget       As Range

    

    strFileName = Application.GetOpenFilename("CSV File, *.csv", 1, "Select CSV File", , False)

    

    If Not strFileName = "False" Then

        On Error Resume Next

            Set rngTarget = Application.InputBox(prompt:="Target Range", Type:=8)

        On Error GoTo 0

        If Not rngTarget Is Nothing Then

            Call TestCSV(strFileName, rngTarget)

        End If

    End If

    

    Set rngTarget = Nothing

    

End Sub

 

Sub TestCSV(ByVal strFullFilePath As String, rngPvtTarget As Range)

 

    Dim conADO        As Object

    Dim rstADO        As Object

    Dim pvtCache      As PivotCache

    Dim pvtTable      As PivotTable

    Dim strSQL        As String

    Dim strFileName   As String

    Dim strFilePath   As String

    

    strFilePath = Left(strFullFilePath, InStrRev(strFullFilePath, "\"))

    strFileName = Replace(strFullFilePath, strFilePath, "")

    

    Set conADO = CreateObject("ADODB.Connection")

    conADO.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _

                    & strFilePath _

                    & ";Extensions=asc,csv,tab,txt;Persist Security Info=False"

    

    strSQL = "SELECT * FROM " & strFileName

    

    Set rstADO = CreateObject("ADODB.Connection")

    Set rstADO = conADO.Execute(strSQL)

    

    'Excel 2003

    If Application.Version < 12 Then

        Set pvtCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)

    Else

    '>= Excel 2007 Use

        Set pvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal)

    End If

    

    Set pvtCache.Recordset = rstADO

    Set pvtTable = pvtCache.CreatePivotTable(TableDestination:=rngPvtTarget)

    

    conADO.Close

    

    Set rstADO = Nothing

    Set conADO = Nothing

    Set pvtCache = Nothing

    Set pvtTable = Nothing

    

End Sub

 
AttachmentSize
PivotFromCSV.xls37.5 KB

Pivot from CSV column limit.

Hi Vishesh,
Thanks for the macro. One question regarding the column limits. When i use this macro to create pivots from a CSV , it only creates pivots for the first 255 columns. Is there a way to get around this and pivot the entire file?. (In some cases files have 1250 columns).
Regards,
Nazmi

Vishesh's picture

Are you able to create pivot

Are you able to create pivot with 1250 columns manuallly ?

Yes, it takes a lot memory .

Yes, indeed. I am able to create such big pivots with 8gb ram computers. This macro you provided works perfect for files that have less then 255 columns and rows exceeding the excel limits. I wonder if it is limited on the sql server side.
Best,
Nazmi

Vishesh's picture

Yes, databases generally

Yes, databases generally support maximum of 255 columns.

Creating a excel pivot without opening it which has more t

Thanks Vishesh,
So all my dreams of creating such big excel pivots without opening are fading. Unless you know an another method that could help.
Best,
Nazmi

Filename issue

Hi Vinesh,

Lovely code, as always. I think changing the following

strSQL = "SELECT * FROM & strFileName"
to
strSQL = "SELECT * FROM [" & strFileName & "]"

will mean the code can work with filenames that have spaces.

Cheers...

Nick's picture

thanks Shep

thanks Shep