VBA Importing Text File To Excel

Hobolord

Board Regular
Joined
Sep 9, 2015
Messages
64
Hello,

I am using Excel 2013, Windows 7, on a PC.

I am attempting to have my code import a text file to excel. This should be very simple. I used the recorder, but the code is bugging on the
Code:
.Commandtype = 0
line, and if I remove that, it doesn't import any data either.

My code matches all code I'm able to find via Google. Someone please help :(.

Code:

Code:
Dim filepath    As String

filepath = InputBox("Please enter the filepath for the audit report .txt file" & vbNewLine & vbNewLine & "For example: 'C:\Users\cbs1715\Desktop\Audit Report 09282016.txt'", "Enter Filepath")

With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & filepath, Destination:=Range _
        ("$A$1"))
        .Commandtype = 0
        .name = "Audit Report"
        .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 = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileFixedColumnWidths = Array(17, 13, 17, 16, 27, 5, 4)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hello,

I am using Excel 2013, Windows 7, on a PC.

I am attempting to have my code import a text file to excel. This should be very simple. I used the recorder, but the code is bugging on the
Code:
.Commandtype = 0
line, and if I remove that, it doesn't import any data either.

My code matches all code I'm able to find via Google. Someone please help :(.

Code:

Code:
Dim filepath    As String

filepath = InputBox("Please enter the filepath for the audit report .txt file" & vbNewLine & vbNewLine & "For example: 'C:\Users\cbs1715\Desktop\Audit Report 09282016.txt'", "Enter Filepath")

With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & filepath, Destination:=Range _
        ("$A$1"))
        .Commandtype = 0
        .name = "Audit Report"
        .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 = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileFixedColumnWidths = Array(17, 13, 17, 16, 27, 5, 4)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
try this on a sample workbook:
Code:
Sub hobolord()
Dim filepath    As String

filepath = InputBox("Please enter the filepath for the audit report .txt file" & vbNewLine & _
                    vbNewLine & _
                    "For example: " & Environ("UserProfile") & "\Desktop\Audit Report 09282016.txt'", "Enter Filepath")

With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & filepath, Destination:=Range _
        ("$A$1"))
        '.CommandType = 0
        .Name = "Audit Report"
        .FieldNames = True
        .PreserveFormatting = True
        .RefreshStyle = xlInsertDeleteCells
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = Null
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileFixedColumnWidths = Array(17, 13, 17, 16, 27, 5, 4)
        .TextFileTrailingMinusNumbers = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,616
Members
449,238
Latest member
wcbyers

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