excelenergy
Board Regular
- Joined
- Jun 7, 2012
- Messages
- 142
Hello,
I have a userform (I provided the code that controls the userform below). What I essintially want to happen, is the userform inputs its data into the excel sheet. Right now there is a field in the userform to type in the ID of the entry. I would like Excel to automatically number these (Ie: Check to see if there is an entry 1, if not, Excel automatically assigns 001 to the entry, then, when the next user makes an entry, excel will automatically number 002 and so on and so fourth.
I tried doing the auto numbering myself, but couldn't get it to work, does anyone know how to do this? Thanksss
I have a userform (I provided the code that controls the userform below). What I essintially want to happen, is the userform inputs its data into the excel sheet. Right now there is a field in the userform to type in the ID of the entry. I would like Excel to automatically number these (Ie: Check to see if there is an entry 1, if not, Excel automatically assigns 001 to the entry, then, when the next user makes an entry, excel will automatically number 002 and so on and so fourth.
I tried doing the auto numbering myself, but couldn't get it to work, does anyone know how to do this? Thanksss
Code:
Option Explicit
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("View Lessons")
'''find first empty row in database
''iRow = ws.Cells(Rows.Count, 1) _
'' .End(xlUp).Offset(1, 0).Row
'revised code to avoid problems with Excel tables in newer versions
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.txtPart.Value) = "" Then
Me.txtPart.SetFocus
MsgBox "Please enter a part number"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtPart.Value
ws.Cells(iRow, 2).Value = Me.txtLoc.Value
ws.Cells(iRow, 3).Value = Me.txtDate.Value
ws.Cells(iRow, 4).Value = Me.txtQty.Value
ws.Cells(iRow, 5).Value = Me.txtSdate.Value
ws.Cells(iRow, 6).Value = Me.txtldescription.Value
ws.Cells(iRow, 7).Value = Me.txtcauselesson.Value
ws.Cells(iRow, 8).Value = Me.txtlearned.Value
ws.Cells(iRow, 9).Value = Me.txtfirst.Value
ws.Cells(iRow, 10).Value = Me.txtlast.Value
ws.Cells(iRow, 11).Value = Me.txtemail.Value
ws.Cells(iRow, 12).Value = Me.txtphone.Value
ws.Cells(iRow, 13).Value = Me.txtBU.Value
ws.Cells(iRow, 14).Value = Me.txtBCat.Value
ws.Cells(iRow, 15).Value = Me.txtBSub.Value
ws.Cells(iRow, 16).Value = Me.txtlocation.Value
ws.Cells(iRow, 17).Value = Me.txtrisk.Value
ws.Cells(iRow, 18).Value = Me.txtattach.Value
ws.Cells(iRow, 19).Value = Me.txtophase.Value
ws.Cells(iRow, 20).Value = Me.txtphase.Value
ws.Cells(iRow, 21).Value = Me.txtaddition.Value
ws.Cells(iRow, 22).Value = Me.txtlessons.Value
ws.Cells(iRow, 23).Value = Me.txtabc.Value
ws.Cells(iRow, 24).Value = Me.txtkeywords.Value
'clear the data
Me.txtPart.Value = ""
Me.txtLoc.Value = ""
Me.txtDate.Value = ""
Me.txtQty.Value = ""
Me.txtSdate.Value = ""
Me.txtldescription.Value = ""
Me.txtcauselesson.Value = ""
Me.txtlearned.Value = ""
Me.txtfirst.Value = ""
Me.txtlast.Value = ""
Me.txtemail.Value = ""
Me.txtphone.Value = ""
Me.txtBU.Value = ""
Me.txtBCat.Value = ""
Me.txtBSub.Value = ""
Me.txtlocation.Value = ""
Me.txtrisk.Value = ""
Me.txtattach.Value = ""
Me.txtophase.Value = ""
Me.txtphase.Value = ""
Me.txtaddition.Value = ""
Me.txtlessons.Value = ""
Me.txtabc.Value = ""
Me.txtkeywords.Value = ""
Me.txtPart.SetFocus
End Sub