Userform not starting in the center of the sheet

SantanaKRE8s

Board Regular
Joined
Jul 11, 2023
Messages
114
Office Version
  1. 365
Platform
  1. Windows
Can someone check this code please, to see why its not starting at the center of my sheet. I copied it from another workbook and made the changes to names but still not starting in the center.

Sub UserForm()
Dim i As Integer

'Typical approach for zero based collection is to use For i = 0 To [collection].Count-1
'If there is only 1 form open that counter will become For i = 0 to 0 (because of 1-1)
'This approach makes the counter from 1 to 1 when only 1 form is open
'and Userforms(i-1) produces the correct zero based starting point for the collection

'If count = 0 then no form is open, skip to With block, else test if open form name is UserForm.
'If true, close it and exit sub
For i = 1 To VBA.UserForms.Count
If VBA.UserForms(i - 1).Name = "UserForm" Then Unload VBA.UserForms(i - 1)
Exit Sub
Next

With UserForm
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.3 * .Height)
.Show
End With

End Sub

Sub StoreUserformCode1()
Dim txtFrame As TextFrame

Set txtFrame = Sheets("Shipping").Shapes("Rounded Rectangle 1").TextFrame
If txtFrame.Characters.Text = "Close UF" Then
Unload UserForm
txtFrame.Characters.Text = "UserForm"
Exit Sub
Else
With UserForm
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.3 * .Height)
.Show
End With

End If
txtFrame.Characters.Text = "Close UF"
End Sub

Sub StoreUserformCode2()
Dim txtFrame As TextFrame

Set txtFrame = Sheets("Shipping").Shapes("Rounded Rectangle 1").TextFrame
If txtFrame.Characters.Text = "Close UF" Then
Unload UserForm
txtFrame.Characters.Text = "UserForm"
Exit Sub
Else
txtFrame.Characters.Text = "Close UF"
With UserForm
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.3 * .Height)
.Show
End With

End If

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,215,084
Messages
6,123,029
Members
449,092
Latest member
ikke

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