mercredi 14 février 2018

Code for number count, date and generate random code for user database in Userform

First, apologize that i'm very noob.

I want to create a user database for my personal stuff but i want to make it using userform in excel-vba as i think it will be great for me to learn coding.

Here is the userform that i've created :

Userform1

In first textbox, i will insert user's name. Second textbox is their ID.

But the third textbox i want to generate 5 digit code by clicking the 'Generate' button (but i have no idea what is the coding)

Once i clicked 'Add user', i would like the data to be populated in the excel sheet.

So here what i want the database looks like : database example

Here is the code that i managed to copied from someone in the forum.

Private Sub CommandButton2_Click()

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database")

'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
 SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'check for a Name number
If Trim(Me.TextBox1.Value) = "" Then
 Me.TextBox1.SetFocus
 MsgBox "Please complete the form"
 Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.TextBox1.Value
ws.Cells(iRow, 2).Value = Me.TextBox2.Value
ws.Cells(iRow, 3).Value = Me.TextBox3.Value

MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"

'clear the data
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox1.SetFocus

End Sub

1- My first challenge is i want to generate 5 digit ID/code(mix number&alphabet) in the third textbox by clicking the 'Generate' button.

2- Once i click 'Add user' button, i would like the database to insert number 1,2,3... in Column A, today's date (when the user details added) in Column B. Followed by the data added in the Userform in Column C,D & E.

Could someone please help me with the code.

Thank you so much.




Aucun commentaire:

Enregistrer un commentaire