XL97: How to Use a UserForm for Entering Data

ID: Q161514


The information in this article applies to:
  • Microsoft Excel 97 for Windows


SUMMARY

In Microsoft Excel, you can create a custom UserForm that provides a simple interface for entering data. This article includes steps for creating a custom UserForm and a sample Visual Basic for Applications macro that places the data you enter on a worksheet.


MORE INFORMATION

Creating a Custom UserForm

To create a custom UserForm, use the following steps:

  1. Save and close any open workbooks, and then create a new workbook.


  2. Type the following in Sheet1:
    A1: Name B1: ID C1: Phone


  3. Start the Visual Basic Editor (press ALT+F11).


  4. On the Insert menu, click UserForm.


  5. Add three TextBox controls (TextBox1, TextBox2, and TextBox3) to the UserForm and space them out vertically so that you can add a Label control above each TextBox control.


  6. Add three Label controls (Label1, Label2, and Label3), one above each of the TextBox controls you added.


  7. Change the Caption properties for each of the Label controls to the following.
    
          Control   Caption property
          --------------------------
    
          Label1    Name
          Label2    ID
          Label3    Phone 


  8. Add two CommandButtons (CommandButton1 and CommandButton2) to the UserForm.


  9. Change the Caption properties for each of the CommandButton controls to the following.
    
          Control          Caption property
          ---------------------------------
    
          CommandButton1   Add Record
          CommandButton2   Exit 


Sample Macro for Adding UserForm Data to a Worksheet

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

To attach macro code to the controls on the UserForm, use the following steps:

  1. Double-click the UserForm to display the code module that is associated with the UserForm.


  2. Type the following code for the CommandButton1 Click event:
    
          Private Sub CommandButton1_Click()
              Dim LastRow As Object
    
              Set LastRow = Sheet1.Range("a65536").End(xlUp)
    
              LastRow.Offset(1, 0).Value = TextBox1.Text
              LastRow.Offset(1, 1).Value = TextBox2.Text
              LastRow.Offset(1, 2).Value = TextBox3.Text
    
              MsgBox "One record written to Sheet1"
    
              response = MsgBox("Do you want to enter another record?", _
                  vbYesNo)
    
              If response = vbYes Then
                  TextBox1.Text = ""
                  TextBox2.Text = ""
                  TextBox3.Text = ""
    
                  TextBox1.SetFocus
    
              Else
                  Unload Me
              End If
    
          End Sub 


  3. Type the following code for the CommandButton2 Click event:
    
          Private Sub CommandButton2_Click()
              End
          End Sub 


  4. On the Insert menu, click Module.


  5. In this module, type the following code:
    
          Sub Show_UserForm()
              UserForm1.Show
          End Sub 


Using the UserForm for Data Entry

To use the UserForm, use the following steps:

  1. In the Visual Basic Editor, click "Close and Return to Microsoft Excel" on the File menu.


  2. Save the workbook.


  3. Run the Show_UserForm macro (press ALT+F8, and then double-click Show_UserForm in the list of macros).

    The UserForm is displayed, and you can start typing data in the three text boxes.


  4. After typing the data, click Add Record on the UserForm.

    The data you typed is placed in Sheet1 under the field headers in row 1.


  5. When you are prompted whether to add another record, click Yes to continue or No to exit the UserForm.


For additional information, please see the following article in the Microsoft Knowledge Base:
Q141289 XL: How to Use a Custom Dialog Box for Data Entry


REFERENCES

For more information about Custom UserForms, click the Office Assistant, type custom dialog boxes, click Search, and then click to view "Creating a custom dialog box".

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Excel Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

Q120802 Office: How to Add/Remove a Single Office Program or Component

Additional query words: XL97

Keywords : kbprg kbualink97 kbdta kbdtacode xlui KbVBA
Version : WINDOWS:
Platform : WINDOWS
Issue type :


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.