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

时间:2014-02-08 06:39:11

标签: excel excel-vba vba

我有一个带有x工作表的工作簿。 还有一个Sheet1中的按钮(commandbutton1),它打开一个带有两个复选框和一个预览按钮的用户窗体,可以在两张纸之间进行选择,以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&之前取消选择sheet1。 21 ??

我也尝试了另一种方法,但它总是停留在这一行(Sheets(Array(SheetNames))。选择)

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

有任何帮助吗?

提前感谢你。

1 个答案:

答案 0 :(得分:1)

技巧是如何传递

中的参数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

OR

enter image description here