Macro Runs From Editor But Not From The Worksheet

JLouis

Active Member
Joined
Jan 1, 2004
Messages
295
Office Version
  1. 365
Platform
  1. Windows
I have a macro that runs from the VBA editor but crashes if run from a button on a worksheet. It is an out of range error referring to a userform that is unloaded at the start of the macro. I unloaded the userform and made the workbook active at the beginning of the macro without success. The error occurrs in the worksheet calculate event routine.

Any insight is helpful. Let me know if additional info is needed.

VBA Code:
Sub PFF()
    Windows("Single Game.xlsm").Activate
    Sheets("single").Select
         Dim path As String
    path = ThisWorkbook.path
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Unload UserForm9
       Workbooks.Open Filename:=(path & "\exports\single dfs.xlsx")
           ActiveWorkbook.Save
    ActiveWindow.Close
PFFNewestFile
 ActiveSheet.Name = "1"
     ActiveWorkbook.SaveAs Filename:=path & "\Exports\Single DFS.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        PFFImportPic
ActiveWorkbook.Save
    ActiveWindow.Close
 Application.ScreenUpdating = True

End Sub

The error occurs here Private Sub Worksheet_Calculate()

VBA Code:
If Sheets("SINGLE").Range("Q134").Value > 0 Then UserForm9.Frame10.BackColor = &HC0FFC0
  If Sheets("SINGLE").Range("Q134").Value = "" Then UserForm9.Frame10.BackColor = &HC0FFFF
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
The subscript out of range error can only be referring to the Sheets("SINGLE") part(s) of the code, which would suggest that the active workbook at the time does not have that sheet. Given that it is in a worksheet_activate event, I assume it refers to a sheet in the same workbook as the sheet with the activate event code, so you should use Thisworkbook.Sheets("SINGLE") to refer to it.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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