[英]ElseIf statement error. VBA Macro to save to pdf
如果我打印为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
感谢您仔细研究
您在代码的第四行之后缺少下划线(换行符)。 另外,您还应在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
我现在知道了。 谢谢你的帮助。 这是代码:
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
重构后,代码更容易阅读:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.