Excel VBA Form: Add an Incrementing Serial Number

scottmcclean

New Member
Joined
Jul 2, 2014
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi there,
I have an Excel VBA form that populates a sheet. The problem that I could do with some help on is that I would like to be able to have a text box that adds an incrementing serial number. I can't figure out how to add the code that can look at the last completed row in the sheet, then return the value of the last serial number then add 1. If this is possible I would really appreciate some help!

Many thanks
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Assumming serial numbers are numeric and in Column A and the data starts in A1. Sheet is Sheet1

Code:
 Dim rData As Range


    Set rData = Sheet1.Range("A1").CurrentRegion
   
    rData.Rows.Count.Offset(1).Value = Application.WorksheetFunction.Max(rData.Columns(1)) + 1
 
Upvote 0
Hi Roy,
thank you for replying.how do I relate the code to a text box on my form so that it adds in the serial number with the rest of the form data?
 
Upvote 0
You don't really need a TextBox for the ID but I would have a TextBox on the Form & when you add data or load the Form place the ID in it using the code

Code:
Option Explicit


Dim rData As Range


Private Sub CommandButton1_Click()
    Cells(rData.Rows.Count + 1, 1).Value = Me.TextBox1.Value
    Set rData = Sheet1.Range("A1").CurrentRegion
    Me.TextBox1.Value = Format(WorksheetFunction.Max(rData.Columns(1)) + 1, "0000")
End Sub


Private Sub UserForm_Initialize()
    Set rData = Sheet1.Range("A1").CurrentRegion
    Me.TextBox1.Value = Format(WorksheetFunction.Max(rData.Columns(1)) + 1, "0000")
End Sub
 
Upvote 0
you're a genius. works superbly.
thank you very much, seriously appreciated.
 
Upvote 0
Glad it helped. You should apply the same method of declaring the data range to apply the other TextBoxes, see my DatbaseForm example.
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,114,002
Members
448,543
Latest member
MartinLarkin

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