How to Use Excel VBA to Import a CSV File without Opening (3 Methods)

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.

3 Examples of Excel VBA to Import CSV File without Opening


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.

Apply VBA to Import Single CSV File without Opening in Excel

  • In the Microsoft Visual Basic window, click on the Insert tab.
  • Select the option Module.

Apply VBA to Import Single CSV File without Opening in Excel

  • 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.

Apply VBA to Import Single CSV File without Opening in Excel

  • 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.

Import Multiple CSV Files without Opening with VBA

  • In the Microsoft Visual Basic window, click on the Insert tab.
  • Select the option Module.

Import Multiple CSV Files without Opening with VBA

  • 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.

Insert VBA Command Button to Import CSV File without Opening

  • Go to Insert > ActiveX Controls > Command Button.

Insert VBA Command Button to Import CSV File without Opening

  • 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

Insert VBA Command Button to Import CSV File without Opening

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.

Insert VBA Command Button to Import CSV File without Opening

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.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mukesh Dipto
Mukesh Dipto

Mukesh Dipto is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including Rhino3D, Maxsurf C++, AutoCAD, HTML, CSS, JavaScript, and WordPress. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role, he... Read Full Bio

2 Comments
  1. 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

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo