LoginSignup
0
0

More than 3 years have passed since last update.

Excel列番号を変換する関数

Last updated at Posted at 2019-06-06

Cellsオブジェクトを中継するのが何となく癪だったので再帰呼び出しで実装してみた。

R1C1→A1

Function GetA1Col(col As Integer) As String
    If col < 27 Then
        GetA1Col = Chr(col + 64)
    Else
        GetA1Col = GetA1Col(Int((col - 1) / 26)) & Chr((col - 1) Mod 26 + 65)
    End If
End Function

A1→R1C1

Function GetR1C1Col(col As String) As Integer
    If Len(col) = 1 Then
        GetR1C1Col = Asc(col) - 64
    Else
        GetR1C1Col = Asc(Right(col, 1)) - 64 + GetR1C1Col(Left(col, Len(col) - 1)) * 26
    End If
End Function

こだわらないひと向け

R1C1→A1

Function GetA1Col(col As Integer) As String
    Dim adrs As String
    adrs = Cells(1, col).Address(True, False)
    GetA1Col = Left(adrs, InStr(adrs, "$") - 1)
End Function

A1→R1C1

Function getR1C1Col(col As String) As Integer
    getR1C1Col = Cells(1, col).Column
End Function

Excelで本来不可能な列番号(マイナス値とか、16385以上とか)への配慮はしていません。利用シーンに合わせてお好みで実装してください。
ただし再帰呼び出しが入っているので、ダイアログ出すなら場所に気を付けて。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0