VBA - Macro displaying error when executed

Lee Rabbit

New Member
Joined
Apr 30, 2020
Messages
43
Office Version
  1. 2010
Platform
  1. Windows
Hi All,

Been trying to fix this for a couple of days but not having much luck.

I execute the following VBA via a command button and it works perfectly, however, on the sheets that are selected in the array, there are buttons.

Herein lies the problem. These buttons are temperamental and resize when the VBA runs.

VBA Code:
Sub save_sh()

    Dim i As Long, c As Long
    Dim SheetArray() As String
   
    With ActiveSheet.ListBoxSh
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve SheetArray(c)
                SheetArray(c) = .List(i)
                c = c + 1
               
           
            End If
           
       
        Next i
       
       
   
    End With
   
   
    If c = 0 Then
        MsgBox "Nothing was selected.. try again!"
    Else
           
    'set the save folder (change accordingly)
    Dim saveFolder As String
    saveFolder = "C:\PDF\EMAIL\"
   
    'set the saveas filename (change accordingly)
    Dim saveFilename As String
    saveFilename = Range("D22").Value & "_" & "WEEK" & "_" & Range("D23").Value & "_" & "INVOICES" & ".pdf" _
   
    'select the specified sheets
    Sheets(SheetArray()).Select
   
    'export the selected sheets to PDF
   
    With ActiveSheet.PageSetup
    .Orientation = xlLandscape
    .PrintArea = "$A$1:$M$30"
    .Zoom = False
    .FitToPagesTall = 1
    .FitToPagesWide = 1
   
     
    End With
   
       
    ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=saveFolder & saveFilename
       
    'de-select the selected sheets
    Sheets(SheetArray(0)).Select
   
    MsgBox "THE WEEKLY INVOICE REPORT HAS BEEN SAVED AS PDF", vbExclamation
    ThisWorkbook.Sheets("MASTER SHEET").Activate
   
    End If
   
End Sub


To overcome the button resizing, I thought I'd run a WorkSheet_activate VBA. The buttons are grouped on the worksheet.

VBA Code:
Private Sub Worksheet_Activate()

    With Me.Shapes("Group 2")
        .Width = 514
       
    End With

End Sub

I thought this would solve the issue but now I get a Run-Time error when I execute the code to create the PDF.

1589294325153.png


That points to this in my WorkSheet_activate code

1589294441575.png


I am confused why this is happening and any help on this matter would be gratefully received.

Is there a way I can run the code to resize the buttons that is not using the Worksheet_Activate option? Or a way to get passed this error?

Thanks in advance,
Regards,
Lee
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Select each button and right-click, use Format Control, then check the option button to not move or size with cells:

1589298022725.png
 
Upvote 0
Select each button and right-click, use Format Control, then check the option button to not move or size with cells:

View attachment 13679

Unfortunately, this is how the buttons are already set. It makes no difference the random resize of the buttons I am experiencing.

Thanks for the input though.
Regards,
Lee
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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