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.
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