Importing CSV into workbook as qurytable and deleting connection

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
213
Hi Guys,

I am using this function to import CVS into workbook:

VBA Code:
Function ImportDataCSVQuery(wsh, workbookToImportPath, ThisWb)

    With wsh
'   Downloads .csv file with header row
        With .QueryTables.Add(Connection:="TEXT;" & workbookToImportPath, Destination:=wsh.Range("A1"))
        .Name = "Connection"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .TextFileDecimalSeparator = "."
        .TextFileThousandsSeparator = ","
        .Refresh BackgroundQuery:=False
        End With
    End With
    
        ThisWb.Connections(ThisWb.Connections.Count).delete
    
End Function

And after import i want to create listobject from imported data, i am doing:

VBA Code:
  With wshTemp
    
        lstrow = .Cells(ActiveSheet.Rows.Count, "A").End(xlUp).row
        lstcolumn = .Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
        .ListObjects.Add(xlSrcRange, .Range(.Cells(1, 1), .Cells(lstrow, lstcolumn)), , xlYes).Name = "TEST"
        
    End With

and getting error:

Screenshot_18.png


so it means that this code is not working:

VBA Code:
ThisWb.Connections(ThisWb.Connections.Count).delete

Why this is not working? Can anybody explain?

Crossposted from here:
Importing CSV into workbook and creating listobject

Jacek
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
That code line is working, it's just not doing what you think it should. You still have a querytable, you just removed its connection. Add:

Code:
    wsh.QueryTables("Connection").Delete

to your function and you should be fine.
 
Upvote 0
Thank you Rory!

I am writing on 2 different formulas but only you are helping me :) , thank you so much!

Hmm what if i have already Connections in workbook?
This will be added as Connection1 I assume and your function will not work?

ThisWb.Connections.Count this is also counting connection but why not working in my formula above? - ThisWb.Connections(ThisWb.Connections.Count).delete

Jacek
 
Upvote 0
It is working - it deleted the connection but it didn’t delete the query table. That’s what the extra line does and it uses the name that you gave it.
 
Upvote 0
You don't. You just say thanks, as you did. :)
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

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