automatic increment of permit numbers

arjan kooger

New Member
Joined
Feb 18, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a row with different permit numbers starting with H or C, i try try figure out if it is possible that if i select a H or C in the next empty cell in column C, that it automatically put the next C or H number in that cell.

This to make it easier and avoid typo.

Would this be possible with a Sub or a function?
 

Attachments

  • PERMIT NUMBERS.JPG
    PERMIT NUMBERS.JPG
    56 KB · Views: 12

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You could try something like this based on what you type in the HOT/COLD column:
I think if you had this in a table it would automatically fill down, but if not you would have to drag the cell above down.

Book1
ABCD
1
2C0000001Cold
3H0000001Hot
4C0000002Cold
5H0000002Hot
6H0000003Hot
7C0000003Cold
8C0000004Cold
9H0000004Hot
10C0000005Cold
11C0000006Cold
12C0000007Cold
13H0000005Hot
14H0000006Hot
15H0000007Hot
16H0000008Hot
17C0000008Cold
18H0000009Hot
19C0000009Cold
20H0000010Hot
21C0000010Cold
Sheet2
Cell Formulas
RangeFormula
C2:C21C2=LET( Above,$C$1:$C1, Letter,LEFT(D2,1), List,FILTER(Above,LEFT(Above)=Letter,0),Nbrs,1*SUBSTITUTE(List,Letter,""), Output,Letter&TEXT(1+MAX(Nbrs),"0000000"),Output)
 
Upvote 0
I think @awoohaw has given you a perfectly suitable solution, however I took a different approach to your problem. With the code below, if you enter just an H or a C in column B and press enter, the next sequential number for that letter will be entered automatically. The code goes in the worksheet code area of the sheet in question (right click the sheet tab name, select view code & paste the code in the window that appears on the right of screen). Assumes your sequential numbers are in column B and that the first is in row 2.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 Then
        If Target.Column = 2 And (UCase(Target) = "H" Or UCase(Target) = "C") Then
            On Error GoTo Escape
            Application.EnableEvents = False
            Dim s As String, i As Long, j As Long, a
            s = UCase(Target)
            a = Range("B2", Target.Offset(-1))
            For i = LBound(a, 1) To UBound(a, 1)
                If UCase(Left(a(i, 1), 1)) = s Then
                    If CInt(Right(a(i, 1), 7)) > j Then j = CInt(Right(a(i, 1), 7))
                End If
            Next i
            j = j + 1
            Target.Value = s & Format(CStr(j), "0000000")
        End If
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub

When a few H & C's are entered:
Book1
B
2C0010080
3H0001775
4C0010081
5C0010082
6C0010083
7C0010084
8C0010085
9C0010086
10C0010087
11H0001776
12H0001777
13H0001778
14H0001779
15
16
17
18
Sheet1


Book1
B
2C0010080
3H0001775
4C0010081
5C0010082
6C0010083
7C0010084
8C0010085
9C0010086
10C0010087
11H0001776
12H0001777
13H0001778
14H0001779
15C0010088
16H0001780
17C0010089
18C0010090
19
Sheet1
 
Upvote 0
Solution
Both of you thank you very much for the examples!!

This was exactly what i was looking for, it was my first time to ask on a forum and did not expect such a quick reply and solution, thanks again.

For this project i go for the VBA solution, but also @awoohaw Thanks for your formula, i save it for future use since in this sheet column D (Cold and Hot) already contain a formula.


@kevin9999 This works exactly as i wanted, thank you very much. i just was not clear which column was involved (C), so i chanced it in column in 3.


1 More question: my sheet contained already a Code for auto capitalise letter in column AC, but if i intergrade part of that code in your code, excel will crash, probably because it is both worksheet change.

I don't know if it can be intergraded in your code, but if not, no problem, since we change those letter only once a day at shift change.


Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

If Not Intersect(Target, Range("AC:AC")) Is Nothing Then

Target.Value = UCase(Application.Substitute(Target.Value, " ", ""))

End If

Application.EnableEvents = True

End Sub


Thanks again both of you!


 
Upvote 0
Glad we were able to help & welcome to the Forum :) (y)
What you're asking for shouldn't be a problem, try the following code & see how you go:
EDITED

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 Then
        If Target.Column = 3 And (UCase(Target) = "H" Or UCase(Target) = "C") Then
            On Error GoTo Escape
            Application.EnableEvents = False
            Dim s As String, i As Long, j As Long, a
            s = UCase(Target)
            a = Range("C2", Target.Offset(-1))
            For i = LBound(a, 1) To UBound(a, 1)
                If UCase(Left(a(i, 1), 1)) = s Then
                    If CInt(Right(a(i, 1), 7)) > j Then j = CInt(Right(a(i, 1), 7))
                End If
            Next i
            j = j + 1
            Target.Value = s & Format(CStr(j), "0000000")
        End If
        If Target.Column = 29 Then
            Target.Value = UCase(Application.Substitute(Target.Value, " ", ""))
        End If
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0
Glad we were able to help & welcome to the Forum :) (y)
What you're asking for shouldn't be a problem, try the following code & see how you go:
EDITED

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 Then
        If Target.Column = 3 And (UCase(Target) = "H" Or UCase(Target) = "C") Then
            On Error GoTo Escape
            Application.EnableEvents = False
            Dim s As String, i As Long, j As Long, a
            s = UCase(Target)
            a = Range("C2", Target.Offset(-1))
            For i = LBound(a, 1) To UBound(a, 1)
                If UCase(Left(a(i, 1), 1)) = s Then
                    If CInt(Right(a(i, 1), 7)) > j Then j = CInt(Right(a(i, 1), 7))
                End If
            Next i
            j = j + 1
            Target.Value = s & Format(CStr(j), "0000000")
        End If
        If Target.Column = 29 Then
            Target.Value = UCase(Application.Substitute(Target.Value, " ", ""))
        End If
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
Hello Kevin9999,

Thanks for your help, i did try it but i got the same result as when i added part of the uppercase code before, it does put uppercase 1 time in the cell when i press enter, but then i got a debug/end messagebox and cannot select any, and a few seconds later Excel crashes and shutdown.

I will try it when i'm at home on my own PC, since i use corporate PC now, maybe they don't like to much tinkering, use of marco's is limited.
 
Upvote 0
It worked for me on a sample file I created - are you able to share your actual file via Google Drive, Dropbox or similar file sharing platform so we can get to the bottom of the problem?
 
Upvote 0
It worked for me on a sample file I created - are you able to share your actual file via Google Drive, Dropbox or similar file sharing platform so we can get to the bottom of the problem?
I have to find out a file share service, its been a long time since i used it. i prepared a example copy of the file, but since the company restrict use of macro ( as user you have to request activation of macros for your account), i have some other little programs who do not run nicely or let Excel crash, although other Excel files with a lot of macros run fine. what i see now that it seems to enter a loop of some sort (mouse pointer is flickering) and then the debug message box pops up and excel start to shutdown.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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