Importing Multiple csv files

anuradhagrewal

Board Regular
Joined
Dec 3, 2020
Messages
87
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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
Why stopping after 3 sheets , it should run until count of your selected CSV files. (if you select 5 files go to sheet5 and ...)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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