Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am a programmer but recently new to both powershell and vba. I found this simple but effective command in powershell to read in a csv file and you can then immediately access "columns" by their heading in the csv file. in the example below there was a heading called "Location Description" and I could immediately access a column by that heading. Does VBA have something like for reading in a csv file and would that also be similar to reading in a worksheet if the macro was run from within the spreadsheet?

Snippet of Code

$InputData = import-csv $InputFile
foreach ($row in $InputData)
{
    $locdesc = $row."Location Description"
share|improve this question
    
I don't think there is a standard excel function for that (that I know of anyway). For this purpose I've rolled my own according to need. –  Pynner Dec 9 '12 at 20:08

1 Answer 1

There's nothing built in to Excel VBA that would allow you to address a column in a CSV file by it's header/field name.

There is, however, ADODB in the Microsoft ActiveX Data Objects Libraries. In there you can open a connection to a CSV by using a JET/CSV connection string to point to the file, define your delimiter, and tell it you have headers. Then you can pull open the content of the CSV into a recordset and refer to the fields by name within the recordset object. You can also throw whatever SQL you want at the CSV and do some of your data manipulation before getting the recordset back form JET.

That's a bit round-about, but if all you know is the name of a field in a CSV and not the ordinal, then it's probably your best bet.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.