简体   繁体   中英

Vba code for hide and unhide rows by using a checkbox

enter image description here {SOLVED by TOM- please refer ActiveX control} I have two buttons ( button 1 & button 2) to hide and unhide row contains a specific word “petroleum” in another sheets of workbook ( if I click button 1 all the rows contains “petroleum” will hide , and if I click button 2 then all the rows contains “petroleum” will unhide)

My doubt is, can I use ONE check box instead of TWO button to run this vba code ( the idea is if I checkedin the checkbox the row should hide and unhide if the checkbox is unchecked .

"for row hide "

Sub Button1_Click()
Dim sht As Worksheet
Application.ScreenUpdating = False
For Each sht In Worksheets
    beginRow = 2
    endRow = 1000
    chkCol = 12
    For RowCnt = beginRow To endRow
        If sht.Cells(RowCnt, chkCol).Value = "Petroleum" Then
            sht.Cells(RowCnt, chkCol).EntireRow.Hidden = True

        End If
    Next RowCnt
Next sht
Application.ScreenUpdating = True
End Sub

"For row unhide"

Sub Button2_Click()
Dim sht As Worksheet
Application.ScreenUpdating = False
For Each sht In Worksheets
    beginRow = 2
    endRow = 1000
    chkCol = 12
    For RowCnt = beginRow To endRow
        If sht.Cells(RowCnt, chkCol).Value = "Petroleum" Then
            sht.Cells(RowCnt, chkCol).EntireRow.Hidden = False

        End If
    Next RowCnt
Next sht
Application.ScreenUpdating = True
End Sub

'As advised by Tom i have revised the code as per below (this is working for me)

Private Sub CheckBox13_Click()
    Dim sht As Worksheet

    Application.ScreenUpdating = False
    For Each sht In Worksheets
        beginRow = 2
        endRow = 1000
        chkCol = 12
        For RowCnt = beginRow To endRow
            If sht.Cells(RowCnt, chkCol).Value = "Petroleum" Then

                sht.Cells(RowCnt, chkCol).EntireRow.Hidden = CheckBox13.Value
            End If
        Next RowCnt
    Next sht
    Application.ScreenUpdating = True
End Sub

You can also use the onclick event on the checkbox, like this:

Private Sub CheckBox1_Click()
    If CheckBox1.Value = True Then
       ' hide rows
    Else
        ' unhide rows
    End If
End Sub

Good luck

For Form controls you could do it with one button using the following

Sub Button1_Click()
    Dim sht As Worksheet
    Dim chkBox As CheckBox

    Set chkBox = Application.Caller

    Application.ScreenUpdating = False
    For Each sht In Worksheets
        beginRow = 2
        endRow = 1000
        chkCol = 12
        For RowCnt = beginRow To endRow
            If sht.Cells(RowCnt, chkCol).Value = "Petroleum" Then
                sht.Cells(RowCnt, chkCol).EntireRow.Hidden = IIf(chkBox.Value = 1, True, False)
            End If
        Next RowCnt
    Next sht
    Application.ScreenUpdating = True
End Sub

For ActiveX similarly you could do it with

Private Sub CheckBox13_Click()
    Dim sht As Worksheet

    Application.ScreenUpdating = False
    For Each sht In Worksheets
        beginRow = 2
        endRow = 1000
        chkCol = 12
        For RowCnt = beginRow To endRow
            If sht.Cells(RowCnt, chkCol).Value = "Petroleum" Then

                sht.Cells(RowCnt, chkCol).EntireRow.Hidden = CheckBox13.Value
            End If
        Next RowCnt
    Next sht
    Application.ScreenUpdating = True
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM