简体   繁体   中英

Need checkbox to hide and unhide empty rows

I have the following and I'm trying to just hide the lines that are empty and bring them back with a CHECKBOX so that I can add lines back when needed. I not want to delete them.

Sub Sample()
    Dim i As Long
    Dim DelRange As Range

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    For i = 1 To 250
        If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "L" & i)) = 0 Then
            If DelRange Is Nothing Then
                Set DelRange = Rows(i)
            Else
                Set DelRange = Union(DelRange, Rows(i))
            End If
        End If
    Next i

    If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp
LetsContinue:
    Application.ScreenUpdating = True

    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
  1. On the "Developer" tab in the Ribbon, in the "Controls" section, click the "Insert" dropdown, then click the checkbox control in the "ActiveX Controls" section (not the one in the "Form Controls" section)

  2. Click on your form where you want the checkbox to appear

  3. Double-click the checkbox, and in the auto-generated CheckBox1_Click method (assuming this is the first checkbox on the form), place a call to your method: Sample

  4. In your Sample method, perform the Replace below

  5. On the "Developer" tab in the Ribbon, in the "Controls" section, click the "Design Mode" button to turn off Design Mode (otherwise you won't be able to check and uncheck your checkbox)

Replace this:

If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp

With this, where "Sheet1" is the name of your Worksheet, and "CheckBox1" is the name of your checkbox:

If Not DelRange Is Nothing Then DelRange.Rows.Hidden = Sheets("Sheet1").CheckBox1.Value

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