How to Open a CSV File With VBA

Written by l.p. klages
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Open a CSV File With VBA
(Siri Stafford/Digital Vision/Getty Images)

A Comma Separated Value (CSV) file is a plain text file that has a single record on each line. Each line is separated by a comma. A CSV file is sometimes called a comma delimited file. You can work with CSV files in Microsoft Excel and it's possible to get Visual Basic for Applications (VBA) to do the work for you, including opening the file at the push of a button.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Open the Visual Basic Editor (VBE) in Excel by pressing "Alt" and "F11" together.

  2. 2

    Click on "Insert," then click on "Module."

  3. 3

    Cut and paste the following code into the blank window:

    Sub opencsv() ' opencsv Macro Workbooks.Open Filename:= _ "C:\Desktop\myfile.csv" End Sub

  4. 4

    Change the file name in the above code to reflect the name of the file you want to open and the location it is in. For example, if you wanted to open a CSV file called Bills and it is located in the MyDocuments folder, then you would change the line of code that reads "C:\Desktop\myfile.csv" to "C:\MyDocuments\Bills.csv".

  5. 5

    Run the code by pressing "F5" from the VBE. Once you press F5, the file will open in Excel.

Tips and warnings

  • VBA only works in the worksheet that is open when you write the code. If you want it to work in multiple worksheets, you'll need to cut and paste the code into the VBE on any worksheet you want the code to be able to work in.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.