在Alpha数字编号系统中使用Excel VBA锻炼下一个值

时间:2013-09-16 09:24:57

标签: excel excel-vba vba

好的,这是我拥有的编号系统的风格

A0
A1
A2
A3
A4
A5
A6
A7
A8
A9
AA
AB
AC
AD
....
B0
B1
B2
B3
B4
B5
B6
B7
B8
B9
BA
BB
BC
....
C0
C1
etc

我需要一个功能,我可以传入 C9 并返回 CA

澄清一下,规则是这样的

A0->那么A9 AA - > AZ B0 - > B9 BA - > BZ

所以第二个字母遵循这个顺序

0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

之后,第一个数字使用相同的序列增加

1 个答案:

答案 0 :(得分:2)

这是你在尝试什么?逻辑是获取数字的ASC并将其递增1(条件适用)

Sub sample()
    Dim sCheck As String

    '~~> Uncomment any of the below to test
    'sCheck = "AZ"
    'sCheck = "B9"
    'sCheck = "ZZ"
    'sCheck = "C9"

    Debug.Print GetNext(sCheck)
End Sub

Function GetNext(s As String) As String
    If s = "" Then Exit Function

    Dim s1 As String, s2 As String

    s1 = Left(s, 1)
    s2 = Right(s, 1)

    Select Case UCase(s2)

    Case "Z"
        If s1 = "Z" Then
            GetNext = "You have reached the end of the sequence"
            Exit Function
        End If

        s1 = Chr(Asc(s1) + 1)
        s2 = "0"
    Case "9"
        s2 = "A"
    Case Else
        s2 = Chr(Asc(s2) + 1)
    End Select

    GetNext = s1 & s2
End Function