Import csv

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
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


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 ?
 

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.
Hi
Try this (Not tested)
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_ = Application.Transpose(Evaluate("row(65:91)"))
    For i = 1 To UBound(Type_)
        Type_ = Chr(Type_)
    Next
    '**********************************
    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
 
Upvote 0
Hi
Try this (Not tested)
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_ = Application.Transpose(Evaluate("row(65:91)"))
    For i = 1 To UBound(Type_)
        Type_ = Chr(Type_)
    Next
    '**********************************
    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
Hi Mohadin, may I know the intention for this code ?
Type_ = Application.Transpose(Evaluate("row(65:91)"))
 
Upvote 0
Application.Transpose(Evaluate("row(65:91)"))
Crates an sequence Nub array(65,66,....91) related to characters (A,B,........Z)
sorry miss typing
should be
VBA Code:
'***************************
  Type_ = Application.Transpose(Evaluate("row(65:91)"))
    For i = 1 To UBound(Type_)
        Type_(i) = Chr(Type_(i))
    Next
'***************************
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,674
Members
448,977
Latest member
moonlight6

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