将数字添加到字母数字值(例如:“ ABC” +1 =“ ABD”)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数字添加到字母数字值(例如:“ ABC” +1 =“ ABD”)相关的知识,希望对你有一定的参考价值。

如果我在标题中要求的内容不可行,我会解释我所需要的,也许还有其他方法可以解决。

我有一个包含值或值范围的列表,这些值或值范围用“-”分隔,请参见下文:

9565
9570
9626 - 9627
A015 - A020
A040 - A042
A070 - A072
A078 - A082
A095 - A097
GGAA - GGZZ

我想创建一个循环遍历行并将范围“分解”为单个值的宏。使用示例的前4行,输出为:

9565
9570
9626
9627
A015
A016
A017
A018
A019
A020
...

可悲的是,它们并不总是数字,所以我不能只是将范围放入循环中,并在上一个值之前加上+1,直到循环结束。

任何想法?

非常感谢,感谢您的英语错误。

答案

您有时会需要它-它会按顺序返回下一个文本字符,并在Z处换行。它仅假定为大写,并且我不打扰处理小写的情况

Public Function NextChar(s As String) As String
Dim x As Long
Dim answer As String
Dim flag As Boolean
x = Asc(Right(s, 1))
Select Case x
    Case 65 To 89 'A to Y
        x = x + 1
    Case 90 'wrap at Z
        x = 65
        flag = True
    Case Else '?

End Select
Select Case Len(s)
    Case 1
        If flag Then
            answer = "A" & Chr$(x)
        Else
            answer = Chr$(x)
        End If
    Case 2
        If flag Then
            answer = NextChar(Left(s, 1)) & Chr$(x)
        Else
            answer = Left(s, 1) & Chr$(x)
        End If
    Case Else
    '???
End Select
NextChar = answer

End Function
另一答案

花了我一整天,但实际上我做到了!

感谢@Harassed Dad!我调整了您的代码,结果看起来像这样:

功能:

Function WF_NEXT_CHAR(s As String) As String

    Dim ASCII   As Byte
    Dim i       As Integer

    s = UCase(s)
    ASCII = Asc(Right(s, 1))

    If s = String(Len(s), "Z") Then
        WF_NEXT_CHAR = "#"
            Else
        Select Case ASCII
            Case 57                     'Wrap Number at 9
                ASCII = 65
            Case 48 To 56, 65 To 89     '0 to 8 and A to Y
                ASCII = ASCII + 1
            Case 90                     'Wrap Char at Z
                ASCII = 49
                For i = Len(s) To 1 Step -1
                    If Mid(s, i, 1) = "Z" Then
                        WF_NEXT_CHAR = Left(s, i - 1) & "0" & Right(WF_NEXT_CHAR, Len(s) - i)
                            Else
                        WF_NEXT_CHAR = Left(s, i - 1) & WF_NEXT_CHAR(Mid(s, i, 1)) & Right(WF_NEXT_CHAR, Len(s) - i)
                        Exit For
                    End If
                Next
        End Select
        If Len(s) = 1 Then WF_NEXT_CHAR = Chr(ASCII)
        If WF_NEXT_CHAR = "" Then WF_NEXT_CHAR = Left(s, Len(s) - 1) & Chr(ASCII)
    End If

End Function

Sub(假定范围在“ A”列中:] >>

Sub UHUUUUL_xD()

Const Del   As String = " - "
Dim r       As Long
Dim Rng     As Variant
Dim i       As String

For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    If InStr(1, Cells(r, 1), Del) > 0 Then
        Rng = Split(Cells(r, 1), Del)
        Cells(Cells(Rows.Count, 2).End(xlUp).Row + 1, 2) = IIf(IsNumeric(Rng(0)), "'", "") & Rng(0)
        While i <> "#" And i <> Rng(1)
            i = WF_NEXT_CHAR(Cells(Rows.Count, 2).End(xlUp))
            Cells(Cells(Rows.Count, 2).End(xlUp).Row + 1, 2) = i
        Wend
    End If
Next

End Sub

以上是关于将数字添加到字母数字值(例如:“ ABC” +1 =“ ABD”)的主要内容,如果未能解决你的问题,请参考以下文章

在 sas 中查找包含字母数字值的字符串

如何将 UI 控件的双数据值格式化为数字,例如 1234 到 1.23K? [复制]

需要使用 regexp_replace (Postgresql) 将逗号添加到数字列表中

如何将多个值添加到python中的字典键? [关闭]

如何将数组数字映射到定时值

如何在java中为数字和字母拆分String