VBA Error code Suspected end with

jondavis1987

Active Member
Joined
Dec 31, 2015
Messages
443
Office Version
  1. 2019
Platform
  1. Windows
Not sure what i'm doing wrong but i am trying to save different worksheets as PDFs. When i try to run this macro it highlights End sub and says suspected end with.


Code:
Sub save_Worksheets()


    Dim WB As Workbook
    Dim fName As String
    




'   Export T-88to PDF
    
    With srcWB
  Dim LocationName As String
  Dim WS1 As Worksheet
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     WS1 = "T-88"
     WS1.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\" & LocationName & "\Soils\" & fName & " T-88", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True




'   Export T-89 & T-90 to PDF
     
         With srcWB
  Dim WS2 As Worksheet
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     WS2 = "T-89 & T-90"
     WS2.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\\" & LocationName & "\Soils\" & fName & " T-89 & T-90", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True




'   Export T-99 to PDF


    With srcWB
  Dim WS3 As Worksheet
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     WS3 = "T-99"
     WS3.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\\" & LocationName & "\Soils\" & fName & " T-99", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


'   Export Report to PDF
    With srcWB
  Dim WS4 As Worksheet
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     WS4 = "Report"
     WS4.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\\" & LocationName & "\Soils\" & fName & " Report", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True










End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You need to include your end with statement at the end of each section (above each of the Export report to PDF comment), then again above end sub
 
Upvote 0
There's a few things missing from that code, e.g. wbSrc is never set, so I've had to make some assumptions but give this a try.
Code:
Sub save_Worksheets()
Dim srcWB As Workbook
Dim fName As String
Dim LocationName As String
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim WS3 As Worksheet
Dim WS4 As Worksheet

    Set srcWB = ActiveWorkbook
    
    With srcWB
        ' Export T-88to PDF
        fName = Range("'Report'!J5").Value
        LocationName = Range("'Report'!C6").Value
        Set WS1 = .Sheets("T-88")
        WS1.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                "C:\Users\jdavis\Dropbox\Quality Control\Jobs\" & LocationName & "\Soils\" & fName & " T-88", Quality:=xlQualityStandard, _
                                includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True



        '   Export T-89 & T-90 to PDF
        fName = Range("'Report'!J5").Value
        LocationName = Range("'Report'!C6").Value
        Set WS2 = .Sheets("T-89 & T-90")
        WS2.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                "C:\Users\jdavis\Dropbox\Quality Control\Jobs\\" & LocationName & "\Soils\" & fName & " T-89 & T-90", Quality:=xlQualityStandard, _
                                includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


        '   Export T-99 to PDF
        fName = Range("'Report'!J5").Value
        LocationName = Range("'Report'!C6").Value
        Set WS3 = .Sheets("T-99")
        WS3.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                "C:\Users\jdavis\Dropbox\Quality Control\Jobs\\" & LocationName & "\Soils\" & fName & " T-99", Quality:=xlQualityStandard, _
                                includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


        '   Export Report to PDF
        fName = Range("'Report'!J5").Value
        LocationName = Range("'Report'!C6").Value
        Set WS4 = .Sheets("Report")
        WS4.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                "C:\Users\jdavis\Dropbox\Quality Control\Jobs\\" & LocationName & "\Soils\" & fName & " Report", Quality:=xlQualityStandard, _
                                includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

    End With

End Sub
 
Upvote 0
Thank you for the help with rearranging it! I'm not very good with macros but I ended up getting it fixed before I came back to this. I ended up going with the following. I had never tried to make a macro that wasn't using the active sheet. I guess i was kinda on the right path.

Code:
Sub save_Worksheets()


    Dim WB As Workbook
    Dim fName As String
    




'   Export T-88to PDF
    
    With srcWB
  Dim LocationName As String
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     Worksheets("T-88").Activate
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\" & LocationName & "\Soils\" & fName & " T-88", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


End With
'   Export T-89 & T-90 to PDF
     
         With srcWB
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     Worksheets("T-89 & T-90").Activate
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\" & LocationName & "\Soils\" & fName & " T-89 & T-90", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


End With
'   Export T-99 to PDF


    With srcWB
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     Worksheets("T-99").Activate
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\" & LocationName & "\Soils\" & fName & " T-99", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
'   Export Report to PDF
    With srcWB
     fName = Range("'Report'!J5").Value
     LocationName = Range("'Report'!C6").Value
     Worksheets("Report").Activate
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\jdavis\Dropbox\Quality Control\Jobs\" & LocationName & "\Soils\" & fName & " Report", Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True








End With




End Sub
 
Upvote 0

Forum statistics

Threads
1,215,409
Messages
6,124,737
Members
449,185
Latest member
hopkinsr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top