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:\Users\bob\Documents\team_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:\Users\bob\Documents\team_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:\Users\bob\Documents\team_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:\Users\bob\Documents\team_info2.csv"
End Sub
When I run this macro, I will receive the following error message from VBA:
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 here.
Additional Resources
The following tutorials explain how to perform other common tasks in VBA:
How to Open a Text File Using VBA
How to Open a PDF Using VBA
How to Create Folders Using VBA