Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA User Form Centering when using two Monitors

Status
Not open for further replies.

DylaBrion

Technical User
Dec 18, 2018
45
GB
Hi

Can anyone help with the following. I have a User Form which I would like to centre (on opening) on one of my two monitors.

I have found the following code (which runs on user form initialising)

Private Sub UserForm_Initialize()
With GenAvailInputForm
Me.StartUpPosition = 0
Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
Me.Show
End With
End Sub

It seems to work but when I close the form on the disable button I get the following message

Run-Time error '91'

Object Variable or With Block Variable not set

I'm a bit of a novice so any help would be appreciated

Thanks
 
>but when I close the form on the disable button I get the following message

Ok, but the code you've shown us is simply for the form initialising, not closing. We need to see the code that runs when you are clicking the 'disable' button.
 
Eliminate [tt]Me.Show[/tt] line of code.


---- Andy

There is a great need for a sarcasm font.
 
Hi Strongm and Andrzejek. Removing the Me.Show line code worked

Many Thanks

Paul
 
Good catch - although I'm not quite sure how the rest of the code is supposed to work, since I'm pretty sure the Access Application object doesn't have Left, Top, Width or Height properties - which tends to make me think this code is actually in Excel (or possibly Word), in which case, apart from the Me.Show it is fine. Only we don't actually appear to need With or End With statements ; I presume they are an artifact of some earlier version of the code ...
 
strongm - apologies as I may have misled you. This is in Excel so maybe I am in the wrong Forum?
 
The title of the form is the give away ;-) (mind you, the title of your email is also a giveaway) Yes, non-Access VBA questions are best raised in forum707
 
Many thanks strongm. I'll be more vigilant regarding the Forum I am joining in future
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top