Import CVS File into existing table

Elifly

New Member
Joined
Oct 12, 2020
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hey everyone :)

I'm looking for a VBA for importing CVS Files to an existing workbook.

- It lets me pick multiple CSV file via an Open File dialogue
- The CVS File should be copied to the last written column (end of the existing table)
- The CVS File could be opened and closed again

I'm sure this has been written hundreds of times, but I can't seem to find a solution that fits my needs best

I have a code that works pretty well besides it copies my file to the top of my workbook

VBA Code:
Sub CSV_Import()
Dim dateien, i, lastrow
lastrow = 1
dateien = Application.GetOpenFilename _
("csv-Dateien (*.csv), *.csv", MultiSelect:=True)
If IsArray(dateien) Then
For i = 1 To UBound(dateien)
Workbooks.Open dateien(i), local:=True
With ThisWorkbook.Sheets(1)
ActiveSheet.UsedRange.Copy Destination:=.Range("A" & lastrow)
lastrow = .UsedRange.Rows.Count + 1
End With
ActiveWorkbook.Close False
Next i
End If
End Sub


Looking forward to your solutions :)

Thank you!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub CSV_Import()
   Dim dateien As Variant
   Dim i As Long
   
   dateien = Application.GetOpenFilename _
      ("csv-Dateien (*.csv), *.csv", MultiSelect:=True)
   If IsArray(dateien) Then
      For i = 1 To UBound(dateien)
         Workbooks.Open dateien(i), local:=True
         With ThisWorkbook.Sheets(1)
            ActiveSheet.UsedRange.Copy Destination:=.Cells(1, .Columns.Count).End(xlToLeft).Offset(, 1)
         End With
         ActiveWorkbook.Close False
      Next i
   End If
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
I'd like to do this also.
I have an existing xlsm file with a tab called "CSV".
I added a button and used the VBA code from above and it opens the csv file but it's not adding anything to the tab.
Do I need to add the existing xlsm file name or tab name in the code? I'd have thought the ActiveSheet and ThisWorkbook stuff would take care of that. ???

I'd also like to place the button on a different Tab than the CSV Tab and have it place the CSV file data into the CSV Tab and not the current active tab.

I have been just opening the csv in notepad and then doing a cut and paste into the CSV tab and then running the Excel Text to Columns function. That works but it would be nice to just drag the file in or click a button and open the csv file from the same folder.

It would also be cool if I could get the Text to Columns to stay active. Like any time you paste text in column A it just uses the same settings as before.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,537
Messages
6,120,096
Members
448,944
Latest member
SarahSomethingExcel100

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