Replace Text Only in Filtered Excel Table Rows with VBA
Question details
Replace text in the visible rows of a filtered Excel table without changing hidden rows.
- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- An existing VBA procedure needs to update text in the tenth column of a filtered table and handle filters that return no records.
- Observed behavior
- Only visible table cells containing the target text should be updated; an empty filtered result should be skipped without an error.
Confirm that the table is named List1, has at least 10 columns, and is on the active worksheet. Apply the desired filter before running the replacement, and save the workbook as an .xlsm file if you need to retain the macro.
Replace text in visible cells within an existing VBA procedure
Use the table column's visible DataBodyRange cells to update filtered rows and skip an empty result.
This example replaces the text within each visible cell rather than requiring an exact cell match. It skips formulas, errors, empty cells, and non-text values.
In Excel, open the VBA editor with Alt+F11 and select the module containing your existing procedure. Place the following code after the table's filter has been applied.
Insert this code inside the existing procedure, changing List1 or the column number if needed. Use different variable names if these names are already declared: Dim lo As ListObject Dim visibleCells As Range Dim visibleCell As Range Set lo = ActiveSheet.ListObjects("List1") Set visibleCells = Nothing If Not lo.DataBodyRange Is Nothing Then On Error Resume Next Set visibleCells = lo.ListColumns(10).DataBodyRange.SpecialCells(xlCellTypeVisible) On Error GoTo 0 If Not visibleCells Is Nothing Then For Each visibleCell In visibleCells.Cells If Not visibleCell.HasFormula Then If Not IsError(visibleCell.Value2) Then If VarType(visibleCell.Value2) = vbString Then visibleCell.Value2 = Replace(visibleCell.Value2, "to be paid", "paid", 1, -1, vbTextCompare) End If End If End If Next visibleCell End If End If
Run the existing procedure from the VBA editor or Excel's Macros dialog. Check the visible cells in the tenth table column, then clear the filter to confirm that hidden rows were not changed.
Work with filtered spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface. Open compatible Excel workbooks and use spreadsheet filters as you move between Office applications; it does not fix Excel VBA errors.

Frequently Asked Questions
Can I add this code without creating another Sub?
Yes. Insert it inside your existing procedure after the filter is applied. Adjust the variable names if they are already declared in that procedure.
What happens if the filter returns no visible rows?
SpecialCells may raise an error when there are no visible cells. The example suppresses that error, leaves visibleCells as Nothing, and skips the replacement.
How do I target a different table or column?
Change "List1" in ActiveSheet.ListObjects("List1") to your table name and change 10 in ListColumns(10) to the desired column number. The worksheet containing the table must be active.
Does the replacement change formulas or hidden rows?
No. The example selects visible cells with SpecialCells(xlCellTypeVisible) and skips cells containing formulas. It replaces matching text in visible text values only.




