Shari W
Shari W

Reputation: 537

VBA Excel: How can I make one form change the position of another form?

I have two forms, frmPE and frmCRE. They are modal.

frmCRE has a button "Edit PE" that will open formPE (instance of frmPE). If I don't play with the positions, formPE opens centered on top of frmCRE. I would like to position them side by side so I can see both at once.

When I open frmPE, I can still see frmCRE but not touch it. That's OK.

' This code is in cmdEditPE

Set formPE = New frmPE
Load formPE
If Not formPE.bInitialize(vbFalse) Then Err.Raise glHANDLED_ERROR
Me.Left = 100  ' move frmCRE to the left so I can see it 
formPE.Left = Me.Left + Me.Width   ' move frmPE to move to the right side of frmCRE (bad)
formPE.Show

I think the problem is that when formPE opens, it uses its default left setting. From within PE, I can see that me.Left = 0 even though it was set before the show.

Is there a way I can use the formPE.bInitialize routine to see that frmCRE is open and find out its LEFT and WIDTH so I can set formPE.Left correctly, or a way I can set it from frmCRE as I was trying to do above?

Thanks Shari

Upvotes: 1

Views: 692

Answers (1)

chris neilsen
chris neilsen

Reputation: 53166

Use the Activae event of frmPE to move itself, but only if frmCRE is visible.

In frmPE

Private Sub UserForm_Activate()
    If frmCRE.Visible Then
        Me.Left = frmCRE.Left + frmCRE.Width
    End If
End Sub

Upvotes: 1

Related Questions