Use Excel VBA to Look Up Data in a Closed CSV File
Question details
Look up a key in a CSV file with VBA and return the matching value from its eighth column.
- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- An Excel macro needs to find a value in a CSV file that is not currently open.
- Observed behavior
- A recorded VLOOKUP formula may prompt for the CSV file and return a blank result.
Confirm the CSV path, the column containing the lookup key, and the column to return. The example searches column A and returns column H; close the CSV in Excel before running it.
Open the CSV Read-Only and Search a VBA Array
Use this approach for a CSV small enough to fit comfortably in memory. It avoids relying on a worksheet formula to query the closed file.
The macro temporarily opens the CSV in Excel, reads columns A through H into an array, and closes the CSV without saving. It returns the first exact match, or #N/A if no match is found.
Excel may interpret CSV values as numbers or dates when opening the file. If keys contain leading zeros or require a specific text format, import those columns as text instead.
In Excel, open the workbook that will receive the result. Press Alt+F11, then choose Insert > Module in the VBA editor.
Paste this code into the module: Function LookupCsvColumn8(csvPath As String, lookupKey As String) As Variant Dim csvBook As Workbook Dim csvSheet As Worksheet Dim data As Variant Dim lastRow As Long Dim i As Long On Error GoTo Failed LookupCsvColumn8 = CVErr(xlErrNA) Set csvBook = Workbooks.Open(Filename:=csvPath, UpdateLinks:=0, ReadOnly:=True, Local:=True) Set csvSheet = csvBook.Worksheets(1) lastRow = csvSheet.Cells(csvSheet.Rows.Count, 1).End(xlUp).Row data = csvSheet.Range("A1:H" & lastRow).Value2 For i = 1 To UBound(data, 1) If Not IsError(data(i, 1)) Then If StrComp(CStr(data(i, 1)), lookupKey, vbBinaryCompare) = 0 Then LookupCsvColumn8 = data(i, 8) Exit For End If End If Next i CleanUp: If Not csvBook Is Nothing Then csvBook.Close SaveChanges:=False Exit Function Failed: LookupCsvColumn8 = CVErr(xlErrValue) Resume CleanUp End Function Sub RunCsvLookup() Dim csvPath As String csvPath = "C:\Data\source.csv" ThisWorkbook.Worksheets("Sheet1").Range("C2").Value = _ LookupCsvColumn8(csvPath, CStr(ThisWorkbook.Worksheets("Sheet1").Range("B2").Value2)) End Sub
In RunCsvLookup, replace the example CSV path and Sheet1 with your own values. Change B2 if the lookup key is elsewhere, and C2 if the result should appear in a different cell.
Place the cursor inside RunCsvLookup and press F5. Check the result cell: #N/A means no exact match was found; #VALUE! indicates an error opening or reading the CSV.
Work With CSV and Excel Files in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing CSV and Excel files. Its format support can make migration easier, though it does not fix Excel VBA behavior and macro compatibility may vary.

Frequently Asked Questions
Does this VBA lookup keep the CSV closed?
No. Workbooks.Open temporarily opens it read-only, and the code closes it without saving after the search. This avoids leaving a worksheet formula linked to the closed CSV.
How do I return a different CSV column?
Change the range in csvSheet.Range to include the desired column, then replace data(i, 8) with that column's position within the range. For example, data(i, 3) returns column C when the range starts at A.
Why does the result show #N/A?
The code returns #N/A when it finds no exact, case-sensitive match in column A. Check the key in B2 and the CSV for differences in capitalization, spaces, or leading zeros.
Can I use this array method for a very large CSV?
It may use too much memory because it loads columns A through H at once. For large files, process batches or use a CSV-aware reader that handles quoted fields correctly.




