ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to hide multiple rows (https://www.excelbanter.com/excel-programming/308481-macro-hide-multiple-rows.html)

Minh

Macro to hide multiple rows
 
Hi,

I am writing a macro at the moment that hides a row given
a certain criteria is met. However, at the moment I have
had to do this on a row by row basis:

e.g.
If A8 = Cat Then
Rows("8:8").Select
Selection.EntireRow.Hidden = True
End If
If A9 = Cat Then
Rows("9:9").Select
Selection.EntireRow.Hidden = True
End If

etc....

Is there a way to do this to a range of cells rather than
on a cell by cell basis?

Thanks heaps.

Harald Staff

Macro to hide multiple rows
 
Hi

You will have to loop cell by cell for this, but it's easy and fast:

Sub test()
Dim R As Range, Cel As Range
Set R = Range("A1:M300")
For Each Cel In R
If Cel.Value = "Cat" Then _
Cel.EntireRow.Hidden = True
Next
End Sub

HTH. Best wishes Harald

"Minh" skrev i melding
...
Hi,

I am writing a macro at the moment that hides a row given
a certain criteria is met. However, at the moment I have
had to do this on a row by row basis:

e.g.
If A8 = Cat Then
Rows("8:8").Select
Selection.EntireRow.Hidden = True
End If
If A9 = Cat Then
Rows("9:9").Select
Selection.EntireRow.Hidden = True
End If

etc....

Is there a way to do this to a range of cells rather than
on a cell by cell basis?

Thanks heaps.




JulieD

Macro to hide multiple rows
 
alternatively, you might want to look into using filtering for this ... if
you use the macro recorder and data / autofilter .. and use the custom
option to display only those rows not equal to cat - it might give you what
you want.

Cheers
JulieD

"Harald Staff" wrote in message
...
Hi

You will have to loop cell by cell for this, but it's easy and fast:

Sub test()
Dim R As Range, Cel As Range
Set R = Range("A1:M300")
For Each Cel In R
If Cel.Value = "Cat" Then _
Cel.EntireRow.Hidden = True
Next
End Sub

HTH. Best wishes Harald

"Minh" skrev i melding
...
Hi,

I am writing a macro at the moment that hides a row given
a certain criteria is met. However, at the moment I have
had to do this on a row by row basis:

e.g.
If A8 = Cat Then
Rows("8:8").Select
Selection.EntireRow.Hidden = True
End If
If A9 = Cat Then
Rows("9:9").Select
Selection.EntireRow.Hidden = True
End If

etc....

Is there a way to do this to a range of cells rather than
on a cell by cell basis?

Thanks heaps.







All times are GMT +1. The time now is 07:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com