I want to use VBA to resize a Userform to fill the screen, then move some command buttons to align them at a fixed height above wherever the bottom of the Userform ends up.
When I execute the code, the command buttons vanish!
I've copied the relevant bits of my code and created the module below, which exhibits the same behaviour.
To replicate this (Excel 2010), in a new Workbook create a Userform and name it "UF1", and four command buttons in random positions, naming them CB1, CB2, CB3, and CB4. Ensure CB4 is fairly high up on the form to avoid conflicts with the attempted positioning of the other three.
In each CB's "_Click" event, enter "Me.Hide"
Insert a module, and populate it with the code below. The code is supposed to align CBs 1-3 in a line, 10 points above the base of the Userform, and 20 points apart.
On execution, cbs 1-3 vanish.
Can anyone PLEASE give me a hint as to what I'm doing wrong? I've spent 10 days recoding this to try to understand and get around the problem, but to no avail.
code follows
When I execute the code, the command buttons vanish!
I've copied the relevant bits of my code and created the module below, which exhibits the same behaviour.
To replicate this (Excel 2010), in a new Workbook create a Userform and name it "UF1", and four command buttons in random positions, naming them CB1, CB2, CB3, and CB4. Ensure CB4 is fairly high up on the form to avoid conflicts with the attempted positioning of the other three.
In each CB's "_Click" event, enter "Me.Hide"
Insert a module, and populate it with the code below. The code is supposed to align CBs 1-3 in a line, 10 points above the base of the Userform, and 20 points apart.
On execution, cbs 1-3 vanish.
Can anyone PLEASE give me a hint as to what I'm doing wrong? I've spent 10 days recoding this to try to understand and get around the problem, but to no avail.
code follows
Code:
Option Explicit
Public Sub ButtonShifter()
Dim sngG As Single
Dim sngL As Single
Dim sngScrH As Single
Dim sngScrL As String
Dim sngScrT As Single
Dim sngScrW As Single
Dim sngT As Single
Dim strMsg As String
'*
'** Fill the screen with the
'** Excel application.
'*
Application.WindowState = xlMaximized
'*
'** Recover the screen's Top, Left,
'** Height and Width.
'*
sngScrT = Application.Top
sngScrL = Application.Left
sngScrH = Application.Height
sngScrW = Application.Width
'*
'** Show dimensions to User.
'*
strMsg = "Height: " & CStr(sngScrH) & vbCrLf
strMsg = strMsg & "Width: " & CStr(sngScrW) & vbCrLf
strMsg = strMsg & "Top: " & CStr(sngScrT) & vbCrLf
strMsg = strMsg & "Left: " & CStr(sngScrL)
Call MsgBox(strMsg, vbInformation, "SCREEN DIMENSIONS")
sngG = 20 'Gap
sngT = 30 'Top
sngL = 10 'Initial Left
With UF1
.StartUpPosition = 1 'Center of screen.
.Height = sngScrH
.Width = sngScrW
'Reposition CB1
.CB1.Top = .Height - (.CB1.Height + 10)
.CB1.Left = sngL
sngL = sngL + .CB1.Left + 10
'Reposition CB2
.CB2.Top = .Height - (.CB2.Height + 10)
.CB2.Left = sngL
sngL = sngL + .CB2.Left + 10
'Reposition CB3
.CB3.Top = .Height - (.CB3.Height + 10)
.CB3.Left = sngL
.Show
End With
End Sub 'ButtonShifter