Checkbox Thinks It's Checked When It's Not

ransomedbyfire

Board Regular
Joined
Mar 9, 2011
Messages
121
I've got a userform with something like 9 checkboxes on it that each represent a worksheet to be selected for printing. For some reason, the code seems to be "remembering" the last tab I told it to print and including it in the new print list.

Here is the code that runs with the userform is opened:
Code:
Sub print_custom()

Sheet12.Range("d:d").ClearContents

UserForm1.Cs.Value = False
UserForm1.Es.Value = False
UserForm1.Ex.Value = False
UserForm1.Ey.Value = False
UserForm1.In.Value = False
UserForm1.J.Value = False
UserForm1.Ka.Value = False
UserForm1.Od.Value = False
UserForm1.Sx.Value = False
UserForm1.Sy.Value = False
UserForm1.Select_All.Value = False

UserForm1.Show
And here is one of the codes that add each checked sheetname to the printing queue.
Code:
Private Sub Cs_Click()

sheeti = Sheets("Cs").Index

Set clicked = Sheet12.Range("d:d").Find(what:=sheeti)
    If Not clicked Is Nothing Then
        findrow = Sheet12.Range("d:d").Find(what:=sheeti).Row
        Sheet12.Range("d" & findrow).Delete shift:=xlUp
        GoTo exitsub
    End If
    
firstblank = Sheet12.Range("D:D").Find(what:="").Row

If Sheet12.Range("d1") = "" Then
    firstblank = 1
End If

Sheet12.Cells(firstblank, 4) = sheeti

exitsub:
End Sub
And, finally, here is the code that runs when the Print button is clicked.
Code:
Private Sub Print_Button_Click()

UserForm1.Hide

Application.Wait (100)

If Sheet12.Range("d1") = "" Then
    MsgBox "You have not selected any sheets to print. Please hit the ""Print Custom"" button and select at least one sheet to print."
    GoTo exitsub
End If

lastrow = Sheet12.Range("d:d").Find(what:="").Row - 1

If lastrow = 11 Then
    lastrow = 10
End If

Sheets("Cs").Visible = True

Sheets(Sheet12.Cells(1, 4).Value).Select

For Row = 1 To lastrow
    sheeti = Sheet12.Cells(Row, 4).Value
    ActiveWorkbook.Sheets(sheeti).Select False
    MsgBox Sheets(sheeti).Name
Next Row

    ActiveWindow.SelectedSheets.PrintOut preview:=True
    Sheets("Cs").Visible = False
    ThisWorkbook.Sheets("io").Select
    
exitsub:
End Sub
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I don't see a problem with your code.


Troubleshooting suggestions:

Reduce the size of the VBE window and position it so you can observe Sheet 12, Column D while you click the checkboxes and see if they are populating as you expect.

Add a few preakpoints and see how the worksheets are being selected prior to printing.

Here are a few comments on the code:

It is advisable to use the entire find statement the first time it is executed in your code, otherwise any undefined settings will be those that were used by the previous Find Statement, whether it was applied manually or by code.

.Find(What:="FRT", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

Two ways of finding the last used cell:
lastrow = Sheet12.Range("d:d").Find(what:="").Row - 1
lastrow = Sheet12.Cells(Rows.Count,4).End(xlUp).Row

I believe you can replace this:
findrow = Sheet12.Range("d:d").Find(what:=sheeti).Row
with
findrow = clicked.Row



This might be an alternative to your second and third blocks of code:
Code:
Option Explicit
Sub ProcessCheckBoxes()
    Dim iX As Integer
    Dim iY As Integer
    Dim bPrintMe As Boolean
    Dim bSelected As Boolean
 
    For iX = 0 To Me.Controls.Count - 1
        If TypeName(UserForm1.Controls(iX)) = "CheckBox" Then 'If the control is a CheckBox
            If UserForm1.Controls(iX) Then 'If the CheckBox is checked
                For iY = 1 To Sheets.Count
                    If UCase(UserForm1.Controls(iX).Name) = UCase(Sheets(iY).Name) Then 'Checkbox name matches a sheet name
                        If Not bSelected Then 'First sheet
                            ActiveWorkbook.Sheets(Sheets(iY).Name).Select True 'Select First Sheet
                            bSelected = True
                        Else                  'Selecting additional sheets
                            ActiveWorkbook.Sheets(Sheets(iY).Name).Select False 'Extend Selection
                        End If
                        bPrintMe = True
                    End If
                Next
             End If
        End If
    Next
 
    If Not bPrintMe Then
        MsgBox "You have not selected any sheets to print. Please hit the ""Print Custom"" button and select at least one sheet to print."
    Else
        ActiveWindow.SelectedSheets.PrintOut preview:=True
        Sheets("Cs").Visible = False
        ThisWorkbook.Sheets("io").Select
    End If
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,503
Members
452,917
Latest member
MrsMSalt

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