Friday, June 22, 2012

Macro to hide rows based on cell values

I had many rows that I wanted to hide on many sheets based on what cell values their were.

Suppose you have these values in the A column

Cat Dog
Super Man is strong
Wonder Woman is strong too
I am working

You want to hide the rows that contain Super man and Wonder Woman.

Here is an excel 2007 macro to do that (using an array and a for loop) :


Sub hideRows()


myarray = Array("SUPER MAN", _
    "WONDER WOMAN")


For Each cell In ActiveSheet.Range("A1:a7")


For i = 0 To UBound(myarray)


If InStr(UCase(cell.Value), myarray(i)) Then cell.EntireRow.Hidden = True
Next i


Next cell


End Sub

No comments:

Post a Comment