How to import .any. csv file through program VBA

Jacko1996

New Member
Joined
May 29, 2015
Messages
21
So Im not sure what to do with this. Im trying to make VBA to import any .csv files into excel. PLEASE HELP

Someone told me to use this

' Open GetOpenFilename with the file filters.
Fname = Application.GetOpenFilename( _
FileFilter:="csv Files (*.csv), *.csv", _
Title:="Select a file or files", _
MultiSelect:=False)

and put that in a loop statement around your import csv macro.
So just change the hard-coded csv part in your recorded macro for importing CSV's to fname and job done

This is my hard-coded macro for a random .csv

Sub Macro1()
'
' Macro1 Macro
'


'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\CSV Files\IDCJAC0009_040157_1800_Data.csv", Destination:=Range( _
"$A$1"))
.CommandType = 0
.Name = "IDCJAC0009_040157_1800_Data"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
The following loads a chosen file into the current Activesheet

If you want to do the multiple times how will each file be handled,, a new sheet for each file ( or work book )

also is it every csv in a folder/Directory or is it easier to look for the file each time

Code:
Sub Macro1()
'
' Macro1 Macro
'


' Open GetOpenFilename with the file filters.
fname = Application.GetOpenFilename(FileFilter:="csv Files (*.csv), *.csv", Title:="Select a file or files", MultiSelect:=False)

'and put that in a loop statement around your import csv macro.
'So just change the hard-coded csv part in your recorded macro for importing CSV's to fname and job done

'This is my hard-coded macro for a random .csv

FileNameAlone = Right(Left(fname, Len(fname) - 4), (Len(fname) - 4) - (InStrRev(fname, "\", -1)))

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

End Sub
 
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