troubleshooting Question

Stop connection from csv import

Avatar of Euro5
Euro5Flag for United States of America asked on
Microsoft ExcelMicrosoft Office
5 Comments1 Solution6 ViewsLast Modified:
I am using this to import a csv to an excel spreadsheet.
However, it keeps the connection and when I make changes and change, I lose everything each time I open the spreadsheet because it connects to that data!

Can anyone help!?
Public Sub ImportDataur()
    Dim Wb As Excel.Workbook
    Set Wb = ThisWorkbook
    Dim FName As String
    Dim ws As Excel.Worksheet
    Set ws = Wb.Worksheets("ur data")
        '// retrieve the last row
    Dim rng As Excel.Range
    Set rng = ws.Range("A1").EntireColumn
    Set rng = rng.Cells(rng.Cells.Count).End(xlUp).Offset(rowOffset:=1)

    Dim path As String
    path = Application.ActiveWorkbook.path & "\ur.csv"
    If (path <> vbNullString) Then
        ImportCSV path, ws, rng, "CSVData"
    End If
End Sub

    '// inspired from:
Private Sub ImportCSV(ByVal path As String, ByRef ws As Worksheet, ByRef rng As Range, ByVal TableName As String) ' {
    With ws.QueryTables.Add(Connection:="TEXT;" & path, Destination:=rng)
        .Name = TableName
        .FieldNames = True
        .RowNumbers = False
        .TextFilePlatform = 437
        .TextFileStartRow = 2
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .PreserveFormatting = True
        .TextFileCommaDelimiter = True
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .SaveData = False
        .TextFilePromptOnRefresh = False
        .TextFileTrailingMinusNumbers = True
        '// .textFileTabDelimiter         = false
        .TextFileSemicolonDelimiter = True
        '//.textFileSpaceDelimiter       = false
        '// .textFileColumnDataTypes      = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        '//.refreshStyle                 = xlInsertDeleteCells
        '// .adjustColumnWidth            = True
        '// .refreshPeriod                = 0
        .Refresh BackgroundQuery:=False
    End With
End Sub

Open in new window


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros