Excel Form to Excel Table
The following workbook shows how a custom user form can be created (with validation) as a means of controlling the entry of data into a specified Sheet. Here, data is exported directly into an Excel Table.
Screenshots
Form Object VBA
VBA
'Variable Declaration
Dim BlnVal As Boolean
Private Sub txtSheet_Name_Change()
End Sub
Private Sub UserForm_Initialize()
'Variable declaration
Dim IdVal As Integer
Dim this_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim lastRow As Range
'Update the combo boxes
For Each Item In [SheetNames]
Me.txtSheet_Name.AddItem Item
Next Item
'Set the Sheet Variable to the Sheet called "Table"
Set this_sheet = Sheets("Sheet 1")
Set table_list_object = this_sheet.ListObjects(1)
'Finding last row in the Data Sheet
IdVal = table_list_object.ListRows.Count + 1
'Update next available id on the userform
frmData.txtId = IdVal
End Sub
Sub cmdAdd_Click()
On Error GoTo ErrOccured
'Boolean Value
BlnVal = 0
'Data Validation
Call Data_Validation
'Check validation of all fields are completed are not
If BlnVal = 0 Then Exit Sub
'TurnOff screen updating
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Variable declaration
Dim txtId, txtName, GenderValue, txtLocation, txtCNum, txtEAddr, txtRemarks
'Find Gender value
If frmData.obMale = True Then
GenderValue = "Male"
Else
GenderValue = "Female"
End If
'Update userform data to the Data Worksheet
Dim this_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow
Dim last_Row As Range
Dim Sheet_name As String
'Set the Sheet Variable to the chosen sheet
Sheet_name = frmData.txtSheet_Name
'Assign values to the Sheet variable and Table List Object Variables
Set this_sheet = Sheets(Sheet_name)
Set table_list_object = this_sheet.ListObjects(1)
'Set the Table Object variable to be the number of rows plus 1
Set table_object_row = table_list_object.ListRows.Add
'Now update this row in the table
table_object_row.Range(1, 1).Value = table_list_object.ListRows.Count
table_object_row.Range(1, 2).Value = frmData.txtName
table_object_row.Range(1, 3).Value = GenderValue
table_object_row.Range(1, 4).Value = frmData.txtLocation.Value
table_object_row.Range(1, 5).Value = frmData.txtEAddr
table_object_row.Range(1, 6).Value = frmData.txtCNum
table_object_row.Range(1, 7).Value = frmData.txtRemarks
'Display the next available ID number on the UserForm
'Variable declaration
Dim IdVal As Integer
'Find the last row in the Table
IdVal = table_list_object.ListRows.Count + 1
'Update next available Id on the userform
frmData.txtId = IdVal
'Output a message that displays the number of rows now in the table
MsgBox ("Record #: " & table_list_object.ListRows.Count & " has been successfully added" & " to the Sheet: " & Sheet_name)
ErrOccured:
'TurnOn screen updating
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
'Exit from the Userform
Private Sub cmdCancel_Click()
Unload Me
End Sub
' Check all the data(except remarks field) has entered are not on the userform
Sub Data_Validation()
If txtName = "" Then
MsgBox "Enter Name!", vbInformation, "Name"
Exit Sub
ElseIf frmData.obMale = False And frmData.obFMale = False Then
MsgBox "Select Gender!", vbInformation, "Gender"
Exit Sub
ElseIf txtLocation = "" Then
MsgBox "Enter Location!", vbInformation, "Location"
Exit Sub
ElseIf txtEAddr = "" Then
MsgBox "Enter Address!", vbInformation, "Email Address"
Exit Sub
ElseIf txtCNum = "" Then
MsgBox "Enter Contact Number!", vbInformation, "Contact Number"
Exit Sub
ElseIf txtSheet_Name = "" Then
MsgBox "Choose Sheet Name!", vbInformation, "Sheet Name"
Exit Sub
Else
BlnVal = 1
End If
End Sub
'Clearing data fields of userform
Private Sub cmdClear_Click()
Application.ScreenUpdating = False
txtId.Text = ""
txtName.Text = ""
obMale.Value = True
txtLocation = ""
txtEAddr = ""
txtCNum = ""
txtRemarks = ""
txtSheet_Name = ""
Application.ScreenUpdating = True
End Sub
Module VBA
VBA
Sub Oval2_Click()
frmData.Show
End Sub
Sub Clear_DataSheet()
Dim this_sheet As Worksheet
Set this_sheet = ActiveSheet
With this_sheet.ListObjects(1)
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If
End With
End Sub
Download
The full file can be downloaded here:
Feedback
Submit and view feedback