excel - 复选框仅在主宏运行后运行
问题描述
我有一个主表单(我想我可以这样称呼它),它将 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
不要犹豫,要求澄清。谢谢。
解决方案
我得到了它!
我不得不将复选框宏留空(我不知道你可以),并想出了如何找到该框的名称,并且创建工作表后的以下代码对我有用:
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
谢谢大家的帮助!
推荐阅读
- ios - 如何配置 Pods 项目的构建设置?
- sql - 用于股票市场订单匹配的 SQL SERVER
- python - 加载大型 Pickle 对象的进度条?
- python - 您可以将训练参数与 tf.keras.Model() 构造函数一起使用吗?
- c# - 根据组合框的值计算文本框的值?(wpf)
- java - 检查开关按钮并调用 setOnCheckedChangeListener 后,Android 活动关闭
- c++ - 删除 C++ 中的空行
- c# - 在获取父实体 EFCore 时加载子实体
- reactjs - Fastify 使用 next.js 为渲染提供 react 属性
- c# - EF、ASP.NET MVC 和 StructureMap 的依赖注入问题