Importing CSV into workbook as qurytable and deleting connection

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
185
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
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,066
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
185
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,066
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
185

ADVERTISEMENT

Thank you!

Jacek
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,066
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You don't. You just say thanks, as you did. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,456
Messages
5,596,234
Members
414,048
Latest member
wnied1

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
Top