Excel

Get Userform Metrics

Ease of Use

Intermediate

Version tested with

2000, 2003 

Submitted by:

johnske

Description:

After running the code a new sheet named 'SnapShot' is created, this sheet lists the Height, Width, Top, and Left position of the userform and every control on the userform. 

Discussion:

You may have gone to great care to create, size, and position controls on a userform. If you want to write these sizes and positions (the metrics) into the code it's often a lot of trial and error to get the positions etc. right for the actual code.... Just place this code into your userform module and show the form to create a list of all these basic metrics... 

Code:

instructions for use

			

'******************************** '<< CODE FOR USERFORM MODULE >> Option Explicit Private Sub UserForm_Activate() Dim FormControl As Control, N& '//allow form to load DoEvents Application.ScreenUpdating = False '//error = no 'SnapShot' sheet On Error GoTo AddSnapShot Sheets("SnapShot").Activate SheetExists: Cells.ClearContents '//put column headings on the sheet [A1] = "Control" [B1] = ".Height" [C1] = ".Width" [D1] = ".Top" [E1] = ".Left" '//get userform metrics With [A2] .Offset(0, 0) = Me.Name .Offset(0, 1) = Me.Height .Offset(0, 2) = Me.Width .Offset(0, 3) = Me.Top .Offset(0, 4) = Me.Left N = 1 '//get all the control metrics For Each FormControl In Controls .Offset(N, 0) = FormControl.Name .Offset(N, 1) = FormControl.Height .Offset(N, 2) = FormControl.Width .Offset(N, 3) = FormControl.Top .Offset(N, 4) = FormControl.Left N = N + 1 Next FormControl End With '//tart up the sheet a bit Rows(1).Font.Bold = True Columns(1).Font.Bold = True With Cells .Columns.AutoFit .Rows.AutoFit .HorizontalAlignment = xlLeft End With '//unload the userform & exit sub Unload Me Exit Sub AddSnapShot: '//add a sheet ActiveWorkbook.Sheets.Add after:=Sheets(Sheets.Count) ActiveSheet.Name = "Snapshot" '//clear error and start again at SheetExists Resume SheetExists End Sub '******************************** '******************************** '<< CODE FOR MODULE1 >> Option Explicit Sub ShowForm() UserForm1.Show False End Sub '********************************

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/UserForm
  4. Select the toolbox, paste a variety of controls on the UserForm
  5. Right-Click UserForm1 in the Project - VBA Project pane
  6. Select ShowCode
  7. Select everything, then copy and paste the userform code into the code window
  8. Now select Insert/Module, paste the code for module1 into this module
  9. Click the X in the top right of the VBE window to return to Excel
  10. Save your work...
 

Test the code:

  1. Go to Tools/Macro/Macros.../ShowForm/Run
 

Sample File:

FormMetrics.zip 13.02KB 

Approved by mdmackillop


This entry has been viewed 255 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express