简体   繁体   中英

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.

Currently I am having an annoying issue when trying to open a new workbook using workbooks.open() in Excel 2016.

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. 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.

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.

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. 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.

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. Ribbons will be enabled again when adding this after 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.

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. 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. It will lock the ribbon the first time i open excel so i create a new excel everytime after i open my first excel

I am using an Excel macro to create new workbooks and populate them with data (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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