Moving command buttons using VBA causes them to vanish!

sts023

Board Regular
Joined
Sep 1, 2008
Messages
106
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
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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try...

Code:
  With UF1
    .StartUpPosition = 1    'Center of screen.
    .Height = sngScrH
    .Width = sngScrW
'Reposition CB1
    .CB1.Top = .[COLOR=#ff0000]InsideHeight [/COLOR]- (.CB1.Height + 10)
    .CB1.Left = sngL
    sngL = sngL + .CB1.[COLOR=#ff0000]Width [/COLOR]+ 10
'Reposition CB2
    .CB2.Top = .[COLOR=#ff0000]InsideHeight [/COLOR]- (.CB2.Height + 10)
    .CB2.Left = sngL
    sngL = sngL + .CB2.[COLOR=#ff0000]Width [/COLOR]+ 10
'Reposition CB3
    .CB3.Top = .[COLOR=#ff0000]InsideHeight [/COLOR]- (.CB3.Height + 10)
    .CB3.Left = sngL
    
    .Show
    
  End With

Hope this helps!
 
Last edited:
Upvote 0
Thanks Domenic - works a treat!
I've been creating Userforms for years, but never remember seeing the "InsideHeight" property before. The Help on that topic isn't actually all that informative - good job there's help available in the MrExcel community!
if you happen to know - what was actually happening to the buttons when I was using "Height"? It's not important in terms of my now "un-stalled" project, but all knowledge has value...
 
Upvote 0
You're very welcome. Glad I could help.

The InsideHeight refers to the working area within the userform. So unlike Height, it excludes the title bar and border areas.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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