How do you open a CSV File Using VBA?


You can use the Workbooks.Open method in VBA to open a CSV file from a specific file path.

Here is one common way to use this method in practice:

Sub OpenCSV()
Workbooks.Open "C:UsersbobDocumentsteam_info.csv"
End Sub

This particular macro opens the CSV file called team_info.csv located at a specific file path on my computer.

The following example shows how to use this syntax to open a CSV file in practice.

Example: How to Open a CSV File Using VBA

Suppose we have a CSV file called team_info.csv located at the following file path on my computer:

C:UsersbobDocumentsteam_info.csv

Suppose I would like to open this CSV file in Excel using VBA.

We can create the following macro to do so:

Sub OpenCSV()
Workbooks.Open "C:UsersbobDocumentsteam_info.csv"
End Sub

When we run this macro, the CSV file will automatically be opened:

The file contains information about various basketball teams.

Note that if we used the incorrect path for the CSV file then we would receive an error message from VBA.

For example, suppose I try to open a file called team_info2.csv, which does not exist:

Sub OpenCSV()
Workbooks.Open "C:UsersbobDocumentsteam_info2.csv"
End Sub

The error message lets us know that the CSV file could not be found.

Note: You can find the complete documentation for the Workbooks.Open method .

x