ElseIf语句错误。 VBA宏保存为pdf

时间:2014-10-10 16:44:25

标签: excel excel-vba if-statement vba

如果我打印到PDF,我不喜欢我的pdf的质量。但是当我保存为PDF时,它更加清晰。所以我尝试了这个VBA,但是我一直在ElseIf语句上遇到错误。这究竟是什么问题?

If Range("AE2").Value = 1 Then ActiveSheet.Range("A1:M55").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
ElseIf Range("AE2").Value = 2 Then ActiveSheet.Range("A1:M108").ExportAsFixedFormat Type:=xlTypePDF, Filename:=
ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
ElseIf Range("AE2").Value = 3 Then ActiveSheet.Range("A1:M162").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End If

感谢您仔细研究

3 个答案:

答案 0 :(得分:2)

您在第四行代码后缺少下划线(换行符)。你也应该在Then之后打破这一行。尝试更改您的代码,如:

If Range("AE2").Value = 1 Then
    ActiveSheet.Range("A1:M55").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False
ElseIf Range("AE2").Value = 2 Then
    ActiveSheet.Range("A1:M108").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False
ElseIf Range("AE2").Value = 3 Then
    ActiveSheet.Range("A1:M162").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False
End If

答案 1 :(得分:1)

我现在知道了。谢谢你的帮助。这是代码:

If Range("AE2").Value = 1 Then
ActiveSheet.Range("A1:M55").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
ElseIf Range("AE2").Value = 2 Then
ActiveSheet.Range("A1:M108").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
ElseIf Range("AE2").Value = 3 Then
ActiveSheet.Range("A1:M162").ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End If

答案 2 :(得分:0)

重构后,代码更容易阅读:

Dim exportRng As String

Select Case Range("AE2")
    Case 1
        exportRng = "A1:M55"

    Case 2
        exportRng = "A1:M108"

    Case 3
        exportRng = "A1:M162"

End Select

ActiveSheet.Range(exportRng).ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ActiveWorkbook.Path & Application.PathSeparator & "\" & Range("A23").Value & "\" & Range("AC6").Value & "\" & Range("AD6").Value & "\" & Range("AC3").Value & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False