Creating a macro to hide all rows containing currently selected values


New Member
I am hoping to create a way to hide rows on multiple sheets based off the text in the currently highlighted cell. So, for instance if I am on Sheet 1 and I am highlighting a cell containing "John Smith", I want to hit a button (from developer inserts) and it will hide all rows (across all 5 sheets) if their column c contains "John Smith".

Is this sort of thing even possible? I am incredibly new to creating macros myself, so I am not sure if this is beyond the limits of excel. Perhaps I would need a add-in to be able to do this?



New Member
Additionally, I want to be able to do this multiple times with different cells, while still maintaining the previously hidden rows.

Rick Rothstein

MrExcel MVP
Yes, "John Smith" would be in the cell all by itself.
Give this macro a try...
Sub HideRows()
  Dim Text As String, WS As Worksheet
  Text = ActiveCell.Value
  On Error Resume Next
  Application.ScreenUpdating = False
  For Each WS In Worksheets
    With WS.Columns("C")
      .Replace Text, "#N/A", xlWhole, , False, , False, False
      .SpecialCells(xlConstants, xlErrors).EntireRow.Hidden = True
      .Replace "#N/A", Text
    End With
  Application.ScreenUpdating = True
  On Error GoTo 0
End Sub


New Member
Actually, after looking through the macro I realized I had mistakenly told you column "C", when it was actually column "D". After I changed that in the macro it works great! Thank you so much

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Syntax errors
    Good Morning, Trying to compile a workbook, I keep getting a few errors. Here are the first two: [code=rich]Syntax Error: Function...