VBA code that find next number

Jagat Pavasia

Active Member
Joined
Mar 9, 2015
Messages
404
Office Version
  1. 2021
Platform
  1. Windows
Capture.JPG


Dear Sir,

I have Excel sheet as shown in Image.
My raw "G3:G1000" has space for serial number.

I want that if I enter any empty space in G3 to G1000 than it see last number and give me next number by entering "**" in G3:G1000.

For Example (As Above Image):

I enter "**" in G5 than result should be "6" in G5, and after that if I enter in G12 than result shoub be next number "7".

please help with VBA code
 
Try this (untested) :
VBA Code:
Dim rng As Range, cel As Range, highVal&, celVal&
Set rng = Range("G3:G" & Cells(Rows.Count, "G").End(3).Row)
highVal = 0

'Other code

ElseIf Not Intersect([G:G], target) Is Nothing Then
    If target = "**" Then
        For Each cel In rng
            celVal = Val(Replace(cel, "K-", ""))
            If celVal > highVal Then highVal = celVal
        Next
    End If
    target = "K-" & highVal
    
'Other code
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this (untested) :
VBA Code:
Dim rng As Range, cel As Range, highVal&, celVal&
Set rng = Range("G3:G" & Cells(Rows.Count, "G").End(3).Row)
highVal = 0

'Other code

ElseIf Not Intersect([G:G], target) Is Nothing Then
    If target = "**" Then
        For Each cel In rng
            celVal = Val(Replace(cel, "K-", ""))
            If celVal > highVal Then highVal = celVal
        Next
    End If
    target = "K-" & highVal
   
'Other code
not working
 
Upvote 0
Because I did not that how to merge this VBA code in to .
please give me full VBA code, so that I can paste to my sheet
Is your full code the same as the other sheet?
You said previously "it is not working". Please clarify whether it is not working or you don't know how to add the new code.
It is quite simple. Add the declarations and replace the last "Elseif" with the new "Elseif" code.
 
Upvote 0
Is your full code the same as the other sheet?
You said previously "it is not working". Please clarify whether it is not working or you don't know how to add the new code.
It is quite simple. Add the declarations and replace the last "Elseif" with the new "Elseif" code.
I don't know how to add the new code
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Application.EnableEvents = False
Me.Unprotect
If Not Intersect(Target, [B:B,L:L]) Is Nothing Then
    If Target.Value = "**" Then
        Target.Value = Date
        Target.NumberFormat = "DD/MM/YYYY"
    End If
ElseIf Not Intersect(Target, [E1]) Is Nothing Then
    If Target.Value = "" Then
        Me.Range("A3:U3").AutoFilter Field:=Target.Column
    Else
        Me.Range("A3:U3").AutoFilter Field:=Target.Column, _
            Operator:=xlFilterValues, _
            Criteria1:="*" & CStr(Target.Value) & "*"
    End If
ElseIf Not Intersect(Target, [A1:V1]) Is Nothing Then
    If Target.Value = "" Then
        Me.Range("A3:U3").AutoFilter Field:=Target.Column
    Else
        Me.Range("A3:U3").AutoFilter Field:=Target.Column, _
            Operator:=xlFilterValues, _
            Criteria1:=CStr(Target.Value)
    End If
ElseIf Not Intersect([G:G], Target) Is Nothing Then
    If Target = "**" Then
        Dim rng As Range, cel As Range, highVal&, celVal&
        Set rng = Range("G3:G" & Cells(Rows.Count, "G").End(3).Row)
        highVal = 0
        For Each cel In rng
            celVal = Val(Replace(cel, "K-", ""))
            If celVal > highVal Then highVal = celVal
        Next
    End If
    Target = "K-" & highVal
End If
Me.Protect DrawingObjects:=False, Contents:=True, _
           Scenarios:=False, AllowFormattingCells:=True, _
           AllowFiltering:=True
Application.EnableEvents = True
End Sub
 
Upvote 0
Because I had already manually typed K-1, K-2 .....to.................K-1233.
now, I changed format of all cell, and type "**" in empty cell but it did not working.

so, I told you,,,dear..footoo
You could select the blank cells (via SpecialCells) and format as : "K-"0
 
Upvote 0

Forum statistics

Threads
1,221,519
Messages
6,160,285
Members
451,635
Latest member
nithchun

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