December 29, 2017

Data Entry With UserForm Vlookup Countif Dynamic Named Ranges

How to make data entries with User-Form, Vlookup, Countif and Dynamic Named Ranges to create databases quickly and easily. Watch the video below:

 

Watch this video on YouTube.

Here’s the complete VBA code:

Module1

Sub showItemForm()
frmItem.Show
frmItem.tb1.SetFocus

End Sub

Module2

Sub showCustomerForm()
frmCustomer.Show
frmCustomer.tb1.SetFocus

End Sub

frmCustomer

Customer Details form
Customer Details form

Private Sub cmdAddCustomer_Click()
Sheet2.Activate
erow = Sheet2.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
Cells(erow, 3) = tb1.Value
Cells(erow, 4) = tb2.Value
Cells(erow, 5) = tb3.Value
Cells(erow, 6) = tb4.Value
Cells(erow, 7) = tb5.Value
Cells(erow, 8) = tb6.Value
End Sub

Private Sub cmdClear_Click()

For i = 1 To 6
Me.Controls(“tb” & i) = “”
Next

End Sub

Private Sub CommandButton1_Click()

Unload Me

End Sub

Private Sub tb1_AfterUpdate()
Sheet2.Activate
If WorksheetFunction.CountIf(Sheet2.Range(“C:C”), Me.tb1.Value) = 0 Then
MsgBox “Wish to add this new customer?”, vbOKCancel, “Add Customer”
End If
If vbOK Then
cmdAddCustomer.Visible = True
Me.tb2.SetFocus
‘Exit Sub
End If

If WorksheetFunction.CountIf(Sheet2.Range(“C:C”), Me.tb1.Value) > 0 Then
cmdAddCustomer.Visible = False
MsgBox “Customer already exists! The data is displayed.”

With Me
.tb2 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet2.Range(“customers”), 2, 0)
.tb3 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet2.Range(“customers”), 3, 0)
.tb4 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet2.Range(“customers”), 4, 0)
.tb5 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet2.Range(“customers”), 5, 0)
.tb6 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet2.Range(“customers”), 6, 0)
End With

End If

End Sub

Private Sub UserForm_Initialize()
tb1.SetFocus
End Sub

frmItems

Items Form
Form for items

Private Sub cmdAddItem_Click()

Dim erow As Long
Sheet3.Activate
erow = Sheet3.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Row
Cells(erow, 4) = tb1.Value
Cells(erow, 5) = tb2.Value
Cells(erow, 6) = Val(tb2) * (Val(tb3) / 100)
Cells(erow, 7) = Cells(erow, 6) / 2
Cells(erow, 8) = Cells(erow, 6) / 2
Cells(erow, 9) = Cells(erow, 5) + Cells(erow, 6)
Cells(erow, 10) = tb4.Value

End Sub

Private Sub cmdClear_Click()
For i = 1 To 4

Me.Controls(“tb” & i) = “”

Next

End Sub

Private Sub CommandButton1_Click()
Unload Me

End Sub

Private Sub tb1_AfterUpdate()
Sheet3.Activate
If WorksheetFunction.CountIf(Sheet3.Range(“D:D”), Me.tb1.Value) = 0 Then
MsgBox “Wish to add this new item?”, vbOKCancel, “Add Item”
End If
If vbOK Then
cmdAddItem.Visible = True
Me.tb2.SetFocus
End If

If WorksheetFunction.CountIf(Sheet3.Range(“D:D”), Me.tb1.Value) > 0 Then
cmdAddItem.Visible = False
MsgBox “Item already exists! The data is displayed.”
With Me
.tb2 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet3.Range(“items”), 2, 0)
.tb3 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet3.Range(“items”), 3, 0)
.tb4 = Application.WorksheetFunction.VLookup((Me.tb1), Sheet3.Range(“items”), 7, 0)
End With
End If
End Sub

 

Private Sub UserForm_Initialize()
tb1.SetFocus
End Sub

Further reading:

Refer to Named Range Excel VBA

Dynamic Named Ranges Excel

Using Offset Property in VBA

Vlookup Excel VBA

How to use Vlookup to get multiple values

 

 

 

 

5 thoughts on “Data Entry With UserForm Vlookup Countif Dynamic Named Ranges

  1. Hi Sir,
    Myself Manikandan, i recently tried to work on Excel VBA for the below one, but am getting issues with the last row
    “vba excel to get values to text box based on combobox”

    Could you please send me Excel file with Macro, that will be helpful very much.

    This is my kind request

  2. Dear Sir, I need to make a vba form to get input information and save that in master sheet along with that I want to generate invoice copy with selected record to make print. But the problem I faced each item has no of batches somtime one somtimes 5 with manufacturering and expiry information. If items are more than 3 and batches 2,3 4 for each how I will configure 2nd page.
    If any thing not clear please let me know.

  3. Just i want to say that, problem is i have a data vehicle no.wise 1 to 30 , total 8 vehicle, arrange the data all 8 vehicle different sheet wise. Total 8 sheet required and data is 1 t0 30.. how is possible can u create vba pls…Thanks

  4. Hello sir,

    I am creating a excel VBA userform in which I have information that I want to pull it from another sheet and few information I have to add manually feeding but now I want all this information from this form to get saved in a new sheet. I need your help has I am not that good with excel VBA.
    Hope you can help me to solve this problem

    Thanks
    Pradeep

Comments are closed.