复制上面的单元格并将值增加 1(值按字母顺序排列)

发布于 2025-01-09 08:25:24 字数 101 浏览 4 评论 0 原文

我是 VBA 新手,需要一些帮助。从活动单元格中,我需要复制上面单元格中的值并将值加 1。该值始终是两个字母,例如 AA、AB、AC。我可以找到有关递增数字的信息,但找不到有关字母的信息。

I'm new to VBA and could use some help. From the active cell, I need to copy the value from the cell above and increment the value by 1. The value is always two letters such as AA, AB, AC. I can find information on incrementing numbers but not letters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生死何惧 2025-01-16 08:25:24
Sub Tester()
    Dim v As String
    v = "CD" 'for example
    Debug.Print Replace(Range(v & "1").Offset(0, 1).Address(0, 0), "1", "") '> CE
End Sub

应该适用于任何两个字母的组合(但“ZZ”会给你“AAA”),

没有使用范围引用的限制:

Sub Tester()
    Dim e
    For Each e In Array("A", "AA", "AZ", "AZZ", "ZZZ", String(10,"Z"))
        Debug.Print e, NextSeq(CStr(e))
    Next e
End Sub

Function NextSeq(s As String)
    Dim rv As String, a As Long, carry As Boolean, ls As Long, i As Long
    ls = Len(s)
    For i = ls To 1 Step -1
        a = Asc(Mid(s, i, 1)) - 64 ' "A"=ASCII 65
        If i = ls Or carry Then
            a = IIf(a = 26, 1, a + 1) 'roll over ?
            carry = a = 1             'carry 1 to next iteration ? 
        End If
        rv = Chr(64 + a) & rv
    Next i
    NextSeq = IIf(carry, "A" & rv, rv) 'one more "A" ?
End Function

输出:

A             B
AA            AB
AZ            BA
AZZ           BAA
ZZZ           AAAA
ZZZZZZZZZZ    AAAAAAAAAAA
Sub Tester()
    Dim v As String
    v = "CD" 'for example
    Debug.Print Replace(Range(v & "1").Offset(0, 1).Address(0, 0), "1", "") '> CE
End Sub

should work for any two-letter combo (but "ZZ" will give you "AAA")

Without the limitations of using range references:

Sub Tester()
    Dim e
    For Each e In Array("A", "AA", "AZ", "AZZ", "ZZZ", String(10,"Z"))
        Debug.Print e, NextSeq(CStr(e))
    Next e
End Sub

Function NextSeq(s As String)
    Dim rv As String, a As Long, carry As Boolean, ls As Long, i As Long
    ls = Len(s)
    For i = ls To 1 Step -1
        a = Asc(Mid(s, i, 1)) - 64 ' "A"=ASCII 65
        If i = ls Or carry Then
            a = IIf(a = 26, 1, a + 1) 'roll over ?
            carry = a = 1             'carry 1 to next iteration ? 
        End If
        rv = Chr(64 + a) & rv
    Next i
    NextSeq = IIf(carry, "A" & rv, rv) 'one more "A" ?
End Function

Output:

A             B
AA            AB
AZ            BA
AZZ           BAA
ZZZ           AAAA
ZZZZZZZZZZ    AAAAAAAAAAA
☆獨立☆ 2025-01-16 08:25:24

字节数组方法作为启动帮助

为了艺术的目的,我演示了另一种在给定范围内从“AA”到“ZZ”“递增”两个字母组合的方法;这可以通过增加所谓的字节数组中最后一个|前一个字母的数值来完成。

将变量(例如 by())声明为 Byte 类型后,可以通过 s)分配给它>by = s。使用的字母(A 到 Z)现在显示元素对中相应的数字字符值(第一个字母值可以取自 by(0),第二个=最后一个取自 by (2)by(ubound(by)-1) 因为这里有 4 个索引为 0 到 3 的元素)。

Public Function IncLetter(Optional ByVal s As String = "AA") As String
    If Len(s) > 2 Or UCase(s) = "ZZ" Then
        IncLetter = s & "?": Exit Function
    End If
'a) get numeric character values
    Dim by() As Byte: by = s
'b) increment last alphabetic letter
    Dim indx As Long: indx = UBound(by) - 1
    'check if last letter is "Z"
    If UCase(Chr(by(indx))) = "Z" Then
        by(indx) = by(indx) - 25
        by(indx - 2) = by(indx - 2) + 1
    Else
        by(indx) = by(indx) + 1
    End If
'c) return new string as function result
    IncLetter = by
End Function

Byte array approach as start help

For the sake of the art I demonstrate another approach to "increment" a two-letter combination within a given range from "AA" to "ZZ"; this can be done by incrementing the numeric value of the last|previous letter in a so called byte array.

Having declared variable (e.g. by()) as of type Byte one can easily assign the string value (e.g. s) to it via by = s. The used alphabetic letters (A to Z) now show the corresponding numeric character value in element pairs (the first letter value can be taken from by(0), the second=last one from by(2) or by(ubound(by)-1) as there are 4 elements with indices 0 to 3 here).

Public Function IncLetter(Optional ByVal s As String = "AA") As String
    If Len(s) > 2 Or UCase(s) = "ZZ" Then
        IncLetter = s & "?": Exit Function
    End If
'a) get numeric character values
    Dim by() As Byte: by = s
'b) increment last alphabetic letter
    Dim indx As Long: indx = UBound(by) - 1
    'check if last letter is "Z"
    If UCase(Chr(by(indx))) = "Z" Then
        by(indx) = by(indx) - 25
        by(indx - 2) = by(indx - 2) + 1
    Else
        by(indx) = by(indx) + 1
    End If
'c) return new string as function result
    IncLetter = by
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文