vArray if else宏

时间:2018-01-17 16:02:29

标签: excel vba excel-vba

我正在尝试创建一个宏,根据复选框状态将不同的页面添加到pdf。这就是我现在所拥有的。

Sub ToyotaMO()
'
' ToyotaMO Macro
'

    Sheets("TC").Visible = True
    Sheets("BS").Visible = True
    Sheets("ToyotaMO").Activate
        If Sheets("ACM").OLEObjects("Toyota").Object.Value = True Then
            vArray = Array("ToyotaMO", "TC", "BS")
        Else
            vArray = Array("Proposal", "TC")
        End If
    ThisWorkbook.Sheets(vArray).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ThisWorkbook.Path & "\" & ActiveSheet.Range("C5").Value & " Toyota Material Only ACM Proposal" & Format(Date, " MMDDYY") & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True
    Sheets("ToyotaMO").Activate
    Range("A1").Select
    Sheets("TC").Visible = False
    Sheets("BS").Visible = False
    Sheets("ACM").Select
    Range("B1").Select
End Sub

宏适用于所有其他形式,唯一的区别是如果选中复选框,我需要将工作表“BS”添加到PDF。 VBA总是停留在ThisWorkbook.Sheets(vArray).Select

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

尝试以下操作,这将循环遍历您的阵列并执行您期望的操作:

Sub ToyotaMO()
'
' ToyotaMO Macro
'

    Sheets("TC").Visible = True
    Sheets("BS").Visible = True
    Sheets("ToyotaMO").Activate
        If Sheets("ACM").OLEObjects("Toyota").Object.Value = True Then
            vArray = Array("ToyotaMO", "TC", "BS")
        Else
            vArray = Array("Proposal", "TC")
        End If

    For i = LBound(vArray) To UBound(vArray)
        ThisWorkbook.Sheets(vArray(i)).Select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        ThisWorkbook.Path & "\" & ActiveSheet.Range("C5").Value & " Toyota Material Only ACM Proposal" & Format(Date, " MMDDYY") & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
        Sheets("ToyotaMO").Activate
        Range("A1").Select
        Sheets("TC").Visible = False
        Sheets("BS").Visible = False
        Sheets("ACM").Select
        Range("B1").Select
    Next i
End Sub