To select only the visible cells in Excel with VBA, use the SpecialCells(xlCellTypeVisible) method on a range. For example, Range("A1:C10").SpecialCells(xlCellTypeVisible).Select selects just the rows that are not hidden by a filter or manual hiding. This is essential when you need to copy, format, or delete filtered data without touching the hidden rows. You do not need any add-ins or special settings, just the Excel desktop application and a basic understanding of the VBA editor.

What You'll Need

  • Excel for Windows or Mac (the VBA editor is not available in Excel for the web).
  • A workbook with data that has been filtered or has hidden rows.
  • The Developer tab enabled (File > Options > Customize Ribbon > check Developer).

Step-by-Step

  1. Open the VBA editor by pressing Alt+F11 (Windows) or Fn+Alt+F11 (Mac). If the Developer tab is enabled, you can also click Developer > Visual Basic.
  2. Insert a new module by right-clicking on your workbook name in the Project Explorer and choosing Insert > Module. This is where you will write your code.
  3. Write the basic selection code in the module. For example:
    Sub SelectVisible()
        Range("A1:C10").SpecialCells(xlCellTypeVisible).Select
    End Sub
    Replace "A1:C10" with your actual data range. This code selects only the visible cells in that range.
  4. Run the macro by pressing F5 or clicking the Run button. The visible cells in the specified range will be selected.
  5. Use the selection for further actions. For instance, to copy the visible cells to another location, add Selection.Copy after the Select line, then paste elsewhere. To delete visible rows, use Selection.EntireRow.Delete.
  6. Handle errors if there are no visible cells. If all cells are hidden, SpecialCells will raise an error. Wrap the code in error handling:
    On Error Resume Next
    Range("A1:C10").SpecialCells(xlCellTypeVisible).Select
    If Err.Number <> 0 Then MsgBox "No visible cells found."
    On Error GoTo 0
  7. Test with filtered data. Apply a filter to your data (Data > Filter), hide some rows, and run the macro again. The selection should include only the rows that are currently visible.

Common Mistakes

  • Forgetting to handle errors when no visible cells exist. Always include error handling to avoid runtime errors.
  • Using SpecialCells on a range that includes hidden columns. The method works on rows and columns, but if you hide entire columns, those cells are also considered hidden and will be excluded from the selection.
  • Assuming SpecialCells works on all ranges. It only works on ranges that contain at least one visible cell. If the entire range is hidden, it fails.
  • Not qualifying the range with a worksheet. In a macro, unqualified Range refers to the active sheet. If you run the macro from a different sheet, it may select the wrong data. Use Worksheets("Sheet1").Range("A1:C10") to be explicit.

Tips

  • To select visible cells in the entire used range, use ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Select.
  • Combine with AutoFilter to select visible cells after applying a filter programmatically.
  • Use Selection.Areas.Count to check how many separate visible areas are selected (useful when rows are non-contiguous).
  • For copying visible cells only, you can also use Range("A1:C10").SpecialCells(xlCellTypeVisible).Copy Destination:=Range("E1").
  • Remember that SpecialCells ignores cells hidden by filters, but also cells hidden by row height set to zero or manually hidden rows.

Troubleshooting

  • Error 1004: No cells were found – This means there are no visible cells in the specified range. Check that your range is correct and that at least one row is visible. Use error handling as shown in step 6.
  • Selection includes hidden rows – Ensure you are using SpecialCells(xlCellTypeVisible) and not just Range.Select. Also, verify that the rows are actually hidden (filtered or manually hidden) and not just grouped.
  • Macro selects wrong range – Qualify the range with the worksheet name, e.g., Worksheets("Data").Range("A1:C10").
  • Code runs but nothing happens – Check that the worksheet is not protected. If protected, you may need to unprotect it first (Review > Unprotect Sheet).
  • SpecialCells fails on large ranges – Excel has a limit of 8192 separate areas for SpecialCells. If your filtered data has many non-contiguous visible rows, you may hit this limit. Work with smaller ranges or use a loop to process rows individually.

Frequently Asked Questions

Can I select visible cells without VBA?

Yes, you can use the Go To Special dialog (Alt+; or F5 > Special > Visible cells only) to select visible cells manually. VBA automates this for repeated tasks.

Does SpecialCells(xlCellTypeVisible) work on filtered data?

Yes, it selects only the rows that are visible after filtering, excluding hidden rows.

What if my data has hidden columns?

SpecialCells(xlCellTypeVisible) will also exclude cells in hidden columns. If you want to include hidden columns but exclude hidden rows, you need a different approach, such as looping through rows and checking EntireRow.Hidden.

Can I use this to copy visible cells to another sheet?

Yes, after selecting visible cells, use Selection.Copy and then paste to the destination sheet. Ensure the destination sheet is activated before pasting.

Why does my macro select the entire row instead of just the range?

If you use EntireRow property, it selects the whole row. Use Range("A1:C10") to limit to specific columns.

Is there a limit to the number of visible cells I can select?

Excel can handle large selections, but SpecialCells has a limit of 8192 areas. If you exceed this, you may need to process in chunks.