首页 > 解决方案 > 复选框仅在主宏运行后运行

问题描述

我有一个主表单(我想我可以这样称呼它),它将 Excel 工作表导出为 PDF。我想要一个复选框,如果选中,则允许用户在运行后打开 PDF 文件。目前,当单击复选框时,它会尝试运行复选框宏,但如果未选中,则会返回错误。这是代码:

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

不要犹豫,要求澄清。谢谢。

标签: excelvba

解决方案


我得到了它!

我不得不将复选框宏留空(我不知道你可以),并想出了如何找到该框的名称,并且创建工作表后的以下代码对我有用:

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

谢谢大家的帮助!


推荐阅读