Check boxes

Jam_Ica

New Member
Joined
Apr 12, 2011
Messages
7
Im creating a macro that can be run to edit a file. The macro has a form that opens with 8 check boxes( named checkbox1, checkbox2 ect) The idea is to click the check boxes of the categories you want to keep. The categories are on the excel spread sheet in column C. If you do not select the checkbox the whole row should be erased. This is what I have for code. Could be way off.... =)
Code:
Private Sub CommandButton1_Click()
    Dim firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim cat As String
    
    With Application
       CalcMode = .Calculation
       .Calculation = xlCalculationManual
       .ScreenUpdating = False
     End With
    
    With ActiveSheet
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False
        firstrow = .UsedRange.Cells(2).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
        cat = "Over All"
        For Lrow = Lastrow To firstrow Step -1
            With .Cells(Lrow, "C")
                If Not IsError(.Value) Then
                    If .Value = cat Then .EntireRow.Delete
                End If
            End With
        Next Lrow
    End With
    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
End Sub

I know I will have to repeat the code for each checkbox but have just been trying the one for now. It is eraseing all rows.

Any help would be greatly appreciated!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Code:
Private Sub CommandButton1_Click()
    
    Dim Lastrow As Long
    Dim CalcMode As Long
    Dim cat As String
    Dim cb As Integer
    
    With Application
       CalcMode = .Calculation
       .Calculation = xlCalculationManual
       .ScreenUpdating = False
    End With
    
    With ActiveSheet
        For cb = 1 To 8
                             
            If Me.Controls("CheckBox" & cb).Value = False Then
                cat = Me.Controls("CheckBox" & cb).Caption
                Lastrow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
                If Not .Columns("C").Find(cat, , xlValues, xlWhole, xlByRows, xlNext, False) Is Nothing Then
                    .Range("C1:C" & Lastrow).AutoFilter Field:=1, Criteria1:=cat
                    .Range("C2:C" & Lastrow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
                End If
                .AutoFilterMode = False
            End If
            
        Next cb
    End With
    
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
    
    MsgBox "Categories deleted.", vbInformation, "Process Complete"
    
End Sub
 
Upvote 0
Where do i put in the phrases. So if checkbox1 is not chexked erase the row if column 3 says "Over All". Each of the different checkboxes has a different prase to look for. Also when i run what u have helped with i get a type mismatch on the fastrow
 
Upvote 0
The "phrases" are the checkbox captions
Code:
cat = Me.Controls("CheckBox" & cb).Caption

Lastrow will have an error if the active sheet is empty.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top