VBA & Macro Problems

Clear Only Visible Filtered Rows in Excel VBA

Steve KSteve K Nov 18, 2026 869 views

Question details

Clear Status and Remarks only in visible Excel table rows filtered to Status “Ok,” then sort and copy Remarks values.

Clear Only Visible Filtered Rows in Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro filters the AS_cs_S table, clears matching cells, removes the filter, sorts by S/N, and copies Remarks to AS Sanitary starting at G3.
Observed behavior
The existing macro clears the entire Remarks column instead of only the visible matching rows.
Before you start

Check that the workbook contains the sheets AS cs S and AS Sanitary, the table AS_cs_S, and the columns Status, Remarks, and S/N. Make sure the table and destination cells are editable; the macro removes active table filters and overwrites destination cells starting at G3.

Solution 1Recommended

Clear visible table cells with a worksheet-qualified VBA macro

Use this macro to collect visible filtered cells before clearing them, then sort the table and copy the Remarks values.

The macro filters Status to “Ok” and collects the Status and Remarks cells from visible table rows. It qualifies the worksheets and table so the active sheet or selection cannot redirect the operation.

Existing filters on other columns can further restrict which rows are visible. The macro removes active table filters after clearing; manually hidden rows are not cleared.

1
Open the VBA editor

In desktop Excel, open the workbook, press Alt+F11, and choose Insert > Module.

2
Add the macro

Paste the following code into the module: Sub AS_Confirm_S() Dim ws As Worksheet, dest As Worksheet, tbl As ListObject Dim rowRange As Range, cellsToClear As Range Dim statusIndex As Long, remarksIndex As Long Set ws = ThisWorkbook.Worksheets("AS cs S") Set dest = ThisWorkbook.Worksheets("AS Sanitary") Set tbl = ws.ListObjects("AS_cs_S") If tbl.DataBodyRange Is Nothing Then Exit Sub statusIndex = tbl.ListColumns("Status").Index remarksIndex = tbl.ListColumns("Remarks").Index tbl.Range.AutoFilter Field:=statusIndex, Criteria1:="Ok" For Each rowRange In tbl.DataBodyRange.Rows If Not rowRange.EntireRow.Hidden Then If cellsToClear Is Nothing Then Set cellsToClear = Union(rowRange.Cells(1, remarksIndex), rowRange.Cells(1, statusIndex)) Else Set cellsToClear = Union(cellsToClear, rowRange.Cells(1, remarksIndex), rowRange.Cells(1, statusIndex)) End If End If Next rowRange If Not cellsToClear Is Nothing Then cellsToClear.ClearContents If tbl.AutoFilter.FilterMode Then tbl.AutoFilter.ShowAllData With tbl.Sort .SortFields.Clear .SortFields.Add Key:=tbl.ListColumns("S/N").DataBodyRange, Order:=xlAscending .Header = xlYes .Apply End With dest.Range("G3").Resize(tbl.DataBodyRange.Rows.Count, 1).Value = _ tbl.ListColumns("Remarks").DataBodyRange.Value End Sub

3
Run and check the results

Return to Excel, select Developer > Macros, choose AS_Confirm_S, and click Run. Check the sorted source table and the copied values on AS Sanitary starting at G3.

Clear visible table cells with a worksheet-qualified VBA macro
Contents and destination: ClearContents removes formulas as well as values from the selected cells. The copy writes one destination cell per table row but does not clear older values below that range.
Free Microsoft Office alternative

Work with Excel tables in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar interface for working with spreadsheet data. Open Excel files and use filtering and sorting tools as you move between office suites; this does not fix Excel VBA behavior, and macro compatibility can vary.

Filter and sort spreadsheet data using familiar controlsOpen and edit Microsoft Excel XLSX and XLS filesUse a free, lightweight office suite with a familiar interfaceMove existing Office files into WPS without rebuilding them from scratch
microsoft office alternative - wps office

Frequently Asked Questions

What happens if no visible rows have Status “Ok”?

The macro has no cells to clear, but it still removes active table filters, sorts by S/N, and copies the Remarks values to AS Sanitary.

Will the macro clear rows hidden by another filter?

No. It collects cells only from visible rows after applying the Status filter. Other active column filters can therefore limit the rows cleared, and the macro removes those filters afterward.

Why qualify the worksheet and table names in VBA?

References such as ThisWorkbook.Worksheets("AS cs S") and ws.ListObjects("AS_cs_S") direct the macro to the intended table instead of relying on the active sheet or selected cells.

Can I run this VBA macro in Excel for the web?

No. Open the workbook in desktop Excel to run VBA macros. Save it in a macro-enabled format such as XLSM if you want to retain the code.