Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Dynamically Resize a User Form


View products that this article applies to.

Summary

This article describes how to create a custom user form that dynamically changes size to show more options when you click a button.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

How to Create the User Form

To create the user form, follow these steps:
  1. Close all open workbooks and open a new workbook.
  2. Start the Visual Basic Editor (press ALT+F11).
  3. Click UserForm on the Insert menu to create a blank user form.
  4. If the Properties window is not displayed, click Properties Window on the View menu.
  5. Type the following values for the Height and Width properties for UserForm1:
    Height: 149
    Width: 352
  6. On the Toolbox, click the CommandButton icon and draw two command buttons that are aligned on the right side of the form.
  7. For the following properties, type the following values.
       CommandButton1:
    
          Property            Value
          -----------------------------
          Name                btnExpand
          Caption             Expand
          Height              24
          Width               54
          Left                6
          Top                 6
    
       CommandButton2:
    
          Property            Value
          -------------------------------
          Name                btnContract
          Caption             Contract
          Height              24
          Width               54
          Left                6
          Top                 36
    					
  8. On the Toolbox, click the OptionButton icon and draw two option buttons on the form.
  9. For the following properties, type the following values.
       OptionButton1:
    
          Property            Value
          --------------------------------
          Caption             ExtraOption1
          Left                264
          Top                 30
    
       OptionButton2:
    
          Property            Value
          --------------------------------
          Caption             ExtraOption2
          Left                264
          Top                 54
    					
  10. Set the Width property of the user form to 185.25.

How to Create the Code

Double-click the Expand command button on the user form and type the following code in the Code module for the user form:
Sub btnExpand_Click()
   UserForm1.Width = 352.5
End Sub

Sub btnContract_Click()
   Userform1.Width = 185.25
End Sub
				

How to Run the Macro

To run the macro, follow these steps:
  1. On the File menu, click Close and Return to Microsoft Excel.
  2. On the View menu, point to Toolbars, and then click Forms.
  3. On the Forms toolbar, click the Button icon, and then draw a button on the worksheet.
  4. When the Assign Macro dialog box appears, type ShowForm in the Macro name box, and then click New.
  5. Type the following code in the General module:
    Sub ShowForm()
       UserForm1.Show
    End Sub
    					
  6. On the File menu, click Close and Return to Microsoft Excel.
  7. Click the worksheet that contains the button.
  8. Click the button to run the ShowForm macro.
  9. Click the Expand button.
The dialog box expands and you can see the option buttons. If you click Contract, the dialog box is reduced in size and you no longer see the option buttons.

↑ Back to the top


References

For more information about UserForms, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type userform window in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

↑ Back to the top


Article Info
Article ID : 213768
Revision : 1
Created on : 1/1/0001
Published on : 1/1/0001
Exists online : False
Views : 279