Excel VBA: Check If a String Is in a Column
In this article
To check whether a string appears in a column with Excel VBA, use the Find method on a Range or loop through the cells with InStr. The Find approach is fastest for a quick yes/no answer; a loop lets you test every cell and return the row number or the matching value. Either way, you need the Developer tab enabled and a basic understanding of how to insert a module. The steps below use the Visual Basic Editor (VBE) that ships with every desktop version of Excel; labels can differ slightly by version, device or region, and VBA is not available in Excel for the web or in the mobile apps.
What You'll Need
- Excel for Windows or Mac (desktop version). The Developer tab is hidden by default; enable it via File > Options > Customize Ribbon (Windows) or Excel > Preferences > Ribbon & Toolbar (Mac), then tick Developer.
- A workbook with data in a column, and permission to save it as a macro-enabled file (.xlsm).
- Basic familiarity with the Visual Basic Editor: Alt+F11 (Windows) or Option+F11 (Mac) opens it.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). If the Project Explorer is not visible, press Ctrl+R (Windows) or Cmd+R (Mac) to show it.
- Insert a standard module. In the Project Explorer, right-click your workbook name, choose Insert > Module. A blank code window appears.
- Write a simple Find procedure. Paste the following code, replacing
Sheet1andA:Awith your sheet and column:
This usesSub FindStringInColumn() Dim rng As Range Dim foundCell As Range Dim searchTerm As String searchTerm = "apple" Set rng = ThisWorkbook.Worksheets("Sheet1").Columns("A") Set foundCell = rng.Find(What:=searchTerm, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False) If Not foundCell Is Nothing Then MsgBox "Found in row " & foundCell.Row Else MsgBox "Not found" End If End SubFindand returns the first matching row.LookAt:=xlPartmatches any cell that contains the string; usexlWholefor an exact match. - Run the macro. Press F5 while the cursor is inside the procedure, or use Run > Run Sub/UserForm. A message box shows the result. If the string is not present, you get "Not found".
- Loop through every cell if you need all matches. Replace the procedure with this version, which checks each used cell in column A and lists the rows:
Sub LoopStringInColumn() Dim ws As Worksheet Dim lastRow As Long, i As Long Dim searchTerm As String Dim hits As String Set ws = ThisWorkbook.Worksheets("Sheet1") searchTerm = "apple" lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row For i = 1 To lastRow If InStr(1, ws.Cells(i, "A").Value, searchTerm, vbTextCompare) > 0 Then hits = hits & i & ", " End If Next i If Len(hits) > 0 Then MsgBox "Found in rows: " & Left(hits, Len(hits) - 2) Else MsgBox "Not found" End If End SubInStrwithvbTextCompareignores case. TheEnd(xlUp)trick finds the last used row so the loop does not scan a million empty cells. - Return the value instead of a message. If you want the matching cell's content, change the message line to
MsgBox foundCell.Valuein the Find version, or collectws.Cells(i, "A").Valuein the loop version. - Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). A normal .xlsx file discards the macro when closed.
Common Mistakes
- Using
LookAt:=xlWholewhen you meant a partial match, so "pineapple" is missed when searching for "apple". - Forgetting that
Findsearches only the first match by default; to get all matches, callFindNextin a loop, starting from the first found cell. - Hard-coding the sheet name. If the sheet is renamed, the macro fails with "Subscript out of range". Use the sheet's CodeName or prompt the user.
- Leaving
MatchCaseat its default. It isFalseunless you set it, but mixing settings between calls can give inconsistent results. - Running the macro while a different workbook is active.
ThisWorkbookrefers to the file containing the code, which is usually what you want;ActiveWorkbookmay point elsewhere.
Tips
- Use
Application.WorksheetFunction.MatchorCountIffor a one-line existence check:If Application.CountIf(Columns("A"), "*apple*") > 0 Then. - For large columns,
Findis much faster than a cell-by-cell loop because it uses Excel's built-in search engine. - Store the search term in a cell and read it with
Range("D1").Valueso you can change it without editing code. - Turn off
ScreenUpdatingat the start of long loops and turn it back on at the end to keep the screen responsive. - Add
Option Explicitat the top of the module to catch undeclared variables early.
Troubleshooting
- "Subscript out of range" – the sheet name in the code does not match any open sheet. Check spelling and whether the sheet is hidden.
- "Object variable or With block variable not set" – you forgot
Setbefore assigning a Range or Worksheet object. - Macro does nothing – macros may be disabled. Check File > Options > Trust Center > Trust Center Settings > Macro Settings, and enable macros for this session only if you trust the file.
- Find returns Nothing when the text is clearly there – the search range may include only part of the column, or the cell contains a formula returning an error. Search
xlFormulasinstead ofxlValuesto test the underlying formula text. - Loop is slow – reduce the range with
End(xlUp)as shown, or read the column into a Variant array and loop the array in memory.
Frequently Asked Questions
Can I search for a string in a column without VBA?
Yes. Use Ctrl+F (Windows) or Cmd+F (Mac) to open Find, or a formula such as =COUNTIF(A:A,"*apple*")>0 to return TRUE or FALSE.
How do I make the search case-sensitive?
Set MatchCase:=True in the Find call, or use InStr(1, text, term, vbBinaryCompare) in a loop.
Does Find work on hidden rows or filtered data?
Find searches hidden rows as well; it does not skip them. If you need visible cells only, loop through SpecialCells(xlCellTypeVisible) instead.
Why does my macro find the string in the wrong column?
Check the range reference. Columns("A") means the entire column A on the active sheet; if you intended a different sheet, qualify it with the worksheet object.
Can I search for a string across multiple columns?
Yes. Set the range to Range("A:C") or UsedRange, then Find returns the first matching cell anywhere in that block; use foundCell.Column to see which column matched.