hi, i was wondering if there was a way to write a macro that will automatically hide a row based on a value in a cell in excel say, if the sum of a selection of cells = 0, i want to hide the row all together. is there a way to do that thanks.
anthony

macro that hides rows - excel
Brendan Egan
hi
Hiding rows is quite simple, the below hides row 1 is A1 is 0, you can also choose to hide the Rows/Columns in a range in a similar way
With ActiveSheet
If .Cells(1, 1) = 0 Then .Rows(1).Hidden = True
End With
develronioder
mizuno
Hi again
Try
Sub Update()
Dim lngLastRow, lngRow As Long
With ActiveSheet
lngLastRow = .Range("I1").End(-4121).Row
For lngRow = 1 To lngLastRow
If .Cells(lngRow, 9) = "a" Then .Rows(lngRow).Hidden = True
Next
End With
End Sub
NickUk
Ninja
If you need to look at each cell individually you will need to loop through them. If they are in one column and consecutive then this is not too difficult. The below loops through from A1 to the last row used in the sheet.
Public Sub MySub()
Dim lngLastRow, lngRow As Long
With ActiveSheet
lngLastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
For lngRow = 1 To lngLastRow
If .Cells(lngLastRow, 1) = 0 Then .Rows(lngLastRow).Hidden = True
Next
End With
End Sub
If you want to find the last non blank entry in a column use the below ( this is like pressing end down in cell A2)
lngLastRow = .Range("A1").End(-4121).Row
Change A1 to the relevant column / start row
YU_MSVB
Sub Update()
Dim lngLastRow, lngRow As Long
With ActiveSheet
lngLastRow = Cells.SpecialCells(xlCellTypeLastCell).row
For lngRow = 1 To lngLastRow
If .Cells(lngRow, 9) = "a" Then .Rows(lngRow).Hidden = True
Next
End With
End Sub
Thanks for all your help.
@nt
"If .Cells(lngLastRow, 9) = "b" Then .Rows(lngLastRow).Hidden = False"
thinking maybe that if it had a false statement it would continue on through but that didn't work. What am I doing wrong
Sub Update()
Dim lngLastRow, lngRow As Long
With ActiveSheet
lngLastRow = Cells.SpecialCells(xlCellTypeLastCell).row
For lngRow = 1 To lngLastRow
If .Cells(lngLastRow, 9) = "a" Then .Rows(lngLastRow).Hidden = True
lngLastRow = .Range("I1").End(-4121).row
Next
End With
End Sub