Create PDF VBA Code not working

grabrail

Board Regular
Joined
Sep 6, 2010
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Having a bit of trouble with some VBA i Have.

The workbook is made up of multiple worksheets, and the code is designed to look at all sheets that have an entry in Cell C6, if there is an entry then it will collate that worksheet, and check the rest. once all worksheets with an entry in cell c6 are obtained, a pdf report is made from these selected worksheets.

this used to work, but for some reason its not now.

the code is as follows:

VBA Code:
Private Sub createpdf_Click()

  Dim ws            As Worksheet
  Dim strWS         As String
  Dim strFolder     As String
  Dim varRet        As Variant
  
  Const cstrDel As String = ","
  
  'getting information about the sheets
  For Each ws In Worksheets
    If ws.Range("C6").Value <> "" Then
      strWS = strWS & ws.Name & cstrDel
    End If
  Next ws
  
  'getting the folder to which to save to
  With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = -1 Then
      strFolder = .SelectedItems(1) & "\"
    Else
      Exit Sub
    End If
  End With
  'getting the filename to save
  varRet = Application.GetSaveAsFilename(InitialFileName:=strFolder, _
            fileFilter:="PDF Files (*.pdf), *.pdf", _
            Title:="Save Report to Directory")
  'if Cancel is chosen varRet will returm False
  If varRet <> False Then
    'group the worksheets
    
    Worksheets(Split(Left(strWS, Len(strWS) - 1), cstrDel)).Select
    'print to PDF
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=varRet, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True  'display after creation?
  End If
End Sub

When the button that this code is attached to is pressed (on the first worksheet) the first pop up appears asking for folder location, then when you click ok, the next pop up appears asking for file name, when you click ok, it then errors, with

Select method of Worksheet class failed.

and the following line in the code is highlighted

Worksheets(Split(Left(strWS, Len(strWS) - 1), cstrDel)).Select

Can anybody shed any light on why this might be happening
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
It works ok for me.
Do you have any hidden sheets that have text in C6? You'd have to make them visible
 
Upvote 1
Solution
It works ok for me.
Do you have any hidden sheets that have text in C6? You'd have to make them visible
Hi, yes just resolved it, I had a hidden sheet that had some data on it, that I wasnt aware. Removed the data and works great now.
 
Upvote 0

Forum statistics

Threads
1,214,635
Messages
6,120,660
Members
448,975
Latest member
sweeberry

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