How to Create a Self-Clearing Form with VBA and Named Ranges

In this tutorial, we will show how to create a self-cleaning form with VBA and named ranges.

How to Create a Self-Clearing Form with VBA and Named Ranges

 

Creating a self-clearing Form with VBA and Named Ranges in Excel simplifies data entry tasks and enhances productivity. It automatically clears its input fields after data is submitted, making it ideal for data entry scenarios where you need to input multiple records quickly.

In this tutorial, we will show how to create a self-cleaning form with VBA and named ranges.

Let’s create a simple employee information form that accepts employee data, saves the data to a database sheet, and automatically clears the form after submission.

Step 1: Setting Up the Worksheet Structure

  • Open a new Excel workbook.
  • Create your form layout.
  • Rename Sheet1 to “Form”.
  • Create the following layout on the Form sheet.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Next to each label, provide blank cells (B2, B3, B4, B5) for data entry.

Setting Up the Database Sheet:

  • Insert a new worksheet and rename it to “Database”.
  • Create headers in row 1:
    • Name
    • Department
    • Salary
    • Start Date
    • Entry Date

How to Create a Self-Clearing Form with VBA and Named Ranges

Step 2: Creating Named Ranges

Named ranges make referencing cells in VBA easier and clearer. We’ll create named ranges for all form inputs.

  • Select cell B2.
  • In the Name Box (left side of the formula bar), type Name.
  • Press Enter.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Repeat this process for each cell:
    • B3: Name it Department.
    • B4: Name it Salary.
    • B5: Name it StartDate.

Verifying Named Ranges:

  • Go to the Formulas tab >> select Name Manager or press Ctrl+F3.
  • Verify all named ranges are created correctly.
  • Select Close.

How to Create a Self-Clearing Form with VBA and Named Ranges

Step 3: Creating Form Buttons

Adding Submit Button:

  • Go to the Form sheet.
  • Go to Developer tab >> select Insert >> from Form Controls >> select Button.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Draw the button in the cell.
  • When prompted, name the macro SubmitandClearForm.
  • Click OK.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Right-click the button >> select Edit Text.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Change to Submit and Clear Form.

How to Create a Self-Clearing Form with VBA and Named Ranges

Adding Clear Button:

  • Insert another button.
  • Name the macro ClearForm.
  • Change button text to Clear Form.

How to Create a Self-Clearing Form with VBA and Named Ranges

Note: If you don’t see the Developer tab, enable it via FileOptionsCustomize Ribbon → check Developer.

Step 4: Writing the VBA Code

  • Go to the Developer tab >> select Visual Basic.
  • From the menu, click Insert >> select Module.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Copy-paste the following VBA code.

SubmitForm VBA Code:

Sub SubmitandClearForm()
    Dim ws As Worksheet
    Dim dbSheet As Worksheet
    Dim lastRow As Long
    Dim formValid As Boolean
    
    ' Set worksheet references
    Set ws = ThisWorkbook.Sheets("Form")
    Set dbSheet = ThisWorkbook.Sheets("Database")
    
    ' Find the next empty row in database
    lastRow = dbSheet.Cells(dbSheet.Rows.Count, 1).End(xlUp).Row + 1
    
    ' Copy data from form to database
    dbSheet.Cells(lastRow, 1).Value = Range("Name").Value
    dbSheet.Cells(lastRow, 2).Value = Range("Department").Value
    dbSheet.Cells(lastRow, 3).Value = Range("Salary").Value
    dbSheet.Cells(lastRow, 4).Value = Range("StartDate").Value
    dbSheet.Cells(lastRow, 5).Value = Now() ' Entry timestamp
    
    ' Clear the form
    ClearForm
    
    ' Provide feedback
    MsgBox "Employee data submitted successfully!", vbInformation, "Success"
    
    ' Set focus back to name field
    Range("Name").Select
End Sub

How to Create a Self-Clearing Form with VBA and Named Ranges

Explanation:

  • Takes input from named ranges on the Form sheet.
  • Adds this data to the next row in the Database sheet, along with a timestamp.
  • Calls the ClearForm macro to reset the form fields.
  • Shows a confirmation message and sets the cursor to the Name field.

ClearForm VBA Code:

Sub ClearForm()
    ' Clear all form fields using named ranges
    Range("Name").ClearContents
    Range("Department").ClearContents
    Range("Salary").ClearContents
    Range("StartDate").ClearContents
    
    ' Set focus to first field
    Range("Name").Select
End Sub

How to Create a Self-Clearing Form with VBA and Named Ranges

Explanation:

  • Clears the content of all form fields (Name, Department, Salary, StartDate) on the Form sheet.
  • Sets the cursor back to the Name field for quick data entry.

Step 6: Testing Self-Clearing Form

  • Save your workbook as an Excel Macro-Enabled Workbook (.xlsm).
  • Enter data in your form fields.
  • Click Submit and Clear Form.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • The form fields automatically submit data to the database sheet. Automatically clear the form data, and the cursor will return to the first field, ready for new data entry.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Submitted data is stored in the database sheet.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Additionally, you can use the Clear Form button. It will remove the form data, whether it is submitted or not.

How to Create a Self-Clearing Form with VBA and Named Ranges

  • Returns the cursor to the first field.

How to Create a Self-Clearing Form with VBA and Named Ranges

Benefits of Using Self-Clearing Forms

  • Efficiency: Saves time by eliminating manual deletion of input.
  • Accuracy: Reduces human error from repetitive tasks.
  • Convenience: Enhances user experience and data-entry speed.

Conclusion

By following the above steps, you can create a fully functional self-clearing form with VBA and named ranges. Now your Excel form will clear itself after each entry, enhancing your workflow dramatically. The use of named ranges makes the code more maintainable and easier to understand, while the self-clearing functionality ensures efficient data entry workflows.

Get FREE Advanced Excel Exercises with Solutions!

Shamima Sultana
Shamima Sultana

Shamima Sultana, BSc, Computer Science and Engineering, East West University, Bangladesh, has been working with the ExcelDemy project for 3+ years. She has written and reviewed 1000+ articles for ExcelDemy. She has also led several teams with Excel VBA and Content Development works. Currently, she is working as the Project Manager and oversees the day-to-day work, leads the services team, allocates resources to the right area, etc. Her work and learning interests vary from Microsoft Office Suites, and... Read Full Bio

2 Comments
  1. This is really very use full.
    Can we create something like this for a employee survey also??

    • Hello L Muralidhar,

      Thank you! Yes—you can build a self-clearing employee survey with the same approach (named ranges + a submit macro).

      1. On a sheet (e.g., Survey_Form), create inputs: Name, Department, Q1, Q2, Q3 (use Data Validation for Likert choices if you like).
      2. Give each input a Named Range: EmpName, Dept, Q1, Q2, Q3.
      3. Create a data sheet named Survey_Data with headers in row 1:
      4. Add a Submit button and adjust the existing VBA code and assign it to SubmitForm.

      Regards
      ExcelDemy

Leave a reply

Close the CTA

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo