简体   繁体   English

打开另一个工作簿后Excel不完成宏

[英]Excel Doesn't Complete Macro after Opening Another Workbook

I'm trying to get VBA to fire the commands 我正试图让VBA解雇命令

sImportFilePath = Application.GetOpenFilename(FileFilter:= _
"Excel Files (*.xls), *.xls", Title:="Choose The Source File")
Application.Workbooks.Open (sImportFilePath)
sImportFileName = FunctionGetFileName(sImportFilePath)

And they work when I step through the function, but when I use the hotkey Ctrl+Shift+F or any other hotkey, the Application.Workbooks.Open command works but it navigates the the new Excel document, then doesn't do anything. 当我单步执行该功能时它们会起作用,但当我使用热键Ctrl + Shift + F或任何其他热键时, Application.Workbooks.Open命令可以工作,但它会导航新的Excel文档,然后不会执行任何操作。 However, when I open "Macros" in the developer tab, select my macro, and click "Run" everything is runs fine. 但是,当我在开发人员选项卡中打开“宏”时,选择我的宏,然后单击“运行”,一切运行正常。

I actually ran into this exact problem myself and finally found a solution to my problem. 我实际上遇到了这个问题,最终找到了解决问题的方法。

It is the Shift button in the keyboard shortcut you are using to call your code. 它是用于调用代码的键盘快捷键中的Shift按钮。

Apparently there a feature in Excel specifically designed to prevent code from running when a workbook is opened and the Shift key is pressed, but unfortunately this also impacts opening workbooks with the Workbook.Open method via VBA. 显然,Excel中有一项功能专门用于防止代码在打开工作簿并按下Shift键时运行,但不幸的是,这也会影响使用VBA打开Workbook.Open方法的Workbook.Open This was mentioned in KB Article 555263 , applying to Excel 2000 & 2003, but I encountered the same problem in Excel 2010, so I imagine it is also impacting 2007 as well. 这在KB Article 555263中提到,适用于Excel 2000和2003,但我在Excel 2010中遇到了同样的问题,所以我想它也影响了2007。

This occurs specifically when you try to open a workbook via code very early in the program. 当您尝试在程序的早期通过代码打开工作簿时,会发生这种情况。 If the Workbook.Open call is reached in code before you have had sufficient time to actually let go of the Shift button, Excel is interpreting that as an attempt to block code from running and aborts the process. 如果在您有足够的时间实际放开Shift按钮之前已在代码中达到Workbook.Open调用,则Excel会将其解释为阻止代码运行并中止该过程。 And there are no error messages or anything that I have found. 并且没有错误消息或我找到的任何内容。 It just stops abruptly. 它突然停止了。

The workaround/fix is to force the code to wait for the Shift key to be released before issuing the Workbook.Open command. 解决方法/修复是强制代码在发出Workbook.Open命令之前等待释放Shift键。

Per the article, just add this code to your macro and that should do it: 根据文章,只需将此代码添加到您的宏,并应该这样做:

'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 Filename:="C:\MyPath\MyFile.xlsx"
End Sub

(NOTE: This code is for 32-bit versions of Excel. 64-bit versions will need to use the PtrSafe attribute on the Declare statement). (注意:此代码适用于32位版本的PtrSafe位版本需要在Declare语句中使用PtrSafe属性)。

If you don't want to add the extra code, then your only other options are to not use Ctrl + Shift + Some Letter to launch a macro, or to put the Workbook.Open command later in the macro (not right at the beginning) in order to give yourself time to release the Shift button after starting it. 如果您不想添加额外的代码,那么您唯一的其他选项是不使用Ctrl + Shift + Some Letter来启动宏,或者稍后在宏中放置Workbook.Open命令(不在开头) )以便在启动后给自己时间释放Shift按钮。

Just adding a single call to "DoEvents" before calling Workbooks.Open will already do the trick. 只需在调用Workbooks.Open之前添加一个“DoEvents”调用就可以了。 So the folloiwng snippet will work: 所以folloiwng片段将起作用:

DoEvents
Workbooks.Open Filename:="C:\MyPath\MyFile.xlsx"

Question did you have a line of code selecting multiple tabs anytime before this line of code? 问题你在这行代码之前的任何时候都有一行代码选择了多个标签吗? I have found that this is sort of like holding in the shift key the entire time they're selected. 我发现这有点像在移动键中选择它们的整个时间。 To resolve this I had the macro ungroup (single select) a tab and the macro began working then. 为了解决这个问题,我有一个宏取消组合(单选)一个选项卡,然后宏开始工作。

A simple workaround that did it for me is to assign the VBA to a shortcut key without a shift. 为我做的一个简单的解决方法是将VBA分配给没有移位的快捷键。 I'm not a big fan of doing so because there are many more conflicts with dafault Excel shortcuts. 我不是这样做的忠实粉丝,因为与dafault Excel快捷方式存在更多冲突。 But there's a few available as of Excel 2010 based on this article : Ctrl+e, Ctrl+j, Ctrl+m, Ctrl+q. 但是根据本文的Excel 2010中有一些可用:Ctrl + e,Ctrl + j,Ctrl + m,Ctrl + q。

Temporary solution found on an French forum: use the below ForEachWinDoEvents before activating the workbook of your choice. 在法语论坛上找到的临时解决方案:在激活您选择的工作簿之前,请使用以下ForEachWinDoEvents

Sub Test()
    Application.ScreenUpdating = False
    Set w1 = Workbooks.Add(xlWBATWorksheet)
    Set w2 = Workbooks.Add(xlWBATWorksheet)
    Set w3 = Workbooks.Add(xlWBATWorksheet)
    Application.ScreenUpdating = True
    ForEachWinDoEvents
    w2.Activate
End Sub

Sub ForEachWinDoEvents()
Dim win As Window
  For Each win In Application.Windows
    DoEvents
  Next win
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM