Automatically insert an incrementing number, but allow manual override

offbyone

New Member
Joined
Jun 8, 2019
Messages
5
If possible, I'd like to have Excel insert a number that increments by one in the first column of every new row in a given table like so:
Code:
GID   FN       LN
800   John    Smith
801   Jill    Rogers
802   Sam     Spade
803   Sally   Spade
804   Bill    Bayes
but that will also allow for manual override by the user, like so:
Code:
800
801
802
802 <--- user changed this to 802, which is the same number as cell above
803 <--- incrementing resumed by incrementing cell above by 1
This is like how QuickBooks will automatically increment check and invoice numbers, though it allows the number to be changed and will increment the next one based on the number manually entered.

I tried formulas like:
Code:
=A1+1
and:
Code:
=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1
but these caused errors when sorting.

I also tried Excel's auto incrementing number feature, but have not been able to make it automatically continue the series when a new table row is added.

Does anyone know of a solution?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to the forum :)

It is not possible to do what you want by formula
Here is a VBA solution

right-click on sheet tab \ select View Code \ paste code below into code window \ {ALT}{{F11} to go back to Excel
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub
        
    If Not Intersect(Range("A3:A" & Rows.Count), Target) Is Nothing Then
        If IsNumeric(Target) Then Target = Target.Offset(-1) + 1
    End If
           
End Sub

Save the workbook as macro-enabled
 
Last edited:
Upvote 0
Thanks so much for the warm welcome and your fast reply, Yongle.

Thank you also for the VBA code; it appeared to do just the trick at first, but there is an unfortunate quirk: cell values in column A that have been manually set change again if highlighted.

Is there some way for the assignments to be set so that they do not change again if merely highlighted?
 
Upvote 0
Replace :
Code:
If IsNumeric(Target) Then Target = Target.Offset(-1) + 1

With :
Code:
If Target = "" And IsNumeric(Target.Offset(-1)) Then Target = Target.Offset(-1) + 1
 
Upvote 0
Thanks so much, Yongle - you are a wizard! It now works exactly as desired.


Thanks again!
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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