Macro runs fine via button click event but stops via using Macro shortcut

pizzaboy

Board Regular
Joined
Mar 23, 2015
Messages
56
Office Version
  1. 2019
Platform
  1. Windows
As per title...

What I am trying to do is copy a worksheet from another file.

I've stripped all of the variable declarations and all other non-relevant code in the example below, just know that it does work via the button click event.

When I run the code via pressing the Macro shortcut, the file opens successfully but the code just stops running. No errors. No nothing.

I put a breakpoint where it actually performs the copy, but it never gets there?

That pathetic looking DoEvents was very hopeful, thinking that maybe the file opened too slowly...

Curiously... running the macro directly from the Macros panel, I get a "Run-time error '9': Subscript out of range" error where I try to close Window(file).Close. The file appeared to have automatically been closed?! Commenting out that line and the code ran as it should...


Code:
Option Explicit


Private Sub btnQuotes_Refresh_Click()
    Call Quotes_Refresh
End Sub

Public Sub Quotes_Refresh()
'
' Keyboard Shortcut: Ctrl+Shift+U
'
    Workbooks.Open ("" & QUOTES_DIRECTORY & file & "")
    DoEvents


    
    Sheets(ws).Move After:=Workbooks("SE.xlsm").Sheets(position)



    Windows(file).Close
End Sub


Any help would be very much appreciated.

Thanks.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
When I run the code via pressing the Macro shortcut, the file opens successfully but the code just stops running. No errors. No nothing.

Weird behavior, but I was able to get it to run reliably from the shortcut by putting the DoEvents before the workbook open statement.

Curiously... running the macro directly from the Macros panel, I get a "Run-time error '9': Subscript out of range" error where I try to close Window(file).Close.

That's not how you close an open workbook. See below for the correct method.

Code:
Public Sub Quotes_Refresh()
'
' Keyboard Shortcut: Ctrl+Shift+U
'
    Dim WB As Workbook

    DoEvents
    Set WB = Workbooks.Open(QUOTES_DIRECTORY & file)

    Sheets(WS).Move After:=Workbooks("SE.xlsm").Sheets(Position)
    WB.Close False
End Sub
 
Upvote 0
Create a reference to the workbook you are opening and use that in the subsequent code to copy from and to close the workbook.
Code:
Option Explicit


Private Sub btnQuotes_Refresh_Click()
    Call Quotes_Refresh
End Sub

Public Sub Quotes_Refresh()
'
' Keyboard Shortcut: Ctrl+Shift+U
'
Dim wbQuote As Workbook

    Set wbQuote = Workbooks.Open(QUOTES_DIRECTORY & file)
    
    wbQuotes.Sheets(ws).Move After:=Workbooks("SE.xlsm").Sheets(position)

    wbQuote.Close False

End Sub
 
Upvote 0
Thanks rlv01!

I used your initialization code to reference the workbook, though the file is automatically getting closed. I got an automation error so I had to comment it out...

Putting the DoEvents above the open call strangely solved the issue!

Not sure if its because I ran the macro on a different installation of Excel (another pc), as I tried uninstalling Excel from my main machine due to the automation error and now its refusing to successfully install :(
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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