VBA code for Entering data into a table through userform

Rakeshdimpal

Board Regular
Joined
Nov 2, 2015
Messages
65
I need a VBA Code for entering data into a table having 4 column namely Name, Date, Amount and Mobile number.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm assuming you would have text boxes on this userform (NameTextBox, DateTextBox, AmountTextBox, MobileNumberTextBox, etc.. whatever you name them)
So when you click your button on the userform to add the data, you have to find the next empty row, paste the data from the userform into the table, and then clear the userform:


'This is your userform code for the button you click to paste the data to the table
Private Sub UserformButton_Click()
Call Module2.DataEntry
With UserformName
.NameTextBox.Text = ""
.DateTextBox.Text = ""
.AmountTextBox.Text = ""
.MobileNumberTextBox.Text = ""
End With
End Sub




'This Sub is in your module
Sub DataEntry()
Dim n As Integer: n = 2
Do Until IsEmpty(Worksheets("Sheet1").Cells(n, 1))
n = n + 1
Loop


With Worksheets("Sheet1")
.Cells(n, 1) = UserformName.NameTextBox.Text
.Cells(n, 2) = UserformName.DateTextBox.Text
.Cells(n, 3) = UserformName.AmountTextBox.Text
.Cells(n, 4) = UserformName.MobileNumberTextBox.Text
End With
End Sub
 
Upvote 0
I'm assuming you would have text boxes on this userform (NameTextBox, DateTextBox, AmountTextBox, MobileNumberTextBox, etc.. whatever you name them)
So when you click your button on the userform to add the data, you have to find the next empty row, paste the data from the userform into the table, and then clear the userform:


'This is your userform code for the button you click to paste the data to the table
Private Sub UserformButton_Click()
Call Module2.DataEntry
With UserformName
.NameTextBox.Text = ""
.DateTextBox.Text = ""
.AmountTextBox.Text = ""
.MobileNumberTextBox.Text = ""
End With
End Sub




'This Sub is in your module
Sub DataEntry()
Dim n As Integer: n = 2
Do Until IsEmpty(Worksheets("Sheet1").Cells(n, 1))
n = n + 1
Loop


With Worksheets("Sheet1")
.Cells(n, 1) = UserformName.NameTextBox.Text
.Cells(n, 2) = UserformName.DateTextBox.Text
.Cells(n, 3) = UserformName.AmountTextBox.Text
.Cells(n, 4) = UserformName.MobileNumberTextBox.Text
End With
End Sub

This code works fine and well. But in certain circumstance where the Table have its Total row at bottom, then this code paste the data below Total row i.e, range and not in a Table
 
Upvote 0
Also i need to clear entry in all the text box when i click on command button to paste data to new row automatically so that i need not to delete previous entry in text box to enter a new entry.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top