2014-02-08 122 views
1

我有一個包含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的工作表。

那麼有沒有辦法在導出工作表之前取消選擇Sheet1 & 21?

我也嘗試另一種方式,但它始終堅持在這條線(表(陣列(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

關鍵是如何傳遞參數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

+0

這正是我正在尋找的..謝謝你soooo多..我真的很感謝你的幫助 – user3286479