Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

To make auto-fill serial number

0

Currently, I am using an Excel Macro-Enabled Workbook with Auto-time stamp features. I hope to add in a new function that can auto-fill the serial number when I insert a new row for fresh entry.

I attached the "Auto fill serial number example 1" in which "A3:H8" was the latest status. Currently the serial number is input manually.

"A14:H20", which duplicted "A3:H8", showed a new inserted row in Row 16. I am hoping someone can help me to make an auto-fill serial number like the "auto-time Stamp"

Thankyou in advance.

Answer
Discuss

Discussion

Hi Victor 

Can't answer now (mobile) but are you sure you want a serial numbers as simple as 2 (or 3) digits?
John_Ru (rep: 6142) Jul 23, '23 at 11:26 am
Hi John_Ru. No, this example is a simplifed verson. The serial number goes up to 4 digits. You may noticed that in the current code for the Auto Timestamp, myTableRange is ("A2:D10000").   Thank you.
Victor (rep: 12) Jul 23, '23 at 9:14 pm
Add to Discussion

Answers

0
Selected Answer

Hello Victor and welcome to the forum,

First, I want to tthank you for clearly explaining what you want to achieve and for the sample file which also makes it clear what you gaol is. Your code for Sheet1 is also well done using comments to explain each step. Doing so is very helpful if you (or someone else) need to modify the code a month, 6 months, a year later. It would be great if all posters did as good of a job as you have.

Now to answer your question. There are 3 steps to achieving this. 1) Find the last used row in column "H"; 2) find the MAX value in "H2:H lastrow" and add 1 to it; 3) enter this value in "H" of the target row.

This will require adding 2 variables at the top of your code:

' ==============================
Dim LR As Long   ' last used row in column "H"
Dim NewSerial As Long   ' serial number which will be entered in column "H" of new row
' ==============================

Then, add these lines before EnableEvents:

~ ~ UPDATED: July 24/23 ~ ~

To eliminate the problem of repeated serial number changes (as described below), two condition checks have been added.  1) do cells A, B, C, and D in target row have data;  2) is there already a serial in H of target row  (code below has been updated to nclude these)

' ==============================
' check that cells A, B, C, and D have data >> if not - skip entering serial number
If WorksheetFunction.CountBlank(Range("A" & target.Row & ":D" & target.Row)) > 0 Then GoTo SkipSerial
' check if cell "H" of target row already has a serial >> if so - skip entering serial number
If Range("H" & target.Row) <> "" Then GoTo SkipSerial
' Find Last Used Row In Column " H "
LR = Cells(Rows.Count, 8).End(xlUp).Row
' Determine New Serial Number - - - MAX + 1
NewSerial = WorksheetFunction.Max(Range("H2:H" & LR)) + 1
' Enter New Serial Number Into Column " H "
Range("H" & target.Row) = NewSerial
' serial number is not entered
SkipSerial:
' ==============================

If this solves your problem please mark my answer as selected.

Cheers   :-)

Discuss

Discussion

Thank you WillieD24 for your reply with the added code. But something went as not expected:   1) after I inserted a new row at Row 5, that serial number appeared as 51. but, 2) add data in cell "A5", that number became 52. 3) add data in "B5", it became 53. 4) add data in "C5", it became 54. 5) add data in "D5", it 55. 6) add data in "E5", it remained 55. I have some feeling that it is due to myTableRange is ("A2:D10000"). So modification in col E does not effect the serial number.   7) after I modified data in "D8",  the exiting "H8" serial number changed from 47 to 56.   8) after I deleted row 7,  the now new row 7's  serial number changed further to 57.   I need to modify data, delete row/s from time to time.   Thank you.
Victor (rep: 12) Jul 23, '23 at 9:16 pm
@Victor
That's my fault for not remembering your code is in the Worksheet_Change event. That's why with every change you make in "A" to "D" (your table range) the serial number of the target row is incremented by one.
The quick solution is to make the extra lines their own macro in a module which would be triggered by a button click. Or, add a pop-up asking "Add Serial No ?"  Yes/No. But that would cause the pop-up after each change in the target row, which could be a bit annoying.
Which method do you prefer?

Cheers   :-)
WillieD24 (rep: 557) Jul 24, '23 at 12:27 am
@Victor
I realized there was a simpler way to overcome the repeated serial changes (see update above). This time I tested the code and it works for me.

Cheers   :-)
WillieD24 (rep: 557) Jul 24, '23 at 8:10 am
Hi WillieD24,
Many thanks. This is the solution I needed.
Best regards
Victor (rep: 12) Jul 24, '23 at 12:22 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login