1

I'm using Excel 2021. I have a VBA macro that can be started either by menu command or keyboard shortcut.

It had previously been working fine both ways. But while adding to the macro, I found that when starting it by keyboard command, it stopped partway through with no error message, and also didn't go into the error handler. It still works as expected if I start the macro with the menu command.

If I put a breakpoint on the beginning and step through, it works fine even if I start it by the keyboard command.

By putting a breakpoint near the end and testing, and then moving the breakpoint further up and testing each time, I found that it was stopping on a line that opens an Excel workbook.

The workbook is very small and simple. Text data only; no formulas, no objects. Ten columnns by 12 rows. I open it with UpdateLinks:=False just on general principle; it has no links.

It just stops on that line with no error message, and doesn't go into the error handler. Again, that line procesess as expected if I start the macro by the menu command.

Any suggestions?

UPDATE:

I now have a much simplified test case, and the problem happens with a new workbooks. Here are the simple repro steps:

  1. Create a new default workbook and save it as Book1.xlsx. Close Book1.xlsx.
  2. In the same folder, create a new macro-enabled workbook called Text.xlsm.
  3. Open the VB editor. Add a standard module to Test.xlsm. Allow it to be named the default Module1.
  4. Add this code to Module1:

.

Sub Main()
    Call Application.Workbooks.Open(Filename:=ThisWorkbook.Path & "\Book1.xlsx")    
    MsgBox "Hi"
End Sub

Go to Developer > Macros > Options, and add this keyboard shortcut to Main:

Add keyboard shortcut to Main

Now, making sure Book1.xlsx is closed, press Ctrl+Shift+R. Note that the msgbox does NOT run.

Close Book1 again. Put a breakpoint on Sub Main. Run the keyboard shortcut again. Note that this time you DO get the msgbox.

If Book1 is already open when you run the keyboard shortcut, you do get the msgbox.

Any suggestions?

5
  • We would need to see more code, because that is probably where the symptom is, but not the cause. Can you please paste the full routine? Commented May 28 at 6:07
  • And also, very importantly, can you include the error handler and note if similar error handling is used elsewhere in the codebase? VBA error handling is something to avoid as much as possible. Commented May 28 at 6:11
  • Consider setting your keyboard shortcut with the Onkey method, rather than relying on the UI, so that there is then "programmatic equivalence" of both means of code invocation. Commented May 28 at 14:19
  • @SpectralInstance Thanks, I tried OnKey and the same problem happened. I assigned it to a different keyboard command, "^+S", to make sure the original command defined in the UI wasn't involved. After running OnKey, it started up like usual and then quit in at the Workbooks.Open line. Commented May 28 at 18:31
  • Update: I have a much simplified test case above, and the problem happens with new workbooks. Commented May 28 at 21:06

1 Answer 1

1

I believe I've found the solution.

If you start a VBA macro with any keyboard combination that includes the Shift key, it will stop on any Open statement, after opening the indicated file.

I tested changing the keyboard shortcut to just Ctrl+r (no shift key), and it worked correctly--the msgbox did display.

As far as I can tell, Microsoft doesn't consider this a bug. Instead it seems to have something to do with being able to open a workbook without its auto_open macros running by holding down the Shift key while opening it.

My source:

Excel VBA: Macro hangs after opening a document when run via shortcut key, but runs perfectly from VB editor

Sign up to request clarification or add additional context in comments.

1 Comment

Good find, Greg. I have deleted my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.