VBA - CSV import to Excel

saint_2008

Board Regular
Joined
Sep 6, 2007
Messages
103
I've taken a code I've found by a google search and tried to implement it for what I need but I'm getting an error Subscript out of range error can anyone shed some light on why i might be getting this error?

Sub ImportSamplesReceivedFile()


Dim FPath As Variant

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

ChDrive "I"
ChDir "I:\"
FPath = Application.GetOpenFilename(FileFilter:="CSV Excel Files, *.CSV", Title:="Select Today's Export File")
If FPath = False Then
MsgBox UCase(Environ("UserName")) & " did not select a reference file." & Chr(13) & "Database Update has been aborted!", vbCritical, "Error Handler"
Exit Sub
Else
'Continue Procedure
End If

Sheets("VMDump").Activate

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & FPath, Destination:=Range("$A$2"))
.CommandType = 0
.Name = "VMDump"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

End Sub

If you need any further information please let me known.

I need the data to skip top (header) row on both sheet.

There random amount of data the source sheet can be anything from 10-100 rows.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Have you stepped through the code to see where it highlights the issue. In your code use F8 and it will step through and highlight the issue and then you can post back and provide more information.
 
Upvote 0
Can I make a suggestion record a macro to open a CSV file to give you the basic code then add that into your thread and hopefully we can spot what's actually happening.
 
Upvote 0
Pleased to read you have a working solution well done, and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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