Macro works standalone but breaks in Personal.xlsb

Fishboy

Well-known Member
Joined
Feb 13, 2015
Messages
4,267
Hi all,

I am hoping one of the gurus can step in and explain something which I cannot get my head around.

I have been helping a fellow user to automate the selecting of all visible sheets in a workbook and piping them out as a PDF in another thread here:

http://www.mrexcel.com/forum/excel-questions/940265-selecting-all-sheets-using-personal-xlsb.html

I have managed to put together the following code which does the job just fine when you manually run the macro:

Rich (BB code):
Sub Export_Visible_Sheets_To_PDF_Original()
' Defines variables
Dim wbk As Workbook
Dim ws As Worksheet
Dim SaveName As String, ArraySheets() As String
Dim x As Variant


' Disable screen updating to reduce flicker
Application.ScreenUpdating = False
     
    With Worksheets(1)
        ' For each open workbook
        For Each wbk In Workbooks
            ' If the workbook name is not the same as the .xlsb name then...
            If wbk.Name <> ThisWorkbook.Name Then
                ' Activate the workbook
                wbk.Activate
                ' Clear the variable ArraySheets
                ReDim ArraySheets(x)
                ' Update variable SaveName as the name of the workbook
                SaveName = Left(wbk.Name, (InStrRev(wbk.Name, ".", -1, vbTextCompare) - 1))
                ' For each sheet in the workbook
                For Each ws In wbk.Worksheets
                    ' If the worksheet is visible then...
                    If ws.Visible = xlSheetVisible Then
                        ' Re-dims the variable ArraySheets preserving any existing sheet names
                        ReDim Preserve ArraySheets(x)
                        ' Add the sheet name to variable ArraySheets
                        ArraySheets(x) = ws.Name
                        ' Increase variable x by 1 for each sheet added
                        x = x + 1
                    End If
                ' Check next worksheet
                Next ws
                ' Select all sheets in ArraySheets
                Sheets(ArraySheets).Select
                ' Export the selected sheets as a single PDF to the specified folder as "SaveName".pdf
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                "C:\TestFolder\" & SaveName & ".pdf"
            End If
        ' Check next workbook
        Next wbk
    End With
    
' Re-enable screen updating
Application.ScreenUpdating = True


End Sub

So far so good, except for the automation part. The idea is to get it to run automatically whenever another workbook is opened. I utilized my Google-Fu and found this awesome post in another thread on these forums:

http://www.mrexcel.com/forum/excel-...workbook-event-workbook-open.html#post2411684

This prompted me to add the following code to the ThisWorkbook module of Personal.xlsb:

Rich (BB code):
Private WithEvents AppEvents As Application
 
Private Sub Workbook_Open()
 
    Set AppEvents = Application
 
End Sub


Rich (BB code):
Private Sub AppEvents_WorkbookOpen(ByVal Wb As Workbook)
 
    If Not Wb Is Me Then
        Call Export_Visible_Sheets_To_PDF(Wb)
    End If
 
End Sub

Then I also added my original macro to the ThisWorkbook module and amended it as follows:

Rich (BB code):
Private Sub Export_Visible_Sheets_To_PDF(Wb As Workbook)
' Defines variables
Dim wbk As Workbook
Dim ws As Worksheet
Dim SaveName As String, ArraySheets() As String
Dim x As Variant


' Disable screen updating to reduce flicker
Application.ScreenUpdating = False
     
    With Worksheets(1)
        ' For each open workbook
        For Each wbk In Workbooks
            ' If the workbook name is not the same as the .xlsb name then...
            If wbk.Name <> ThisWorkbook.Name Then
                ' Activate the workbook
                wbk.Activate
                ' Clear the variable ArraySheets
                ReDim ArraySheets(x)
                ' Update variable SaveName as the name of the workbook
                SaveName = Left(wbk.Name, (InStrRev(wbk.Name, ".", -1, vbTextCompare) - 1))
                ' For each sheet in the workbook
                For Each ws In wbk.Worksheets
                    ' If the worksheet is visible then...
                    If ws.Visible = xlSheetVisible Then
                        ' Re-dims the variable ArraySheets preserving any existing sheet names
                        ReDim Preserve ArraySheets(x)
                        ' Add the sheet name to variable ArraySheets
                        ArraySheets(x) = ws.Name
                        ' Increase variable x by 1 for each sheet added
                        x = x + 1
                    End If
                ' Check next worksheet
                Next ws
                ' Select all sheets in ArraySheets
                Sheets(ArraySheets).Select
                ' Export the selected sheets as a single PDF to the specified folder as "SaveName".pdf
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                "C:\TestFolder\" & SaveName & ".pdf"
            End If
        ' Check next workbook
        Next wbk
    End With
    
' Re-enable screen updating
Application.ScreenUpdating = True


End Sub


When another workbook is opened my amended macro is triggered as expected however now for some reason when using it via the open event it breaks on the selecting sheets part...

Rich (BB code):
                ' Select all sheets in ArraySheets
                Sheets(ArraySheets).Select

...with an error "Run-time error '1004': Select methods of Sheets class failed"

Can anyone suggest why it works as a standalone macro, selecting the array of sheet without issue but falls over in the amended version?

Any insight would be greatly appreciated.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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