The input field does not appear when a macro is launched from a keyboard shortcut

I have a simple little Excel macro that opens a template, asks for the file name and saves the file. It starts from the Microsoft VBA window without problems, but when the shortcut key is used in Excel, it opens the file but does not show the input field.

Sub NewCommentSheet()
'
' NewCommentSheet Macro
' Opens the Comments and Recheck template. A dialog box asks for the data module name,
' which is then used for the filename of the new comment sheet.
'
' Keyboard Shortcut: Ctrl+Shift+N
'
    'Opens the file

    Workbooks.Open Filename:= _
        "C:\Users\Kelly.Keck\Documents\Projects\SBIR\QA Notes\Comments and Recheck Template.xlsx"

    'Defines variables. moduleName comes from the input box. newFileName uses moduleName _
    to create a filename: "Comments for [moduleName].xslx"

    Dim moduleName As String
    Dim newFileName As String

    'Asks the user for the data module name--default is set as the common portion of _
    the filename.

    moduleName = Application.InputBox(Prompt:="Enter the name of the data module.", _
    Title:="Data Module Title", Default:="DMTitle-")

    'Checks to see if input was canceled. If canceled, ends the macro to avoid saving with an incorrect filename.

    If moduleName = "False" Then End

    'Saves file with the new name.

    newFileName = "Comments for " & moduleName & ".xslx"
    ActiveWorkbook.SaveAs Filename:=newFileName

End Sub
+4
source share
1 answer

A key Shiftin Excel is used to open a workbook to open a file without running macros, and this makes it difficult to run the rest of the macro.

From MSDN article

Excel , Auto_Open Workbook_Open , , shift. , () VBA.

( , )

( Windows ®) , , . :

'Declare API
Declare Function GetKeyState Lib "User32" (ByVal vKey As Integer) As Integer
Const SHIFT_KEY = 16

Function ShiftPressed() As Boolean
    'Returns True if shift key is pressed
    ShiftPressed = GetKeyState(SHIFT_KEY) < 0
End Function

Sub Demo()
    Do While ShiftPressed()
        DoEvents
    Loop
    Workbooks.Open = "C:\My Documents\ShiftKeyDemo.xls"
End Sub

, , , . DoEvents Workbooks.Open

Sub NewCommentSheet()
    Dim moduleName As String
    Dim newFileName As String

    DoEvents

    Workbooks.Open Filename:= _
        "C:\book1.xlsx"

    moduleName = Application.InputBox(Prompt:="Enter the name of the data module.", _
    Title:="Data Module Title", Default:="DMTitle-")

    If moduleName = "False" Then End

    'Saves file with the new name.

    newFileName = "Comments for " & moduleName & ".xslx"
    ActiveWorkbook.SaveAs Filename:=newFileName
End Sub
+4

All Articles