Home>Blogs>VBA>Data Entry Application in Excel VBA with a List Box
Data Entry Application in Excel VBA with a List Box
VBA

Data Entry Application in Excel VBA with a List Box

A data entry application in Excel VBA gives you one clean window where you add a record, see every saved record in a list box, double-click one to edit it, and update or delete it with a single click. In this tutorial you build exactly that: a UserForm with Name, Country, Mobile Number and Gender fields, Add, Update, Delete and Save buttons, and a multi-column list box that shows the data stored on a Database sheet in the same workbook. Every line of code below is taken from the two-part video.

Data entry application in Excel VBA with Name, Country, Mobile Number and Gender fields, Add, Update, Delete and Save buttons and a list box of saved records
The finished form: entry boxes on the left, every saved record in the list box on the right.

Watch the Video Tutorial

Part 1 designs the form and creates the Database sheet; Part 2 writes the code that fills the list box and runs the Add, Update and Delete buttons.


What You Will Build

  • A styled UserForm with a black title bar, an Enter Details frame and four input fields.
  • A six-column list box with headers that always shows the latest data from the Database sheet.
  • An Add button that checks no field is blank, writes the record with an automatic serial number and a time stamp, and refreshes the list.
  • Double-click to edit: double-clicking a record loads it back into the boxes.
  • Update and Delete buttons that find the selected record on the sheet by its serial number.
  • A Save button that saves the workbook from inside the form.

This is a single-user application: the data lives on a Database sheet inside the same Excel file.

Part 1 – Design the Data Entry Application Form

Step 1: Save a macro-enabled workbook

Start a new workbook and press F12. Save it as an Excel Macro-Enabled Workbook (.xlsm) or Excel Binary Workbook (.xlsb) so the code is kept. In the video I use .xlsb.

Step 2: Insert the UserForm

Press Alt+F11 (or Developer → Visual Basic), then Insert → UserForm. In the Properties window set:

  • BackColor: white
  • (Name): Myform
  • Caption: Enter Details (this shows in the form’s title bar)

Step 3: Build the title bar

Drag an Image control across the top of the form. Set its BackColor to black and BorderStyle to 0 – fmBorderStyleNone. Add a Label on top of it with your title, then set:

  • BackStyle: 0 – fmBackStyleTransparent, so the black shows through
  • ForeColor: white
  • Font: Monotype Corsiva, size 28 to 30
  • TextAlign: center

Step 4: Add the frame, labels and input boxes

Draw a Frame and set its Caption to Enter Details. Inside it add four labels, Name, Country, Mobile Number and Gender, each with BackStyle set to Transparent. If you skip this, the labels show a white block when you colour the frame.

Next to them add the inputs. Keep the default names, because the code uses them:

  • Name – TextBox1
  • Country – TextBox2
  • Mobile Number – TextBox3
  • Gender – ComboBox1

Give the boxes a SpecialEffect for a sunken look. Select the combo box and one text box, right-click and choose Make Same Size → Both. Then use Format → Align → Lefts and Middles, and Format → Vertical Spacing → Make Equal for the labels and the boxes.

Step 5: Add the four buttons

Add four command buttons below the boxes with the captions Add, Update, Delete and Save. They become CommandButton1 to CommandButton4 in that order. Select all four and use Format → Horizontal Spacing → Make Equal.

Step 6: Add the list box

From the Toolbox draw a ListBox (ListBox1) to the right of the frame. Give it the same SpecialEffect and a light grey BackColor. Select the frame and the list box, choose Make Same Size → Height and Align → Tops.

Step 7: Add a hidden helper box

Copy one of the text boxes and paste it somewhere on the form. This is TextBox4. It holds the serial number of the record you double-click, and Update and Delete use it to find the right row. Once everything works, set its Visible property to False.

Step 8: Create the Database sheet

Back in Excel, add a sheet named Database and type these headers in A1:F1: S.No., Name, Country, Mobile, Gender and Update Time. Records start in row 2.

Part 2 – Code the Data Entry Application in Excel VBA

Double-click the form to open its code window. All the code below goes in the Myform module.

Step 9: Fill the list box with Refresh_Data

This procedure links the list box to the data on the Database sheet. Every button calls it, so the list is always up to date.

Sub Refresh_Data()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")

Dim Last_Row As Long

Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))

With Me.ListBox1
    .ColumnHeads = True
    .ColumnCount = 6
    .ColumnWidths = "30,100,90,80,60,90"

    If Last_Row = 1 Then
        .RowSource = "Database!A2:F2"
    Else
        .RowSource = "Database!A2:F" & Last_Row
    End If

End With

End Sub

How Refresh_Data works

  • CountA counts the filled cells in column A, header included. With three records it returns 4, which is also the last used row.
  • ColumnHeads = True makes the list box show the sheet’s header row. With RowSource starting at A2, the row just above it (row 1) becomes the headers.
  • ColumnWidths sets the six column widths in points. Adjust them until the horizontal scroll bar disappears; in the video I reduced Mobile to 80 for that reason.
  • The If handles an empty sheet. With no records Last_Row is 1, and A2:F1 would point the wrong way and break the headers, so the code uses A2:F2 instead.

Step 10: Load the Gender list when the form opens

Private Sub UserForm_Activate()

With Me.ComboBox1
    .Clear
    .AddItem ""
    .AddItem "Male"
    .AddItem "Female"
End With

Call Refresh_Data

End Sub

.Clear runs first so the items are not added twice, and Call Refresh_Data shows the saved records as soon as the form appears.

Step 11: The Add button

Private Sub CommandButton1_Click()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")

Dim Last_Row As Long
Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))

'================ Validations ================
If Me.TextBox1.Value = "" Then
    MsgBox "Please enter the Name", vbCritical
    Exit Sub
End If

If Me.TextBox2.Value = "" Then
    MsgBox "Please enter the Country", vbCritical
    Exit Sub
End If

If Me.TextBox3.Value = "" Then
    MsgBox "Please enter the Mobile Number", vbCritical
    Exit Sub
End If

If Me.ComboBox1.Value = "" Then
    MsgBox "Please select the Gender", vbCritical
    Exit Sub
End If
'=============================================

sh.Range("A" & Last_Row + 1).Value = "=ROW()-1"
sh.Range("B" & Last_Row + 1).Value = Me.TextBox1.Value
sh.Range("C" & Last_Row + 1).Value = Me.TextBox2.Value
sh.Range("D" & Last_Row + 1).Value = Me.TextBox3.Value
sh.Range("E" & Last_Row + 1).Value = Me.ComboBox1.Value
sh.Range("F" & Last_Row + 1).Value = Now

Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.ComboBox1.Value = ""

Call Refresh_Data

End Sub

The validation is deliberately simple: no field may be blank. Each check shows a message and leaves with Exit Sub, so nothing is written until every box is filled.

How the serial number and time stamp work

  • The new record goes to Last_Row + 1, the first empty row.
  • Column A gets the formula =ROW()-1 rather than a typed number. Row 2 shows 1, row 3 shows 2, and so on, because the header takes row 1. When a row is deleted later, the formulas below it renumber themselves.
  • Column F gets Now, the current date and time, which is the Update Time shown in the list.

Step 12: Double-click a record to edit it

Double-click the list box in the form designer, pick DblClick from the event drop-down and delete the empty Click event:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

Me.TextBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
Me.TextBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
Me.TextBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 3)
Me.ComboBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 4)

End Sub

List(row, column) reads one cell of the list box. ListIndex is the selected row, and both rows and columns start counting at 0. Column 0 is S.No., which goes into the hidden TextBox4; columns 1 to 4 fill Name, Country, Mobile and Gender.

Step 13: The Update button

Private Sub CommandButton2_Click()

If Me.TextBox4.Value = "" Then
    MsgBox "Select the record to update"
    Exit Sub
End If

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")

Dim Selected_Row As Long
Selected_Row = Application.WorksheetFunction.Match(CLng(Me.TextBox4.Value), sh.Range("A:A"), 0)

'  ... the same four validations as the Add button ...

sh.Range("B" & Selected_Row).Value = Me.TextBox1.Value
sh.Range("C" & Selected_Row).Value = Me.TextBox2.Value
sh.Range("D" & Selected_Row).Value = Me.TextBox3.Value
sh.Range("E" & Selected_Row).Value = Me.ComboBox1.Value
sh.Range("F" & Selected_Row).Value = Now

Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.ComboBox1.Value = ""

Call Refresh_Data

End Sub

In the video I copy the Add code and change three things: the TextBox4 check comes first, Last_Row + 1 becomes Selected_Row, and the column A line is removed because the serial number does not change.

How the MATCH line finds the record

  • Match(value, sh.Range("A:A"), 0) returns the row number where the serial number sits in column A. The 0 means exact match.
  • A text box always returns text, but column A holds numbers. Without conversion MATCH fails with an error (this happens in the video at 13:00). CLng turns the text into a whole number first.

Step 14: The Delete button

Private Sub CommandButton3_Click()

If Me.TextBox4.Value = "" Then
    MsgBox "Select the record to update"
    Exit Sub
End If

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")

Dim Selected_Row As Long
Selected_Row = Application.WorksheetFunction.Match(CLng(Me.TextBox4.Value), sh.Range("A:A"), 0)

sh.Range("A" & Selected_Row).EntireRow.Delete

Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.ComboBox1.Value = ""

Call Refresh_Data

End Sub

It uses the same lookup as Update, then deletes the whole row. The message was copied from Update in the video; you may want to change the text to “Select the record to delete”.

Step 15: The Save button

Private Sub CommandButton4_Click()
ThisWorkbook.Save
MsgBox "Data Saved"
End Sub

Step 16: Open the form from the sheet

Hide the Database sheet (right-click its tab → Hide). On Sheet1 go to Developer → Insert → Command Button (ActiveX Control), give it a caption, double-click it in Design Mode and add:

Myform.Show

Leave Design Mode, click the button, and your data entry application is ready.

Common Errors and Fixes

These notes are general VBA guidance, added to help you troubleshoot. They are not from the video.

Type mismatch or “Unable to get the Match property” on Update

Either the CLng is missing, or no record was double-clicked so TextBox4 holds no number. Keep the TextBox4 check at the top of Update and Delete.

The list box headers show Column A, Column B

The RowSource starts at row 1 instead of row 2, or the sheet is empty and the If Last_Row = 1 block is missing. Copy Refresh_Data exactly as shown.

Compile error: Method or data member not found

A control was renamed or deleted. Check the names in the Properties window: the code expects TextBox1 to TextBox4, ComboBox1, ListBox1 and CommandButton1 to CommandButton4.

Serial numbers jump after deleting

Column A must hold the =ROW()-1 formula, not typed numbers. Then the numbers always run 1, 2, 3 without gaps.

Tips

These tips are additions to the video.

  • Add a confirmation before deleting: wrap the delete line in If MsgBox("Delete this record?", vbYesNo + vbQuestion) = vbYes Then … End If.
  • Set the Gender combo box Style to fmStyleDropDownList so users can only pick Male or Female.
  • Add one more check, such as IsNumeric(Me.TextBox3.Value), if you want only digits in Mobile Number.
  • Because the data is in the same file, this is a single-user tool. Keep one copy and back it up regularly.
  • Read Microsoft’s ListBox control reference for every list box property.

Want It Ready-Made?

The finished workbook from this tutorial is available as Data Entry Application in Excel VBA on NextGenTemplates.com, with the form, list box and all buttons already working. You can also browse more Excel VBA tools.

Related Tutorials

Frequently Asked Questions

Where does the data entry application save the data?

On a sheet named Database in the same workbook. Each record takes one row with serial number, name, country, mobile, gender and update time in columns A to F. You can hide that sheet so users only work through the form.

How do I show worksheet data in a VBA list box?

Set the list box RowSource to the sheet range, for example Database A2 to F and the last row, set ColumnCount to the number of columns and ColumnHeads to True. Step 9 shows the full Refresh_Data procedure.

How do I edit a record from the list box?

Double-click the record. The ListBox1_DblClick event copies each column of the selected row into the text boxes and the serial number into a hidden box, and the Update button writes the changes back to that row.

Why do I need CLng in the MATCH line?

Text box values are text, while the serial numbers on the sheet are numbers. MATCH treats them as different values and fails, so CLng converts the text into a number first.

Can several people use this form at the same time?

No. The data is stored inside the same Excel file, so it is a single-user application. Only one person should have the file open and save it at a time.

About the Author

PK is a Microsoft Excel expert and trainer and the founder of PK: An Excel Expert and NextGenTemplates.com. He has been teaching Excel, VBA, Power Query and Power BI since 2016 on his YouTube channel.

Conclusion

You now have a complete data entry application in Excel VBA: a styled form, a list box that always shows the latest records, simple validation, automatic serial numbers and time stamps, and double-click editing with Update and Delete. Build the design first, add Refresh_Data, then wire up one button at a time and test as you go. Once it works, add your own fields and checks in the same pattern.

PK
Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!
https://www.pk-anexcelexpert.com