Home
FREE DOWNLOADS
VBA Tutorials
VBA Code Samples
VBA Code - Excel
VBA for Beginners
Save Time on PC
SaveTimeWebsite
Save Time News
Save Time Blog
SaveTime SiteMap
Customer Service
Gibberish

Subscribe To
This Site

XML RSS
Add to Google
Add to My Yahoo!
Add to My MSN
Add to Newsgator
Subscribe with Bloglines

VBA Excel Importing File

The following code can be used to import a comma separated text file...

Sub vba_excel_importing_file()
    Dim strFileName As String
    
    strFileName = InputBox("Enter the full path to the comma " & _
               "separated file to import")
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & _
               strFileName, Destination:=Range("A1"))
        .Name = "vba excel importing file"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Values for the line...

.TextFileColumnDataTypes = Array(1, 1, 1, 1)

can be...

1 = xlGeneralFormat General 
2 = xlTextFormat    Text 
3 = xlMDYFormat     MDY date
4 = xlDMYFormat     DMY date 
9 = xlSkipColumn    Skip column 

You can use either the number or the constant.
For other date formats, search for "XlColumnDataType" in the Object Browser. View menu > Object Browser.

Google

Return from VBA Excel Importing File to VBA Code Samples

Return to our Homepage



footer for vba excel importing file page