CSV web import giving errors when using .TextFileCommaDelimiter = True

justgotloud

New Member
Joined
Nov 2, 2020
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Everything was sort of working great when automatically importing a csv file to excel...except that it was putting all data into column A. So I added the line .TextFileCommaDelimiter = True to my vba macro, re-ran and now I get the error 'application -defined or object -defined error' When I click debug, it takes me to the newly added line.

VBA Code:
Sub csvimp()

Dim lngConn As Long

url1 = Sheets("urls").Range("B2")

Sheets("csvimp").Cells.ClearContents

  Sheets("csvimp").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;" & url1 _
        , Destination:=Range("A1"))
        .Name = _
        "data"
        .FieldNames = True
        .RowNumbers = False
        .TextFileCommaDelimiter = True
        .FillAdjacentFormulas = False
        .PreserveFormatting = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

With ThisWorkbook
    For lngConn = .Connections.Count To 1 Step -1
        .Connections(lngConn).Delete
    Next lngConn
End With

End Sub

Any ideas what I am doing wrong?
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to MrExcel forums.

The code is using a web query (indicated by Connection:="URL;" and the 'Web' properties) and TextFileCommaDelimiter incompatible because it is used for importing text data, whether the data is in a local file or a file accessed via a URL.

Try this macro:

VBA Code:
Sub csvimp2()

    Dim url1 As String
   
    url1 = Worksheets("urls").Range("B2")

    With Worksheets("csvimp")
        .Select
        .Cells.ClearContents
        With .QueryTables.Add(Connection:="TEXT;" & url1, Destination:=.Range("A1"))
            .Name = "data"
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileCommaDelimiter = True
            .Refresh BackgroundQuery:=False
        End With
        .QueryTables(1).Delete
    End With
   
End Sub
If that doesn't work, or the data is wrong (it assumes row 1 of the file is column headers and you want to import from row 1) record a macro with you running the Text Import Wizard, specifying the URL as the file name. The generated code can then be edited to read the URL from a specific cell.
 
Upvote 0
thanks. this line is giving me issues
With .QueryTables.Add(Connection:="TEXT;" & url1, Destination:=.Range("A1"))
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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