T
Tom
Marshall,
If I could ask for your assistance again....
I do have some very short strings I would like to accommodate, e.g.
1PG or 3PG or 10PG or 11PG, etc. These examples are sorting after
1??PG strings. The below code works very well except for part
numbers like 1PG or 11PG or 1-12345 or 11-12345. I have taken
your rules and attempted to modify them. Hopefully, they make sense for my
needs.
I deal with part numbers that begin all numeric, numeric with non-numeric
characters at the end, numeric that may have a non-numeric
character somewhere after the first numeric character. Then, I have
numbers that start with characters, continue with numeric and may end with
non-numeric characters. I have tried to modify your rules accordingly:
1) The initial text will be from 0 to 4 characters ending with a
non-numeric character(s).
To me, zero characters means the string begins numeric.
Numeric beginning strings may have an alpha or a hyphen or a DOT somewhere
after the first numeric digit, e.g. 1-12345 or 10-12345 or 100-12345 or
100.123456 or 45?12345 and needs to sort according to the number of digits
before the dash or DOT, e.g. 1-????? before 2-????? before 1?-????? before
1??-?????, etc. The 45?12345 example seems to sort just fine with the code
below.
Some numbers can have multiple hyphens and some numbers can have both
hyphens and a DOT.
2) The numeric part can be up to 12 digits and immediately follows the
initial text part. The numeric part may or may not be followed by a
trailing text part that starts with any non-numeric character.
3) The trailing text part is any remainder of the string starting with the
first non-numeric character after the numeric part.
If you could, please comment the code a little more.
If a string template would help, let me know!
Thanks so much!
Your previous reply:
Your new examples do not conform to those rules, so you must
come up with a different set of rules. My **guess** for the
rules is now:
1) The initial text will be from 0 to 3 characters ending
with a non-numeric character.
2) The numeric part can be up to 7 digits and immediately
follows the initial text part. The numeric part may or may
not be followed by a trailing text part that starts with any
non-numeric character.
3) The trailing text part is any remainder of the string
starting with the first non-numeric character after the
numeric part.
You must decide if my guesses at the rules is valid for all
of your various situations. Pay particular attention to the
3 that I used. If you might get very short strings (e.g. X3
or just 12) then you will need to modify rule 1) and the
code to accommodate the very short strings.
With all that said, here's my guess at a new procedure:
Public Function StandardizePartNum(PartNum)
Dim k As Integer, j As Integer
If IsNull(PartNum) Then
StandardizePartNum = Null
Exit Function
End If
'find last non-digit
For k = 3 To 1 Step -1
If Not Mid(PartNum, k, 1) Like "#" Then Exit For
Next k
'find numeric portion
For j = k + 1 To Len(PartNum)
If Not Mid(PartNum, j, 1) Like "#" Then Exit For
Next j
StandardizePartNum = _
Left(PartNum, k) & Space(4 - k) _
& Format(Val(Mid(PartNum, k + 1)), "0000000") _
& Mid(PartNum, j)
End Function
If I could ask for your assistance again....
I do have some very short strings I would like to accommodate, e.g.
1PG or 3PG or 10PG or 11PG, etc. These examples are sorting after
1??PG strings. The below code works very well except for part
numbers like 1PG or 11PG or 1-12345 or 11-12345. I have taken
your rules and attempted to modify them. Hopefully, they make sense for my
needs.
I deal with part numbers that begin all numeric, numeric with non-numeric
characters at the end, numeric that may have a non-numeric
character somewhere after the first numeric character. Then, I have
numbers that start with characters, continue with numeric and may end with
non-numeric characters. I have tried to modify your rules accordingly:
1) The initial text will be from 0 to 4 characters ending with a
non-numeric character(s).
To me, zero characters means the string begins numeric.
Numeric beginning strings may have an alpha or a hyphen or a DOT somewhere
after the first numeric digit, e.g. 1-12345 or 10-12345 or 100-12345 or
100.123456 or 45?12345 and needs to sort according to the number of digits
before the dash or DOT, e.g. 1-????? before 2-????? before 1?-????? before
1??-?????, etc. The 45?12345 example seems to sort just fine with the code
below.
Some numbers can have multiple hyphens and some numbers can have both
hyphens and a DOT.
2) The numeric part can be up to 12 digits and immediately follows the
initial text part. The numeric part may or may not be followed by a
trailing text part that starts with any non-numeric character.
3) The trailing text part is any remainder of the string starting with the
first non-numeric character after the numeric part.
If you could, please comment the code a little more.
If a string template would help, let me know!
Thanks so much!
Your previous reply:
Your new examples do not conform to those rules, so you must
come up with a different set of rules. My **guess** for the
rules is now:
1) The initial text will be from 0 to 3 characters ending
with a non-numeric character.
2) The numeric part can be up to 7 digits and immediately
follows the initial text part. The numeric part may or may
not be followed by a trailing text part that starts with any
non-numeric character.
3) The trailing text part is any remainder of the string
starting with the first non-numeric character after the
numeric part.
You must decide if my guesses at the rules is valid for all
of your various situations. Pay particular attention to the
3 that I used. If you might get very short strings (e.g. X3
or just 12) then you will need to modify rule 1) and the
code to accommodate the very short strings.
With all that said, here's my guess at a new procedure:
Public Function StandardizePartNum(PartNum)
Dim k As Integer, j As Integer
If IsNull(PartNum) Then
StandardizePartNum = Null
Exit Function
End If
'find last non-digit
For k = 3 To 1 Step -1
If Not Mid(PartNum, k, 1) Like "#" Then Exit For
Next k
'find numeric portion
For j = k + 1 To Len(PartNum)
If Not Mid(PartNum, j, 1) Like "#" Then Exit For
Next j
StandardizePartNum = _
Left(PartNum, k) & Space(4 - k) _
& Format(Val(Mid(PartNum, k + 1)), "0000000") _
& Mid(PartNum, j)
End Function