Macro to Import Multiple CSV files into specific Excel tabs

Mencae

New Member
Joined
Jul 26, 2017
Messages
1
Hello,

I have a project that I'm hoping to get help with.

I need to import multiple CSV files into specific Macro Enabled Workbook that already has uniquely named Worksheet tabs. I have code to do this for 1 CSV file into 1 worksheet tab. However I have 7 CSV files (always the same CSV file name) that need to go into 7 specific tabs.

Here are some details to help:
  1. A job on an External Server runs a job creating the 7 CSV files.
  2. The files are saved in (let's say) L:\CSV_OutPut
  3. The file names are 556.csv, 557.csv, 556.csv, 557.csv, 558.csv, 559.csv, 560.csv, 561.csv, 563.csv
The Excel workbook is: L:\Queues\Queue_Report.xlsm
  1. The workbook has tabs called: Data_556, Data_557, Data_558, Data_559, Data_560, Data_561, Data_563
  2. So the 556.csv file needs to be imported into tab Data_556 (and the same follows for the Other CSV's into their similarly named TAB)
  3. Each CSV file will have its data imported into its corresponding Tab

The code I have tested to do this so far (that I borrowed from this site) allows me to run the macro, manually find the 556.csv file, and it will import the CSV data into the Data_556 tab perfectly.

Since the CSV file names, as well as the Worksheet tab names will never change, I'd like to hard-code this into the macro. Can this be done for all the files I've listed in the the way I've described above?

Here is the code I'm working with right now:

Sub load_csv()
Dim fStr As String

With Application.FileDialog(msoFileDialogFilePicker)
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Cancel Selected"
Exit Sub
End If
'fStr is the file path and name of the file you selected.
fStr = .SelectedItems(1)
End With

With ThisWorkbook.Sheets("Data_556").QueryTables.Add(Connection:= _
"TEXT;" & fStr, Destination:=Range("$A$1"))
.Name = "CAPTURE"
.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 = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

End With
End Sub

Any suggestions?
Thanks in advance to all you experts
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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