VBA Approach To Moving A Userform

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a userform is Height = 220 and Width = 312. By default, this userform will pop up in the centre of the window.
In my code, this userform expands to a width of 432, and the height remains unchanged.

I am looking for a VBA solution to recentre the userform based on its new size. One factor that will need to be considered is that the Excel VBA application I'm working on will be used on different sized monitors, so fully a solution can be shared that will compensate for that.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Dataluver ... I'm thinking application window.
 
Upvote 0
I suppose that this would do.

VBA Code:
Private Sub UserForm_Resize()
    Move ActiveWindow.Width / 2 - Width / 2, ActiveWindow.Height / 2 - Height / 2
End Sub
 
Upvote 0
Thanks dataluver!
Here is how Ihave integrated this into my project:
Rich (BB code):
Sub chg_rcode()
    'Stop
    rcode = permit.cbx_rcode.Value
    With permit
        .Width = 655
        Move ActiveWindow.Width / 2 - Width / 2, ActiveWindow.Height / 2 - Height / 2
    End With
    ... 


This is in a standard module. permit is the user form to be mover. The application is stopping with an error at the point highlighted in red.
"Argument not optional."
 
Upvote 0
I think...

Rich (BB code):
With permit
    .Move ActiveWindow.Width / 2 - 655 / 2, ActiveWindow.Height / 2 - .Height / 2, 655
End With

Maybe this is more readable:
VBA Code:
    Dim NewWidth As Long
    Dim NewTop As Long
    Dim NewLeft As Long
    
    NewWidth = 655
    NewLeft = (ActiveWindow.Width / 2) - (NewWidth / 2)
    NewTop = (ActiveWindow.Height / 2) - (Permit.Height / 2)
    Permit.Move NewLeft, NewTop, NewWidth
 
Last edited by a moderator:
Upvote 0
Ding ding ding! Winner winner chicken dinner!
Thats what I was looking for ... yeah!

Thanks so much DL!

(i do have to test it on a different size monitor, but I have no doubts)
 
Upvote 0

Forum statistics

Threads
1,215,221
Messages
6,123,701
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top