gpt4 book ai didi

excel - 取消选择或取消选择工作表

转载 作者:行者123 更新时间:2023-12-04 20:44:42 27 4
gpt4 key购买 nike

我有一个带有 x 个工作表的工作簿。
Sheet1 中的一个按钮 (commandbutton1) 打开一个带有两个复选框的用户表单和一个预览按钮,用于在 2 个工作表之间进行选择以在 PDF 中进行预览。

这是我的预览按钮代码。

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton1_Click()
Dim i As Integer
Dim CheckBoxName As String

For i = 1 To 2
CheckBoxName = "CheckBox" & i
If Me.Controls(CheckBoxName).Value = True Then
Sheets(Me.Controls(CheckBoxName).Caption).Select (False)
End If
Next i

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Preview.pdf"

Unload Me

Dim File As String
File = "D:\Preview.pdf"
ShellExecute 0, "Open", File, "", "", vbNormalNoFocus
End Sub

现在,问题是 Sheet1 总是包含在预览文件中,因为它是包含 commandbutton1 的工作表。

那么有没有办法在导出工作表 6 和 21 之前取消选择 sheet1 ?

我也尝试了另一种方法,但它总是停留在这一行 (Sheets(Array(SheetNames)).Select)
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton1_Click()
Dim i As Integer
Dim CheckBoxName As String
Dim SheetNames As String

For i = 1 To 2
CheckBoxName = "CheckBox" & i
If Me.Controls(CheckBoxName).Value = True Then
SheetNames = SheetNames & Me.Controls(CheckBoxName).Caption & ","
End If
Next i

SheetNames = Mid(SheetNames, 1, Len(SheetNames) - 1)

Sheets(Array(SheetNames)).Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Preview.pdf"

Unload Me

Dim File As String
File = "D:\Preview.pdf"

ShellExecute 0, "Open", File, "", "", vbNormalNoFocus
End Sub

请问有什么帮助吗??

提前谢谢你。

最佳答案

诀窍是如何传递参数 False

Sheets(Me.Controls(CheckBoxName).Caption).Select (False)

这是你正在尝试的吗?
Private Sub CommandButton1_Click()
Dim i As Integer, j As Integer

For i = 1 To 6
CheckBoxName = "CheckBox" & i
If Me.Controls(CheckBoxName).Value = True Then
j = j + 1
Worksheets(Me.Controls(CheckBoxName).Caption).Select (j = 1)
End If
Next
If j = 0 Then Exit Sub

'
'~~> Rest of your code
'
End Sub

之前:

enter image description here

之后:

enter image description here

或者

enter image description here

关于excel - 取消选择或取消选择工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21642911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com