Import CSV files to existing worksheets with same names

wikus

Active Member
Joined
May 2, 2010
Messages
297
Office Version
  1. 365
How can I import all CSV files in a folder to existing worksheets with the same names as files (Without .csv)?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi,

try this code.
I have used this to import files and add a sheet with the same name as the file but without extn or imported into existing sheet.
There's no error checking and it overwrites content but it might get you started.

Code:
Sub Import_csv()
'Display Open Dialog to select csv location.
  filenames = Application.GetOpenFilename("Excel Files (*.csv*)," & _
    "*.csv*", 1, "Select CSV files", "Open", False)
   
'If the user cancels file selection then exit
     If TypeName(filenames) = "Boolean" Then
        Exit Sub
     End If
                             
'Set csv as SampleFile
       FileCSV = Dir("*.csv")
               
'Cycle through the directory
 Do While FileCSV <> ""
 
'Remove extension for sheetname, add sheet and rename
 CSVNoext = (Left(FileCSV, Len(FileCSV) - 4))
 
'To import csvs into active WB adding new sheet same name as file without extn use  '1'
'To import into existing sheets with same name no extn use '2'
 
    'ThisWorkbook.Sheets.Add - 1
    'ActiveSheet.Name = CSVNoext - 1
    
    Workbooks.Open (FileCSV)
    ActiveSheet.UsedRange.Copy
    Application.DisplayAlerts = False
    ActiveWorkbook.Close False
    Application.DisplayAlerts = True
    Worksheets(CSVNoext).Activate '- 2
    ActiveSheet.PasteSpecial
    
FileCSV = Dir
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,174
Messages
6,053,932
Members
444,694
Latest member
JacquiDaly

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