Checkbox to run only after the main macro is run

Viewed 46

I have a main form (I guess I could call it so), that exports Excel sheets to PDFs. I want to have a checkbox that allows users to open the PDF file after run if it's checked. At the moment, when the checkbox is clicked, it tries to run the checkbox macro, but if it's not checked, it returns an error. This is the code:

Sub CheckBox5_Click()
    Dim ws As Worksheet
    Dim chckBox As CheckBox
    Set chckBox = Sheets("CompileSheet").CheckBoxes("CheckBox5")
    Set ws = ThisWorkbook.Sheets("Sheet2")
    If chckBox.Value = 1 Then
        ws.ExportAsFixedFormat Type:=xlTypePDF, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        OpenAfterPublish:=True
    Else
        ws.ExportAsFixedFormat Type:=xlTypePDF, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        OpenAfterPublish:=False
    End If
End Sub

Don't hesitate to ask for clarification. Thanks.

1 Answers

I got it!

I had to leave the checkbox macro blank(I didn't know you could) and figured out how to find the name if the box, and the following code after the sheet is created works for me:

If xlBook.Sheets("CompileSheet").Shapes("Check Box 5").OLEFormat.Object.Value = 1 Then
    ws.ExportAsFixedFormat Type:=xlTypePDF, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    OpenAfterPublish:=True
Else
    ws.ExportAsFixedFormat Type:=xlTypePDF, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    OpenAfterPublish:=False
End If

Thank you everyone for the help!

Related