简体   繁体   English

打开新工作簿 Excel 2016 VBA 后功能区无响应

[英]Unresponsive ribbon after opening new workbook Excel 2016 VBA

I'm quite a rookie when it comes to programming, however I have been able to build a couple of useful applications in excel, which automate some of my everyday tasks.我在编程方面是个菜鸟,但是我已经能够在 excel 中构建几个有用的应用程序,这些应用程序可以自动执行我的一些日常任务。

Currently I am having an annoying issue when trying to open a new workbook using workbooks.open() in Excel 2016.目前,我在尝试使用 Excel 2016 中的workbooks.open()打开新工作workbooks.open()时遇到了一个烦人的问题。

I have made a workbook with a button which calls a userform.我制作了一个带有调用用户表单的按钮的工作簿。 Once the form is launched, the user can select to download and open several types of .xls files.表单启动后,用户可以选择下载和打开多种类型的 .xls 文件。 When the user clicks the form's OK button all the appropriate functions are called, the selected workbooks are opened and the form gets unloaded and hidden.当用户单击表单的 OK 按钮时,所有适当的函数都会被调用,选定的工作簿被打开,表单被卸载和隐藏。

However, the ribbon of the last workbook that was opened, which btw is the one that is currently active, is unresponsive and the only way to overcome this, is to ALT+TAB between open windows.但是,打开的最后一个工作簿的功能区(顺便说一下,当前处于活动状态的工作簿)没有响应,解决此问题的唯一方法是在打开的窗口之间按 ALT+TAB。

It seems like the "focus" is still on the initial workbook with the button, because if I call a Msgbox after the form is unloaded, that's where it appears.似乎“焦点”仍然在带有按钮的初始工作簿上,因为如果我在卸载表单后调用Msgbox ,它就会出现在那里。 It's worth mentioning that this happens although the initial workbook is not the one that's active!值得一提的是,尽管初始工作簿不是活动的,但会发生这种情况!

After doing some experimenting I was able to solve the issue by disabling Application.ScreenUpdating when my function is called and then re-enabling it just before the form is unloaded.在做了一些实验之后,我能够通过在调用我的函数时禁用Application.ScreenUpdating来解决这个问题,然后在卸载表单之前重新启用它。

This however only works when multiple workbooks are being opened at the same time.但是,这仅在同时打开多个工作簿时才有效。 If the user chooses to open only one workbook then the problem persists.如果用户选择仅打开一个工作簿,则问题仍然存在。 I came across a suggestion to make the userform modeless, which indeed solves the issue but creates other kinds of unwanted behavior.我遇到了一个使用户表单无模式的建议,这确实解决了问题,但会产生其他类型的不需要的行为。

A simplified version of the code which replicates the problem is as follows:复制问题的代码的简化版本如下:

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False 'solves the issue but only for multiple files - comment out to replicate the problem
If OptionButton1 Then
    Workbooks.Open ("http://www.admie.gr/fileadmin/user_upload/reports/DayAheadSchedulingUnitAvailabilities/20180602_DayAheadSchedulingUnitAvailabilities_01.xls")
Else
    Workbooks.Open ("http://www.admie.gr/fileadmin/user_upload/reports/DayAheadSchedulingUnitAvailabilities/20180603_DayAheadSchedulingUnitAvailabilities_01.xls")
    Workbooks.Open ("http://www.admie.gr/fileadmin/user_upload/reports/DayAheadSchedulingUnitAvailabilities/20180604_DayAheadSchedulingUnitAvailabilities_01.xls")
End If
Application.ScreenUpdating = True 'solves the issue but only for multiple files - comment out to replicate the problem
Unload UserForm1
UserForm1.Hide
MsgBox ActiveWorkbook.Name 'for debugging purposes - comment out to replicate the problem - the msgbox will be displayed on the workbook which called the userform, although it's not the active one
End Sub

Sub Button1_Click() 'calls the userform
UserForm1.OptionButton1.Value = True
Load UserForm1
UserForm1.Show vbModeless 'comment out to replicate the problem - solves the issue but creates unwanted behavior
End Sub

Has anyone dealt with this before?有没有人处理过这个问题?

What would you suggest?你有什么建议?

I faced the same issue on a stock excel when opening only 1 workbook.当我只打开 1 个工作簿时,我在股票 excel 上遇到了同样的问题。 Ribbons will be enabled again when adding this after Workbooks.open :在 Workbooks.open 之后添加此功能区时,将再次启用功能区:

    ThisWorkbook.Activate
    Workbooks("*openedWorkbookName*").Activate

Btw, some older code lead to runtime errors when selecting a Cell in a Worksheet which is not currently activated.顺便说一句,在当前未激活的工作表中选择单元格时,一些较旧的代码会导致运行时错误。 This helped for that issue as well.这也有助于解决这个问题。

I know that it's past years but I think this can help someone.我知道这已经过去几年了,但我认为这可以帮助某人。

I faced to this problem too.我也遇到过这个问题。 I found answer somewhere that this problem can solve by hide userform -before- open worksheet.我在某个地方找到了这个问题可以通过隐藏用户表单 -before-open 工作表来解决的答案。

So, just re-arrange commands like this.所以,只需像这样重新排列命令。

Private Sub CommandButton1_Click()
UserForm1.Hide 'or Me.Hide
If OptionButton1 Then
    Workbooks.Open ("filename.xls")
Else
    Workbooks.Open ("filename1.xls")
    Workbooks.Open ("filename2.xls")
End If
Unload UserForm1 'if you still want it
End Sub

Do you have any Add-Ins enabled on your Excel outside of the standard.您是否在 Excel 上启用了标准之外的任何加载项。 For Example I use a Utility product called ASAP and the executable files that trigger events overwrite each other when i start up my excel.例如,我使用名为 ASAP 的实用程序产品,当我启动我的 excel 时,触发事件的可执行文件会相互覆盖。 It will lock the ribbon the first time i open excel so i create a new excel everytime after i open my first excel它会在我第一次打开 excel 时锁定功能区,所以我每次打开第一个 excel 后都会创建一个新的 excel

I am using an Excel macro to create new workbooks and populate them with data (Office 365).我正在使用 Excel 宏来创建新工作簿并用数据填充它们 (Office 365)。

Public New_Wb As Workbook
Public New_Book_Name as String

Workbooks.Add
New_Book_Name = ActiveWorkbook.Name
Set New_Wb = ActiveWorkbook

I too ran into the issue that the new workbook would be partially active when the macro finished.我也遇到了宏完成后新工作簿将部分激活的问题。 I could work within the workbook cells, but the ribbon was inactive.我可以在工作簿单元格中工作,但功能区处于非活动状态。 Tabbing away and tabbing back to the new workbook activated the ribbon, but it was an annoyance.移开并移回新工作簿会激活功能区,但这很烦人。 I noticed that right clicking within the workbook was enough to activate the ribbon.我注意到在工作簿中右键单击足以激活功能区。 Weird, yeah.奇怪,是的。 So I tracked down code to force a right and then left mouse click event once the macro was finished (below).因此,我跟踪代码以在宏完成后强制执行鼠标右键单击事件(如下所示)。 It's a real kludge, but it happens so fast it's not visible, and leaves the ribbon in an active state.这是一个真正的混杂,但它发生得如此之快以至于不可见,并使功能区处于活动状态。

Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10

... after the workbook has been populated ... ...在工作簿填充后...

New_Wb.activate

mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0

mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

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

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