Good evening from NZ.
I have a command button that hides rows based on zero value in Col B
then a 2nd btn to unhide those rows. I then tried combiningthese to create a toggle button hide/umhide rows. (Read on McDuff.)
The challenge is that as I want this code for several workbooks that
will have data of varying row lengths & not all the zero values will
be in the exact same Cell (but range will be similar) for each workbook or each month.
Having done a little research on the Board (& googled) I have not
found an answer to my challenge. So I am sure someone will point me
in the right direction. (I have tried combing the 2 macros using a
flag & the syntax must be right as no error but it doesn't toggle
hide/unhide rows,)
Code:Private Sub CommandButton1_Click() 'Hide rows with value equal zero BeginRow = 3 EndRow = ActiveSheet.UsedRange.Rows.Count ChkCol = 2 Range("B3:B" & EndRow).EntireRow.Hidden = True For RowCnt = BeginRow To EndRow If Cells(RowCnt, ChkCol).Value <> 0 Then Cells(RowCnt, ChkCol).EntireRow.Hidden = False End If Next RowCnt Range("A2").Activate End Sub Private Sub CommandButton2_Click() 'Unhide all rows BeginRow = 3 EndRow = ActiveSheet.UsedRange.Rows.Count ChkCol = 2 Range("B3:B" & EndRow).EntireRow.Hidden = False Range("A2").Activate End Sub Private Sub CommandButton3_Click() 'Toggles rows with value equal zero Dim flg As Boolean flg = True BeginRow = 3 EndRow = ActiveSheet.UsedRange.Rows.Count ChkCol = 2 For RowCnt = BeginRow To EndRow If Cells(RowCnt, ChkCol).EntireRow.Hidden = True Then flg = False Exit For Else flg = True End If Next RowCnt If flg = False Then Range("B3:B" & EndRow).EntireRow.Hidden = False Else 'Cells(RowCnt, ChkCol).EntireRow.Hidden = True For RowCnt = BeginRow To EndRow If Cells(RowCnt, ChkCol).Value <> 0 Then Cells(RowCnt, ChkCol).EntireRow.Hidden = False End If Next RowCnt End If Range("A2").Activate End Sub




Reply With Quote