pgbigorokuのブログ

プログラムの再利用できそうなコードをアップ

EXCEL VBA 列の番号をA~Zの記号にする

'
'   列の番号をA~Zの記号にする
'
'URL https://pgbigoroku.hatenablog.com/entry/2022/08/14/230217
'2022/8/15
Private Function pstr列アルファベットFrom列番号(ByVal plng列番号 As Long) As String
    Dim str1 As String  '1文字目
    Dim int1 As Integer
    Dim str2 As String  '2文字目
    Dim int2 As Integer
    Dim str3 As String '3文字目
    Dim int3 As Integer
    int3 = plng列番号 Mod 26
    str3 = pstr列アルファベットFrom列番号_Sub(int3)
    If plng列番号 > 26 Then
        int2 = Int((plng列番号 - 1) / 26) Mod 26
        str2 = pstr列アルファベットFrom列番号_Sub(int2)
    Else
        str2 = ""
    End If
    If plng列番号 >= 703 Then
        int1 = Int((plng列番号 - 1) / (26 * 26)) Mod 26
        str1 = pstr列アルファベットFrom列番号_Sub(int1)
    Else
        str1 = ""
    End If
    pstr列アルファベットFrom列番号 = str1 & str2 & str3
End Function


Private Function pstr列アルファベットFrom列番号_Sub(ByVal pint As Long) As String
    If pint = 0 Then
        pstr列アルファベットFrom列番号_Sub = "Z"
    Else
        pstr列アルファベットFrom列番号_Sub = Chr(64 + pint)
    End If
End Function