I have a userform that has a command button to open another userform. This works well most of the time but there is a particular scenario where it does not work at all.
The users of said userforms typically work on multiple monitors and also have multiple workbooks open. These other workbooks are not associated with my workbook.
If the user is working in another workbook on monitor 1 and then clicks the button to open the secondary userform on monitor 2, the secondary userform opens on monitor 1 and behaves like it's parent is the other workbook. To correct this behavior, the user needs to click a cell on monitor 2's workbook and then click the button to open the secondary userform. So it seems to be a workbook activation issue but this is not the case.
I have tried different methods of activating the correct workbook before showing the secondary userform with code to no avail.
Here's the basic code I use to launch the secondary userform when the button is clicked:
Private Sub CommandButton1_Click()
With UserForm2
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
.Show vbModeless
End With
End Sub
I found this thinking I just need to explicitly set the userforms parent window before showing it but the userform will not open at all with this code: http://www.cpearson.com/excel/SetParent.aspx
I hope this makes sense. It's kind of hard to explain.
With
block is bloody misleading, once you realize thatWith New
witholds an object reference that gets destroyed atEnd With
(i.e. if you change your code toWith New
, form will be destroyed as soon as it's shown). – Mathieu Guindon