2017-08-10 102 views
0

我有一个Active X按钮,单击时会为我的工作簿中的每个工作表打开一个带有复选框的用户窗体。我想要做的是允许用户选择他们想要生成的PDF到PDF中。目前,无论用户点击生成PDF的输入按钮时选择的复选框是什么,工作簿中的所有工作表都包含在PDF中,而不是选定的内容,并且子工具不会自动结束,我必须自动进入VBA并点击停止按钮。所以,如果你能让我知道我在做什么我的代码出错了,为什么复选框没有选择我想要生成PDF的工作表。预先感谢所有帮助!复选框不选择用于PDF生成的工作表

Private Sub chbxEnter_Click() 

Dim PDFsheets As String 
Dim s As Worksheet 
PDFsheets = "Approval Form,Business Plan,Deal Worksheet,All Manager Deal Recap,Deal Recap,MEC Dealership Profile,Loyal,Mid Loyal,Non Loyal,Projected Incentive Report,MEC" 
ary = Split(PDFsheets, ",") 

    If CheckBox1.Value = True Then 
    PDFsheets = "Approval Form" 
    End If 

    If CheckBox2.Value = True Then 
     If PDFsheets = "" Then 
     PDFsheets = "Business Plan" 
    Else 
     PDFsheets = PDFsheets & ",Business Plan" 
    End If 
    End If 

    If CheckBox3.Value = True Then 
     If PDFsheets = "" Then 
     PDFsheets = "Deal Worksheet" 
    Else 
     PDFsheets = PDFsheets & ",Deal Worksheet" 
    End If 
    End If 


    If CheckBox4.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "Deal Recap" 
    Else 
     PDFsheets = PDFsheets & ",Deal Recap" 
    End If 
    End If 

    If CheckBox5.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "All Manager Deal Recap" 
    Else 
     PDFsheets = PDFsheets & ",All Manager Deal Recap" 
    End If 
    End If 

    If CheckBox6.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "MEC Dealership Profile" 
    Else 
     PDFsheets = PDFsheets & ",MEC Dealership Profile" 
    End If 
    End If 

    If CheckBox7.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "Loyal" 
    Else 
     PDFsheets = PDFsheets & ",Loyal" 
    End If 
    End If 

    If CheckBox8.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "Mid Loyal" 
    Else 
     PDFsheets = PDFsheets & ",Mid Loyal" 
    End If 
    End If 

    If CheckBox9.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "Non Loyal" 
    Else 
     PDFsheets = PDFsheets & ",Non Loyal" 
    End If 
    End If 

    If CheckBox10.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "Projected Incentive Report" 
    Else 
     PDFsheets = PDFsheets & ",Projected Incentive Report" 
    End If 
    End If 

    If CheckBox11.Value = True Then 
    If PDFsheets = "" Then 
     PDFsheets = "MEC" 
    Else 
     PDFsheets = PDFsheets & ",MEC" 
    End If 
    End If 

ThisWorkbook.Sheets(ary).Select 
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
    strPath & strFName, Quality:=xlQualityStandard, _ 
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 

ActiveWindow.SelectedSheets(1).Select 


End Sub 

回答

0

未经测试,但我认为这应该做你所需要的。许多重复的逻辑被分解为一个单独的子。

Option Explicit 

Private Sub chbxEnter_Click() 

    Dim pdfSheets As String 

    pdfSheets = "" 

    AddASheet CheckBox1, pdfSheets, "Approval Form" 
    AddASheet CheckBox2, pdfSheets, "Business Plan" 
    AddASheet CheckBox3, pdfSheets, "Deal Worksheet" 
    AddASheet CheckBox4, pdfSheets, "Deal Recap" 
    AddASheet CheckBox5, pdfSheets, "All Manager Deal Recap" 
    AddASheet CheckBox6, pdfSheets, "MEC Dealership Profile" 
    AddASheet CheckBox7, pdfSheets, "Loyal" 
    AddASheet CheckBox8, pdfSheets, "Mid Loyal" 
    AddASheet CheckBox9, pdfSheets, "Non Loyal" 
    AddASheet CheckBox10, pdfSheets, "Projected Incentive Report" 
    AddASheet CheckBox11, pdfSheets, "MEC" 


    ThisWorkbook.Sheets(Split(pdfSheets, ",")).Select 
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
     strPath & strFName, Quality:=xlQualityStandard, _ 
     IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True 

    ActiveWindow.SelectedSheets(1).Select 
End Sub 

'utility: add a sheet name if a checkbox is checked 
Sub AddASheet(cb, ByRef pdfSheets As String, shtName As String) 
    If cb.Value Then 
     pdfSheets = pdfSheets & IIf(Len(pdfSheets) > 0, ",", "") & shtName 
    End If 
End Sub