I am looking for a VBA user-defined function that will read in the contents of a .csv file and return the results as an array to populate the cells of a worksheet, i.e something like this:
=CSV_Read("path/to/file.csv")
I have found this but looks proprietary/shareware. Does anyone know of any alternatives? Or what the best approach to rolling my own in VBA would be?
In your VBA code, add reference to “Microsoft Scripting Runtime”.
Add a module, paste the following code.
Sample CSV file data
Looking at the data, it has 7 columns and 4 rows.
Now, goto cell A1 and select the range from cell A1 to G4.
Type in the formula (
=CSV_Read("the full path to your csv file"), in cell A1 (keeping the selection as it is).Important:
Press CTRL + SHIFT + ENTER, once you have finished typing in the formula. This is array formula.