VBA Find Part Number and highest Serial Number and add 1 to last row

yager9110

New Member
Joined
Jul 28, 2016
Messages
2
I am having trouble coming up with a VBA code to find a part number I am searching for and then finding the highest (or last row) serial number, adding 1 to that number and creating a new line item.

Part NumberSerial NumberDescription
5462H023Pan Head Screw
6524X018Washer

<tbody>
</tbody>


I want it to find "5462H" and then realize the last serial number input was 023. Create a new line shown below:

Part NumberSerial NumberDescription
5462H023Pan Head Screw
6524X018Washer
5462H024Pan Head Screw

<tbody>
</tbody>

I am not inputting in text, just a click of a command button with the label "5462H"

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Sub newrow()
Dim x As Variant
x = Cells(4, 6).Value
Dim lastrow As Long
Dim lastColumn As Long
Application.ScreenUpdating = False
lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
For i = lastrow To 2 Step -1
If Cells(i, 1).Value = x Then
Cells(lastrow + 1, 1).Value = Cells(i, 1).Value
Cells(lastrow + 1, 2).Value = WorksheetFunction.Text(1 + 1 * Cells(i, 2).Value, "00#")
Cells(lastrow + 1, 3).Value = Cells(i, 3).Value
Exit For

End If
Next

End Sub



Place your required value in F4
 
Upvote 0
Sub newrow()
Dim x As Variant
x = Cells(4, 6).Value
Dim lastrow As Long
Dim lastColumn As Long
Application.ScreenUpdating = False
lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
For i = lastrow To 2 Step -1
If Cells(i, 1).Value = x Then
Cells(lastrow + 1, 1).Value = Cells(i, 1).Value
Cells(lastrow + 1, 2).Value = WorksheetFunction.Text(1 + 1 * Cells(i, 2).Value, "00#")
Cells(lastrow + 1, 3).Value = Cells(i, 3).Value
Exit For

End If
Next

End Sub



Place your required value in F4


Is there a way to search for highest value rather than just the last row in the table? I thought that would work, but I am realizing now serial numbers may be entered later causing an issue with the way it is currently set up.
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,342
Members
448,570
Latest member
rik81h

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