Hey, everyone. I've written a Macro which allows me to import multiple .csv files as separate sheets. But I'm having trouble finding a way to rename each new sheet to match the name of the file being imported.
For example, if I want to import the contents of files called "File1.csv" and "File2.csv" to new worksheets, I'd like to name those worksheets "File1" and "File2" respectively.
Any advice?
I'll attach my very amateur coding.
Thanks in advance!
>>>
Sub ImportFilesToSheets()
Dim fpath As String
Dim fname As Variant
Dim X As Long
fpath = "D:\Documents and Settings\"
fname = Application.GetOpenFilename _
(filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=True)
For X = LBound(fname) To UBound(fname)
Sheets("Main Sheet").Select
Sheets.Add
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
& fname(X), Destination:=Range("A1"))
.Name = "fname"
.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)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next X
End Sub
>>>
Bookmarks