Add next row to table using next sequential number of highest number in Column A

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
564
Office Version
  1. 365
Platform
  1. 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.

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.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I've managed to get through this one with the code below. Working on a slightly different problem. If I can't get it, I will create a new post for it as well.

VBA Code:
Private Sub CommandButton1_Click()
    Dim tbl As ListObject
    Dim rng As Range
    Dim My_Value As Integer
    
    Set tbl = ActiveSheet.ListObjects("Table1")
    Set rng = tbl.ListColumns("Job No").DataBodyRange
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    Dim Lastrow As Integer
        With ws
        
        My_Value = Application.WorksheetFunction.Max(rng) + 1
        
        TextBox1.Text = My_Value
        
        Lastrow = Sheet1.Cells(Sheet1.Rows.Count, "B").End(xlUp).Row + 1
        
        .Cells(Lastrow, 1).Value = TextBox1.Text
        .Cells(Lastrow, 2).Value = TextBox2.Text
        .Cells(Lastrow, 3).Value = TextBox3.Text
        .Cells(Lastrow, 4).Value = TextBox4.Text
        End With
        
        
        TextBox1.Value = ""
        TextBox2.Value = ""
        TextBox3.Value = ""
        TextBox4.Value = ""
        
        


End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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