Find the maximum or large of a string ID

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Say I have
Code:
CPI_101
CPI_109


And more as IDs in my database . Can I use vba to find the largest number part?

It's looking trick for me.

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
If they are all the same format, you don't need VBA


Excel 2013/2016
AB
1CPI_101109
2CPI_109
3CPI_100
4CPI_099
5CPI_106
Cover
Cell Formulas
RangeFormula
B1{=MAX(--RIGHT(A1:A5,3))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Say I have
Code:
CPI_101
CPI_109


And more as IDs in my database . Can I use vba to find the largest number part?

It's looking trick for me.

Thanks
Here's a UDF you can try. After you install it, you can use it like a worksheet function - see the example below. It assumes your Part IDs always have the format: string_number. You must have at least one cell in the range input that has this format or the UDF will return an error (#VALUE!).
Code:
Function LargestPartID(R As Range) As String
Dim V As Variant, i As Long, x As Double, MaxVal As Double, Prefix As String
V = R.Value
For i = 1 To UBound(V, 1)
    If InStr(V(i, 1), "_") > 0 Then
        x = Replace(V(i, 1), Mid(V(i, 1), 1, InStr(V(i, 1), "_")), "")
        If x > MaxVal Then
            MaxVal = x
            Prefix = Mid(V(i, 1), 1, InStr(V(i, 1), "_"))
        End If
    End If
Next i
If MaxVal > 0 Then
    LargestPartID = Prefix & MaxVal
Else
    LargestPartID = CVErr(xlErrValue)
End If
End Function
Excel Workbook
ABC
1Part IDLargest
2CPI_101XYZ_117
3CPI_109
4
5XYZ_117
6CPI_100
Sheet4
 
Upvote 0
If they are all the same format, you don't need VBA

Excel 2013/2016
AB
CPI_101
CPI_109
CPI_100
CPI_099
CPI_106

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: right"]109[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"][/TD]

[TD="align: center"]3[/TD]

[TD="align: right"][/TD]

[TD="align: center"]4[/TD]

[TD="align: right"][/TD]

[TD="align: center"]5[/TD]

[TD="align: right"][/TD]

</tbody>
Cover

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Array Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]B1[/TH]
[TD="align: left"]{=MAX(--RIGHT(A1:A5,3))}[/TD]
[/TR]
</tbody>[/TABLE]
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself[/TD]
[/TR]
</tbody>[/TABLE]


Okay cool.

Can we use the "CPI_" to determine how many characters at the right?

By taking the length of "CPI_" from it then use the rest as the right character.

Because they will not all have same number of characters.

Thanks
 
Upvote 0
As long as there is only one _ in the string you could use


Excel 2013/2016
AB
1CPI_1011001
2CPI_109
3CPI_1001
4CPI_99
5CPI_11
Cover
Cell Formulas
RangeFormula
B1{=MAX(--RIGHT(A1:A5,LEN(A1:A5)-FIND("_",A1:A5)))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Or, in code
Code:
Sub kelly_mort()
   Dim x As Variant
   Dim Addr As String
   Addr = Range("A1", Range("A" & Rows.count).End(xlUp)).Address
   x = Evaluate("max(--right(" & Addr & ",len(" & Addr & ")-find(""_""," & Addr & ")))")
End Sub
Or use JoeMo' code as that allows for blank cells
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Or, in code
Code:
Sub kelly_mort()
   Dim x As Variant
   Dim Addr As String
   Addr = Range("A1", Range("A" & Rows.count).End(xlUp)).Address
   x = Evaluate("max(--right(" & Addr & ",len(" & Addr & ")-find(""_""," & Addr & ")))")
End Sub
Or use JoeMo' code as that allows for blank cells


Yes I observed that blank cell issue with your formula.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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