I assign keyboard shortcut to open a VBA UserForm in Excel. For some reason when UserForm is shown Excel activates the workbook which was active the first time the UserForm was shown. The code is vanilla. Does anyone know why it happens and how to fix it?
Sub AssignShortcuts()
Application.OnKey "^+%s", "UserFormShow"
End Sub
Sub UserFormShow()
UserForm.Show
End Sub
Sub CloseButton_Click()
Me.Hide
End Sub
I tried capturing and reactivating active workbook in UserFormShow(), but it didn’t help. Debug.print outputs the right workbook, but it is not activated.
Sub UserFormShow()
Dim wb As Workbook
Set wb = ActiveWorkbook
frmMain.Show
wb.Activate
Debug.Print "Activated Workbook: " & wb.Name
End Sub
user2443705 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.