【问题标题】:ElseIf statement error. VBA Macro to save to pdfElseIf 语句错误。 VBA 宏保存为 pdf
【发布时间】:2014-10-11 01:12:55
【问题描述】:

如果我打印为 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

感谢您的调查

【问题讨论】:

    标签: excel if-statement vba


    【解决方案1】:

    第四行代码后缺少下划线(换行符)。你也应该在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 的其他情况
    • 我认为您缺少一些换行符。请参阅我编辑的答案。
    【解决方案2】:

    我现在知道了。谢谢你的帮助。代码如下:

    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】:

      重构后,代码更易阅读:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多