Importing Multiple csv files

anuradhagrewal

New Member
Joined
Dec 3, 2020
Messages
37
Office Version
  1. 2010
Platform
  1. Windows
Hello
I have an excel sheet in which I regularly need to import data that is in *.csv format.
I would be very grateful if someone can provide me a VB code where I can import in a query all these csv files into the existing Excel file.
I have been able to find code where the codes *.csv files are imported but with the csv file name.
I want that the file name, this I want to be replaced by a number like 1,2,3 etc depending on the number of csv files imported.

Can anyone please help me.

Much Obliged

Regards

Anu
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,829
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,829
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Please Upload Minimum example of 2 CSV file to work on it. Upload at free hosting site e.g. www.dropbox.com or GoogleDrive or OneDrive & insert link here.
 

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,829
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Try this:
VBA Code:
Sub CombineCsvFiles()
    Dim xFilesToOpen As Variant, I As Integer, xWb As Workbook, xTempWb As Workbook
    Dim xDelimiter As String, fileName As String
    Dim xScreen As Boolean
    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    xDelimiter = "|"
    xFilesToOpen = Application.GetOpenFilename("Text Files (*.csv), *.csv", , , , True)
    If TypeName(xFilesToOpen) = "Boolean" Then
        MsgBox "No files were selected"
        GoTo ExitHandler
    End If
    I = 1
    fileName = xFilesToOpen(I)
    Debug.Print fileName
    Set xTempWb = Workbooks.Open(xFilesToOpen(I))
    xTempWb.Sheets(1).Copy
    Set xWb = Application.ActiveWorkbook
    xTempWb.Close False
    Do While I < UBound(xFilesToOpen)
        I = I + 1
        Set xTempWb = Workbooks.Open(xFilesToOpen(I))
        xTempWb.Sheets(1).Move , xWb.Sheets(xWb.Sheets.Count)
    Loop
ExitHandler:
    Application.ScreenUpdating = xScreen
    Set xWb = Nothing
    Set xTempWb = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub
 

anuradhagrewal

New Member
Joined
Dec 3, 2020
Messages
37
Office Version
  1. 2010
Platform
  1. Windows
Try this:
VBA Code:
Sub CombineCsvFiles()
    Dim xFilesToOpen As Variant, I As Integer, xWb As Workbook, xTempWb As Workbook
    Dim xDelimiter As String, fileName As String
    Dim xScreen As Boolean
    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    xDelimiter = "|"
    xFilesToOpen = Application.GetOpenFilename("Text Files (*.csv), *.csv", , , , True)
    If TypeName(xFilesToOpen) = "Boolean" Then
        MsgBox "No files were selected"
        GoTo ExitHandler
    End If
    I = 1
    fileName = xFilesToOpen(I)
    Debug.Print fileName
    Set xTempWb = Workbooks.Open(xFilesToOpen(I))
    xTempWb.Sheets(1).Copy
    Set xWb = Application.ActiveWorkbook
    xTempWb.Close False
    Do While I < UBound(xFilesToOpen)
        I = I + 1
        Set xTempWb = Workbooks.Open(xFilesToOpen(I))
        xTempWb.Sheets(1).Move , xWb.Sheets(xWb.Sheets.Count)
    Loop
ExitHandler:
    Application.ScreenUpdating = xScreen
    Set xWb = Nothing
    Set xTempWb = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub
Can you modify this code where
1)The csv files are added in the existing workbook and does not open another workbook
2)Instead of selecting the file name for eg : xxxx.csv it becomes sheet1.csv and so on

Thanks
 

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,829
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Try this. But you shouldn't have sheet1 ,2 , (until sheet count) at your workbook
if you want add another Name as S1, S2, S3 Change ActiveSheet.Name = "Sheet" & I to ActiveSheet.Name = "S" & I
VBA Code:
Sub CombineCsvFiles()
    Dim xFilesToOpen As Variant, I As Integer, xWb As Workbook, xTempWb As Workbook
    Dim xDelimiter As String, fileName As String, wbNew As Workbook, xScreen As Boolean
    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    xDelimiter = "|"
    xFilesToOpen = Application.GetOpenFilename("Text Files (*.csv), *.csv", , , , True)
    If TypeName(xFilesToOpen) = "Boolean" Then
        MsgBox "No files were selected"
        GoTo ExitHandler
    End If
    I = 0
    Set xWb = Application.ActiveWorkbook
    Do While I < UBound(xFilesToOpen)
        I = I + 1
        Set xTempWb = Workbooks.Open(xFilesToOpen(I))
        xTempWb.Sheets(1).Move , xWb.Sheets(xWb.Sheets.Count)
        ActiveSheet.Name = "Sheet" & I
    Loop
ExitHandler:
    Application.ScreenUpdating = xScreen
    Set xWb = Nothing
    Set xTempWb = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
End Sub
 

anuradhagrewal

New Member
Joined
Dec 3, 2020
Messages
37
Office Version
  1. 2010
Platform
  1. Windows
Hi
It does what is expected but stops after 3 sheets. Can u tell me what can be added so that the number of sheets that can be added is infinite.

Much obliged

Thanks

Anu
 

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,829
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Why stopping after 3 sheets , it should run until count of your selected CSV files. (if you select 5 files go to sheet5 and ...)
 

Watch MrExcel Video

Forum statistics

Threads
1,130,014
Messages
5,639,555
Members
417,098
Latest member
steverob

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
Top