简体   繁体   中英

use checkbox to hide/unhide rows based on cell value

Using VBA in excel, trying to understand how I can use a checkbox to hide/unhide any row that has a specific value in a specific column. My VBA skills are getting better more I practice but I am still not good with loops just yet. Appreciate any help you can provide. Here is what I have so far.

Private Sub CkBx_ShowAllRecords_Click()
If Me.CkBx_ShowAllRecords = True Then
 For Each Row In Range("Table1").ListObject.ListColumns
    If Row.Cells(1, "column5").Value = "Submission Complete" Then
    Application.EntireRow.Visible=True
Next
End if
End Sub

Additionally when I uncheck the box I would want all rows where column 5 cell value equals "submission complete" would be hidden (just the opposite of what I put above when I check the box control).

Hope this may help you:

Private Sub CkBx_ShowAllRecords_Click()
Dim i As Long
If Me.CkBx_ShowAllRecords = True Then
    For i = 1 To ActiveSheet.ListObjects("Table1").Range.Rows.Count
        If ActiveSheet.ListObjects("Table1").DataBodyRange(i, 5).Value = "Submission Complete" Then
            Rows((i + 1) & ":" & (i + 1)).Select
            Selection.EntireRow.Hidden = True
        End If
    Next i
Else
    ActiveSheet.Rows.EntireRow.Hidden = False
End If
Me.Hide
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