Results 1 to 11 of 11

Reading CSV into listobject query table

Threaded View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Reading CSV into listobject query table

    Hi Guys,

    i want to do 2 things:
    1) Import CSV table in format like:

    "Col1","Col2","Col3"
    "1","2",""
    "","3","4"

    into Excel activesheet.

    2) If listobject not exists just create new listobject.

    3) If listobject exists just refresh whole table to get the same result like in CSV (for example data added, column deleted and so on).

    I tried with ADODB object :

    Public Function GetCSVData(ByVal strFile As String) As Object
        Dim lngSplit As Long, strTable As String, strPath As String
        Dim objRS As Object
        Dim strConnection As String, strSQL As String
        
        lngSplit = InStrRev(strFile, "\")
        
        strTable = Mid$(strFile, lngSplit + 1)
        strPath = Left$(strFile, lngSplit - 1)
        
        strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                        "Data Source=" & strPath & ";" & _
                        "Extended Properties=Text;"
                        
        strSQL = "SELECT * FROM " & strTable & ";"
    
    
        Set objRS = CreateObject("ADODB.Recordset")
        varCsvData = objRS.Open(strSQL, strConnection, 1, 3, 1)
    End Function
    
    Sub CSVDownload()
    
        Dim varCsvData As Variant
        Dim path As Variant
        
        path = "D:\Pulpit\Newest Pull request\ImportingBundles\CSVTest.csv"
    
        varCsvData = GetCSVData(path)
        
    End Sub
    but it failed for me - the varCsvData variant array is empty.

    and i tried this code:

    Sub main() ' {
    
        Dim connectionString As String
        Dim path As Variant
        
        path = "D:\Pulpit\Newest Pull request\ImportingBundles\CSVTest.csv"
    
        connectionString = _
           "OLEDB;" & _
           "provider=Microsoft.ACE.OLEDB.12.0;" & _
           "data source=" & path & ";" & _
           "extended Properties=text"
    
        Dim destTable As ListObject
    
        Set destTable = ActiveSheet.ListObjects.Add( _
           SourceType:=xlSrcExternal, _
           Source:=connectionString, _
           Destination:=Cells(2, 2))
    
        With destTable.QueryTable
    
            .CommandType = xlCmdSql
            .CommandText = Array("select * from [CSVTest.csv]")
            .BackgroundQuery = True
    
            .Refresh BackgroundQuery:=False
    
        End With
    
    End Sub
    but i am getting:

    Screenshot_106.png

    Maybe i could use somehow power query formula through VBA to change it dynamically and use query?
    I do not know,
    please help,
    Best Wishes,
    Jacek
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Delete listobject with query table based on connection name
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-03-2024, 07:12 PM
  2. [SOLVED] Create listobject from external query
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-26-2018, 03:18 AM
  3. [SOLVED] Copy filtered column of ListObject to another ListObject
    By snapfade in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-15-2016, 04:00 PM
  4. ListObject SQL query not setting NumberFormat
    By cheetah05 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-22-2014, 06:08 AM
  5. [SOLVED] Converting Query Table to ListObject
    By x10sion in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-04-2013, 06:56 AM
  6. Remove Filter From ListObject Table
    By efernandes67 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-06-2011, 04:47 AM
  7. Table ListObject Filter
    By efernandes67 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2011, 07:40 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1