How to code command button of user form to create database

How to create a database quickly and easily by coding the ‘add data’ command button which does the following:
(i) First checks whether all controls have been filled with data
(ii) Checks if any duplicate entry has been made
(iii) Clears the data of controls once the data has been transferred to the Excel worksheet

Code add button to create database
Code add command button to create a database in Excel

Watch the video below:

You can watch this video also on YouTube.

Her’s the complete VBA code:

Private Sub UserForm_Initialize()
myCtl6.List = Array(“arts”, “science”)
End Sub
Private Sub CommandButton1_Click()
Dim ctlNum As Long
Dim erow As Long
Dim intmyCtl As Integer
ctlNum = 6
For intmyCtl = 1 To ctlNum
If Me.Controls(“myCtl” & intmyCtl).Value = “” Then
MsgBox “You haven’t entered data in all controls”
Exit Sub
End If
If WorksheetFunction.CountIf(Sheet1.Range(“A:A”), Me.myCtl1.Value) > 0 Then
MsgBox “Duplicate! This Admission Number is already in use!”
Exit Sub
End If
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For intmyCtl = 1 To ctlNum
Cells(erow, intmyCtl) = Me.Controls(“myCtl” & intmyCtl).Value
For intmyCtl = 1 To ctlNum
Me.Controls(“myCtl” & intmyCtl).Value = “”
End Sub

Published by

Dinesh Kumar Takyar

Welcome to! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: For a structured Excel VBA training course online you can visit:

3 thoughts on “How to code command button of user form to create database”

  1. Hello Sir,
    I am using Excel VBA to code a software for my Computer Science project. I need your help with some of the coding, how can I contact you?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.