Importing text file data

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,148
I get no errors when running this code, however the data in the text file does not appear in Sheet1, which is the only sheet in the workbook?
I also want to refresh the csv file in case the data has changed.

Code:
Sub Button1_Click()

    Dim ws As Worksheet, destRng As Range, fileName As String
    
    fileName = "C:\Users\RasulA\Documents\Personal\Book1.csv"
    
    Set destRng = Range("A1")
    Set ws = ActiveSheet
    
    With ActiveSheet.QueryTables.Add(Connection:="text; & C:\Users\RasulA\Documents\Personal\Book1.csv", Destination:=Application.Cells(1, 1))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 852
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        'Select your delimiter - selected below for Comma
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileTrailingMinusNumbers = True
        'This will refresh the query
        'ws.QueryTables.Refresh BackgroundQuery:=False
    End With
    
    ActiveWorkbook.Save

End Sub
 

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,816
Dim sFile As String

sFile = "C:\temp\Documents\names.csv"

Workbooks.OpenText Filename:=sFile, Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True, _
Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1)), _
TrailingMinusNumbers:=True
 
Last edited:

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,148
That opened the csv file but it didn't copy in the data from the csv to my Excel file that I am using.
 
Last edited:

Forum statistics

Threads
1,085,467
Messages
5,383,873
Members
401,859
Latest member
tomahawk61

Some videos you may like

This Week's Hot Topics

Top