I have two csv files need to import to Excel. I would like to import both csv files to current workbook as below
(a) create Sheet_Company_A and import 1st csv files
(b) create Sheet_Company_B and import 2nd csv files
How to modify above code so that it can execute dynamically for n csv files ?
(a) create Sheet_Company_A and import 1st csv files
(b) create Sheet_Company_B and import 2nd csv files
VBA Code:
Sub Import()
Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim Filename As Variant, Path As Variant, FileToRead As Variant
Dim Brand() As String, TextString As String
Dim i As Integer
Type = Split("A,B", ",")
Path = "C:\Users\My\Downloads\"
Filename = Dir(Path & "Report*")
For i = LBound(Type()) To UBound(Type())
Do While Filename <> ""
' Destination & Source Path
Filename = Dir()
Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Sheet_Company_" & Type(i)
Set FileToRead = FSO.OpenTextFile(Path & Filename, ForReading) 'add here the path of your text file
TextString = FileToRead.ReadAll
ThisWorkbook.Sheets("Sheet_Company_& Type(i)").Range("A1").Value = TextString
FileToRead.Close
Loop
Next i
End Sub
How to modify above code so that it can execute dynamically for n csv files ?