sspatriots
Well-known Member
- Joined
- Nov 22, 2011
- Messages
- 585
- Office Version
- 365
- Platform
- Windows
Hi,
I have this table called "Table1' that has 4 columns labeled "Job No", "First Name", "Last Name" and "Occupation" from left to right. The numbers in column A are all 4 digits and can be in any order due to users sorting on the table and saving. I need my code below to look at the highest 4 digit number in the column "Job No" and sequentially add a value of "1" to it and assign that number from my UserForm when a new record is being added. Right now I have the following Textboxes that correspond to the columns above:
Textbox1 for "First Name"
Textbox2 for "Last Name"
Textbox3 for "Occupation"
Below is what I have for the code so far, but I need to plug a fourth textbox in for the "Job No" and get this code to pull the highest number in that column and add one for the new record.
I think I am on the right track, but lost at this point. Any suggestions greatly appreciated. Thanks.
I have this table called "Table1' that has 4 columns labeled "Job No", "First Name", "Last Name" and "Occupation" from left to right. The numbers in column A are all 4 digits and can be in any order due to users sorting on the table and saving. I need my code below to look at the highest 4 digit number in the column "Job No" and sequentially add a value of "1" to it and assign that number from my UserForm when a new record is being added. Right now I have the following Textboxes that correspond to the columns above:
Textbox1 for "First Name"
Textbox2 for "Last Name"
Textbox3 for "Occupation"
Below is what I have for the code so far, but I need to plug a fourth textbox in for the "Job No" and get this code to pull the highest number in that column and add one for the new record.
VBA Code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Dim Lastrow As Integer
With ws
Lastrow = Sheet1.Cells(Sheet1.Rows.Count, "B").End(xlUp).Row + 1
.Cells(Lastrow, 2).Value = TextBox1.Text
.Cells(Lastrow, 3).Value = TextBox2.Text
.Cells(Lastrow, 4).Value = TextBox3.Text
End With
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
'Autoincrement code vba
'Find Lastrow
Lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
For x = 1 To 11
lastnum = Right(ws.Cells(x, 1), 4)
If Lastrow >= 1 Then
Sheet1.Range("A" & Lastrow).Value = Lastrow - 1
End If
Next x
lastnum = Format(lastnum + 1, "000#")
End Sub
I think I am on the right track, but lost at this point. Any suggestions greatly appreciated. Thanks.