To illustrate the methods, we will use the following CSV file. The file consists of three columns: Salesman, Product, and Sales. We will import the data from the CSV file without opening it.
Method 1 – Applying VBA to Import a Single CSV File without Opening
STEPS:
- Go to the Developer tab > Visual Basic or press Alt + F11 to open the Microsoft Visual Basic window.
- In the Microsoft Visual Basic window, click on the Insert tab.
- Select the option Module.
- A blank VBA code window will appear. Enter the following code in that code window:
Sub csv_Import()
Dim wsheet As Worksheet, file_mrf As String
Set wsheet = ActiveWorkbook.Sheets("Single")
file_mrf = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Provide Text or CSV File:")
With wsheet.QueryTables.Add(Connection:="TEXT;" & file_mrf, Destination:=wsheet.Range("B2"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
End Sub
- Click the Run or press the F5 key to run the code.
- In the Macro The Application.GetOpenFilename statement accesses the device directory, where CSV files can be selected.
- Select the CSV file that we want to import and click OK.
- We get the CSV file loaded in our existing Excel worksheet.
Method 2 – Importing Multiple CSV Files without Opening Using VBA
STEPS:
- Press Alt + F11 or go to the Developer tab > Visual Basic to open the Microsoft Visual Basic window.
- In the Microsoft Visual Basic window, click on the Insert tab.
- Select the option Module.
- A blank VBA code window appears. Enter the following code in the code window:
Sub import_csv()
Dim file As FileDialog
Dim filePath As String
Dim csv As String
Dim wsheet As String
Application.DisplayAlerts = False
Application.StatusBar = True
wsheet = ActiveWorkbook.Name
Set file = Application.FileDialog(msoFileDialogFolderPicker)
file.Title = "Folder Selection:"
If file.Show = -1 Then
filePath = file.SelectedItems(1)
Else
Exit Sub
End If
If Right(filePath, 1) <> "\" Then filePath = filePath + "\"
csv = Dir(filePath & "*.csv")
Do While csv <> ""
Application.StatusBar = "Converting: " & csv
Workbooks.Open Filename:=filePath & csv
ActiveWorkbook.SaveAs Replace(filePath & csv, ".csv", ".xlsx", vbTextCompare), xlWorkbookDefault
ActiveWorkbook.Close
Windows(wsheet).Activate
csv = Dir
Loop
Application.StatusBar = False
Application.DisplayAlerts = True
End Sub
- Press F5 or click the Run button to run the code.
- The above action asks to select a folder.
- Select a folder from our directory that contains multiple CSV files.
- Click OK.
- The CSV files from that folder have been imported into Excel files without being opened.
Read More: Excel VBA: Merge Multiple CSV Files into One Workbook
Method 3 – Inserting a VBA Command Button to Import a CSV File without Opening
STEPS:
- Go to the Developer tab and select the option Design Mode.
- Go to Insert > ActiveX Controls > Command Button.
- On the active sheet, press the command button, as shown below.
- To open the code window, double-click the command button.
- Enter the following code in the code window:
Private Sub CommandButton1_Click()
Dim z As FileDialog
Dim zPath As String
Dim csv As String
Dim wsheet As String
Application.DisplayAlerts = False
Application.StatusBar = True
wsheet = ActiveWorkbook.Name
Set z = Application.FileDialog(msoFileDialogFolderPicker)
z.Title = "Folder Selection:"
If z.Show = -1 Then
zPath = z.SelectedItems(1)
Else
Exit Sub
End If
If Right(zPath, 1) <> "\" Then zPath = zPath + "\"
csv = Dir(zPath & "*.csv")
Do While csv <> ""
Application.StatusBar = "Converting: " & csv
Workbooks.Open Filename:=zPath & csv
ActiveWorkbook.SaveAs Replace(zPath & csv, ".csv", ".xlsx", vbTextCompare), _
xlWorkbookDefault
ActiveWorkbook.Close
Windows(wsheet).Activate
csv = Dir
Loop
Application.StatusBar = False
Application.DisplayAlerts = True
End Sub
NOTE: You can also paste this code into the Module box. In that instance, press Alt + F11 to bring up the Visual Basic window, then choose Insert. There will be a drop-down menu. From there, select Module and paste the code into the Module window. Example 2 illustrates this procedure.
- Press Ctrl + S to save the code. Close the code window.
- Unselect the Design Module in the Developer tab.
- Click on the Command Button.
- A new dialogue box will open.
- Select the folder that contains the CSV file and press OK.
The data of the CSV file is imported into an Excel file.
Read More: Excel VBA to Convert CSV File to XLSX
Download the Practice Workbook
You can download the practice workbook from here.
Dear Mukesh,
This article gives me a starting point. What I need to do is:
1. Open a CSV file created by a Leica LS10 digital level.
2. Extract the data I need.
3. Export the data to an Excel template for record keeping.
Regards,
Mark
Hello Mark,
Thank you for your comment. To achieve your goal, you can follow these steps:
Open the CSV: Use VBA’s Open statement to access the CSV file.
Extract Data: Use Input or Line Input to read each line, then parse it using Split().
Export to Excel: Write the extracted data into a predefined Excel template using Range.Value.
If you’d like a detailed guide or VBA code, feel free to ask, and I’ll be happy to help!
Best regards,
ExcelDemy