2014-01-15 3 views
4

템플릿을 열고 파일 이름을 묻고 파일을 저장하는 간단한 작은 Excel 매크로가 있습니다. Microsoft VBA 창에서 문제없이 실행되지만 Excel에서 바로 가기 키를 사용하면 파일이 열리지 만 입력 상자는 표시되지 않습니다.매크로가 바로 가기 키에서 실행될 때 입력 상자가 나타나지 않습니다.

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 
+0

전에 DoEvents 추가 예는 잘 알려진 문제 (내가 그렇게 희망)입니다. 왜 이런 일이 일어날 지 알려주세요 :) –

+0

BTW 내가 "그렇게되기를 바란다"는 말은 많은 사람들이 그 한계를 알고 있기를 희망한다고 ... –

답변

4

Excel에서 시프트 키 매크로를 실행하지 않고 파일을 열 수있는 통합 문서를 여는 데 사용되며,이 매크로의 나머지 부분을 실행 방해한다.

기사 엑셀이 통합 문서 시프트 키를 누른 동안 사용자 인터페이스에서 열릴 때에 Auto_Open 및 Workbook_Open 코드를 실행하지 않도록 설계되어

MSDN에서. 불행히도이 (원하는) 동작은 VBA 코드를 통해 통합 문서를 열 때 적용됩니다. (링크가 죽었을 경우) 위의 링크에서

해상도

이 문제 (Windows의 경우에만 적용 ® 플랫폼)에 대한 해결 방법은 시프트 키를 누르면 여부를 감지하고 그것을 위해 기다리는 것입니다 Workbooks.Open 명령을 실행하기 전에 발매 예정 :

'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 

편집을

방금 ​​실험했는데 아래가 제대로 작동하는 것 같습니다. 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 
+0

Works! 고맙습니다! –

관련 문제