Function help

T

Terry

Hello,
I made this simple function that calculates a next-up
number that has leading zeros (from a string) taking
advantage of a variant data type.
This works fine until the leading characters are not all
zeros. I would like to use a letter at the beginning of
the string in some cases.
I'm looking for suggestions on how to efficiently split
the string, do the calculation, and then put it back
together again while maintaining the leading Chr.

**********************************************
'example of max: "0000123" or "R000123"
Function nextupNumber(max As String) As String

Dim j As Variant, i As String
Dim clz As Integer, lz As String
i = max
j = max + 1

clz = Len(i) - Len(j)

Select Case clz
Case 1
lz = "0"
Case 2
lz = "00"
Case 3
lz = "000"
Case 4
lz = "0000"
Case 5
lz = "00000"
Case 6
lz = "000000"
Case 7
lz = "0000000"
Case 8
lz = "00000000"
Case Else
lz = ""
End Select

nextupNumber = lz & j

End Function
**********************************************

Any suggestions are greatly appreciated!

Terry
 
C

chris

This needs more testing, but:


Function nextupNumber(max As String) As String
Dim intTemp As Integer
If IsNumeric(Left(max, 1)) Then
intTemp = CInt(max) + 1
Else
nextupNumber = Left(max, 1)
intTemp = CInt(Mid(max, 2)) + 1
End If
nextupNumber = nextupNumber & Right("0000000" & CStr
(intTemp), 7)
nextupNumber = Right("00" & nextupNumber, 8)

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top