Hello,
I have the following two macros which separately work but when I run one and then the next, they don't to build on top of each other and work in unison. running macro 2 undoes macro 1 and vice versa.
Essentially what I need is:
a) hide entire rows where individual row total = 0 (Macro 1)
b) in addition to this, hide entire rows included in named ranges where total of the named range = 0 (Macro 2)
c) unhide all of the above if the information totals > 0
In the below case, combined macro would hide row 3 (0 total) and hide rows 7-11 (George family named range as total of range = 0)
Thanks in advance!
Here is the data:
[TABLE="width: 324"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Named Range[/TD]
[TD]Name[/TD]
[TD]Total Hours[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Smith[/TD]
[TD]Smith Family[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Smith[/TD]
[TD]John[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Smith[/TD]
[TD]Mary[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Smith[/TD]
[TD]Heather[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Smith[/TD]
[TD]Total Smith[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]George[/TD]
[TD]George Family[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]George[/TD]
[TD]John[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]George[/TD]
[TD]Mary[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]George[/TD]
[TD]Heather[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]George[/TD]
[TD]Total George[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Spencer[/TD]
[TD]Spencer Family[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]Spencer[/TD]
[TD]John[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]Spencer[/TD]
[TD]Mary[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]Spencer[/TD]
[TD]Heather[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD]Spencer[/TD]
[TD]Total Spencer[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]
Macro 1 - purpose is to hide rows where row total = 0. Unhide if data changes to > 0 ( i.e. would hide rows 3 and 8-11)
Sub Hide_H()
Application.ScreenUpdating = False
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row
On Error Resume Next
For Each c In Range("D1:D" & LastRow)
If c.Value > 0 Or c.Value = "" Then
c.EntireRow.Hidden = False
ElseIf c.Value = 0 Then
c.EntireRow.Hidden = True
End If
Next
On Error GoTo 0
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Macro 2 - would hide entire named range if total of range = 0. Unhide if data changes to > 0 (i.e. would hide rows 7-11 George Family as total of range = 0
[TABLE="width: 350"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Sub NamedRange()[/TD]
[/TR]
[TR]
[TD]Smith = WorksheetFunction.Sum(Range("Smith"))[/TD]
[/TR]
[TR]
[TD]George = WorksheetFunction.Sum(Range("George"))[/TD]
[/TR]
[TR]
[TD]Spencer = WorksheetFunction.Sum(Range("Spencer"))[/TD]
[/TR]
[TR]
[TD] If Smith = 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Smith").EntireRow.Hidden = True[/TD]
[/TR]
[TR]
[TD] ElseIf Smith > 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Smith").EntireRow.Hidden = False[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] If George = 0 Then[/TD]
[/TR]
[TR]
[TD] Range("George").EntireRow.Hidden = True[/TD]
[/TR]
[TR]
[TD] ElseIf George > 0 Then[/TD]
[/TR]
[TR]
[TD] Range("George").EntireRow.Hidden = False[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] If Spencer = 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Spencer").EntireRow.Hidden = True[/TD]
[/TR]
[TR]
[TD] ElseIf Spencer > 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Spencer").EntireRow.Hidden = False[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD] End Sub[/TD]
[/TR]
</tbody>[/TABLE]
I have the following two macros which separately work but when I run one and then the next, they don't to build on top of each other and work in unison. running macro 2 undoes macro 1 and vice versa.
Essentially what I need is:
a) hide entire rows where individual row total = 0 (Macro 1)
b) in addition to this, hide entire rows included in named ranges where total of the named range = 0 (Macro 2)
c) unhide all of the above if the information totals > 0
In the below case, combined macro would hide row 3 (0 total) and hide rows 7-11 (George family named range as total of range = 0)
Thanks in advance!
Here is the data:
[TABLE="width: 324"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Named Range[/TD]
[TD]Name[/TD]
[TD]Total Hours[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Smith[/TD]
[TD]Smith Family[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Smith[/TD]
[TD]John[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Smith[/TD]
[TD]Mary[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Smith[/TD]
[TD]Heather[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Smith[/TD]
[TD]Total Smith[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]George[/TD]
[TD]George Family[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]George[/TD]
[TD]John[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]George[/TD]
[TD]Mary[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]George[/TD]
[TD]Heather[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]George[/TD]
[TD]Total George[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Spencer[/TD]
[TD]Spencer Family[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]Spencer[/TD]
[TD]John[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]Spencer[/TD]
[TD]Mary[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]Spencer[/TD]
[TD]Heather[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD]Spencer[/TD]
[TD]Total Spencer[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]
Macro 1 - purpose is to hide rows where row total = 0. Unhide if data changes to > 0 ( i.e. would hide rows 3 and 8-11)
Sub Hide_H()
Application.ScreenUpdating = False
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row
On Error Resume Next
For Each c In Range("D1:D" & LastRow)
If c.Value > 0 Or c.Value = "" Then
c.EntireRow.Hidden = False
ElseIf c.Value = 0 Then
c.EntireRow.Hidden = True
End If
Next
On Error GoTo 0
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Macro 2 - would hide entire named range if total of range = 0. Unhide if data changes to > 0 (i.e. would hide rows 7-11 George Family as total of range = 0
[TABLE="width: 350"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Sub NamedRange()[/TD]
[/TR]
[TR]
[TD]Smith = WorksheetFunction.Sum(Range("Smith"))[/TD]
[/TR]
[TR]
[TD]George = WorksheetFunction.Sum(Range("George"))[/TD]
[/TR]
[TR]
[TD]Spencer = WorksheetFunction.Sum(Range("Spencer"))[/TD]
[/TR]
[TR]
[TD] If Smith = 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Smith").EntireRow.Hidden = True[/TD]
[/TR]
[TR]
[TD] ElseIf Smith > 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Smith").EntireRow.Hidden = False[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] If George = 0 Then[/TD]
[/TR]
[TR]
[TD] Range("George").EntireRow.Hidden = True[/TD]
[/TR]
[TR]
[TD] ElseIf George > 0 Then[/TD]
[/TR]
[TR]
[TD] Range("George").EntireRow.Hidden = False[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] If Spencer = 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Spencer").EntireRow.Hidden = True[/TD]
[/TR]
[TR]
[TD] ElseIf Spencer > 0 Then[/TD]
[/TR]
[TR]
[TD] Range("Spencer").EntireRow.Hidden = False[/TD]
[/TR]
[TR]
[TD] End If[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD] End Sub[/TD]
[/TR]
</tbody>[/TABLE]