VBA: Show userform in center of screen

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I currently use the code below to center the userform in the center of the screen. All works good.

Code:
With UserForm1
  .StartUpPosition = 0
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
End With

I have many userforms and instead of duplicating this code in the Sub to open every userform, can I call it somehow?

I'm just not sure how I would pass the name of the userform to the call to retrieve the position details.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Well glad to see you have things working for you. I thought from the beginning you wanted Multipages hidden so user could only see the one that applied to them. But it's been a week or more now and maybe I do not remember that. But I know how projects can change glad to see you know how to modify scripts to work the way you want. As far as centering the form. My Userform always shows up in the center of the screen by default but I'm not using multi monitors. Using case is a nice way to do things. Take care.


Indeed it is, and as you help lead me down this path, I think the process is shifting into the easiest to maintain (on my part).


I started out with the ComboBox idea only because that's what was stuck in my head. As this UserForm approach has opened my eyes, so has my idea for what's simplest for the user to interact with.

Maybe there isn't any need to hide the Multipages as they are all applicable and the user can jump back and forth amongst the pages as they deem necessary. The cherry on the top was to have the page to open that equals the active sheet.

Anyway, I think the simplest method is to use a Case statement structure on UserForm Initialize and let that chose the page by the activesheet.name.

Code:
Private Sub UserForm_Initialize()
    
    Select Case ActiveSheet.Name
    
        Case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
            MultiPage1.Value = 0
        
        Case "Master"
            MultiPage1.Value = 1
        
        Case "Analysis"
            MultiPage1.Value = 2
        
        Case "Level 1", "Level 2", "Level 3"
            MultiPage1.Value = 3
        
        Case "Summary"
            MultiPage1.Value = 4
        
        Case "Charts"
            MultiPage1.Value = 5
                
        Case "Forecating"
            MultiPage1.Value = 6
        
    End Select
    
End Sub

As you can see, the worksheet will have 20 tabs and the Multipage UserForm will have 7 pages.
 
Upvote 0

Forum statistics

Threads
1,215,457
Messages
6,124,941
Members
449,197
Latest member
k_bs

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