XL97: How to Dynamically Resize a UserForm

Last reviewed: March 13, 1998
Article ID: Q160589
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

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

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Creating the UserForm

  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 UserForm.

  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 CommandButton 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 OptionButton 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 UserForm to 185.25.

Creating the Code

  1. Double-click the Expand command button on the UserForm and type the following code in the Code module for the UserForm:

          Sub btnExpand_Click()
             UserForm1.Width = 352.5
          End Sub
    
          Sub btnContract_Click()
             Userform1.Width = 185.25
          End Sub
    
    

Running the Macro

  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 Button, and then draw a button on the worksheet.

  4. When the Assign Macro dialog box appears, type "ShowForm" (without the quotation marks) 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.

REFERENCES

For more information about UserForms, click the Index tab in Visual Basic for Applications Help, type the following text

   userform window

and then double-click the selected text to go to the "UserForm Window" topic.


Additional query words: XL97
Keywords : kbcode kbprg xlvbahowto xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.