I'm attempting to write a VBA macro that I can attach to a clickable button on an excel spreadsheet.
I need this button to hide and unhide specific rows in the spreadsheet if the word "Comments" is found in a specified column.
I have pieced together something that will do half the job, ie. hide the necessary columns. But I'm a bit stuck as to how to get it to reverse this action upon a second click.
This is what I have so far, but unsure how to proceed from here. Will hide the necessary rows effectively, but I need to be able to unhide them with the same button
Sub HideRows()
Dim rCheck As Range
Dim rHide As Range
Dim rCheckCell As Range
Set rCheck = Range("B18:B8000")
rCheck.EntireRow.Hidden = False
Set rHide = rCheck.Offset(rCheck.Rows.Count).Resize(1)
For Each rCheckCell In rCheck.Cells
If InStr(1, rCheckCell, "Comments", vbTextCompare) > 0 Then Set rHide = Union(rHide, rCheckCell)
Next rCheckCell
If Not rHide Is Nothing Then rHide.EntireRow.Hidden = True
End Sub
Application.ScreenUpdating = Falsebefore your code then set it toTrueafterwards.If Not rHide Is Nothing Then rHide.EntireRow.Hidden = Not rHide.EntireRow.Hiddenmaybe?