Microsoft KB Archive/213749

= XL2000: How to Use a UserForm for Entering Data =

Article ID: 213749

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213749





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 Microsoft Visual Basic for Applications macro that places the data that you enter in a worksheet.



Creating a Custom UserForm
To create a custom UserForm, follow these steps:  Save and close any open workbooks, and then open a new workbook.  Type the following data in Sheet1:   A1: Name     B1: ID     C1: Phone  Start the Visual Basic Editor (press ALT+F11). On the Insert menu, click UserForm. Change the Caption property to Test Form . Add three text box controls (TextBox1, TextBox2, and TextBox3) to the UserForm, and space them out vertically so that you can add a label control above each text box control.</li> Add three label controls (Label1, Label2, and Label3), one above each of the text box controls that you added.</li>  Change the Caption properties for each of the label controls to the following: <pre class="fixed_text">  Control   Caption property --

Label1   Name Label2   ID   Label3    Phone </li> Add two command button controls (CommandButton1 and CommandButton2) to the UserForm.</li>  Change the Caption properties for each of the command button controls to the following: <pre class="fixed_text">  Control          Caption property -

CommandButton1  Add Record CommandButton2  Exit </li></ol>

Sample Macro for Adding UserForm Data to a Worksheet
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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

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

To attach macro code to the controls on the UserForm, follow these steps: <ol> Double-click UserForm1 to display the code module that is associated with the UserForm.</li>  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 </li>  Type the following code for the CommandButton2 Click event: Private Sub CommandButton2_Click End End Sub </li> On the Insert menu, click Module.</li>  In this module, type the following code: Sub Show_UserForm UserForm1.Show End Sub </li></ol>

Using the UserForm for Data Entry
To use the UserForm, follow these 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.
 * 1) After you type the data, click Add Record.

The data that you typed is placed in Sheet1 under the field headers that you entered in row 1.
 * 1) When you are prompted about whether to add another record, click Yes to continue or No to close the UserForm.

<div class="references_section">