1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Excel VBA] 列番号を列文字に変換する/列文字を列番号に変換する

Last updated at Posted at 2024-08-21

下記の記事の機能分離版です

ざっくり説明

列番号を渡すと列文字にしてくれる関数 (以下、列番号→列文字 変換)と、
列文字を渡すと列番号にしてくれる関数 (以下、列文字→列番号 変換)です。

ex.
ConvertColNumberToColLetter (3776) → "EOF"
ConvertColLetterToColNumber ("AB") → 28

コード

列番号→列文字 変換
Function ConvertColNumberToColLetter(ByVal colNumber As Long) As String
'================================
'用途  :列番号を列文字に変換
'--------------------------------
'第一引数:列番号 (Long型)
'戻り値 :列文字 (String型)
'================================
    Const MAXIMUM_COL_NUMBER As Long = 16384

    If colNumber <= 0 Or colNumber > MAXIMUM_COL_NUMBER Then GoTo ErrorLabel

    Dim colLetter As String
    Do While colNumber > 0
        colNumber = colNumber - 1
        colLetter = Chr((colNumber Mod 26) + 65) & colLetter
        colNumber = Int(colNumber / 26)
    Loop

    ConvertColNumberToColLetter = colLetter
    Exit Function
ErrorLabel:

    ConvertColNumberToColLetter = "ERROR"
End Function
列文字→列番号 変換
Function ConvertColLetterToColNumber(ByVal colLetter As String) As Long
'================================
'用途  :列文字を列番号に変換
'--------------------------------
'第一引数:列文字 (String型)
'戻り値 :列番号 (Long型)
'================================
    Const MAXIMUM_COL_LETTER As String = "XFD"

    colLetter = UCase(colLetter)
    If colLetter = vbNullString Then GoTo ErrorLabel
    If Len(colLetter) > Len(MAXIMUM_COL_LETTER) Then GoTo ErrorLabel
    If Len(colLetter) = Len(MAXIMUM_COL_LETTER) And _
        colLetter > MAXIMUM_COL_LETTER Then GoTo ErrorLabel

    Dim colNumber As Long
    Dim i As Long
    For i = 1 To Len(colLetter)
        Dim targetCharacter As String
        Dim asciiCode As Long
        targetCharacter = Left(Right(colLetter, i), 1)
        asciiCode = Asc(targetCharacter)
        If asciiCode < 65 Or asciiCode > 90 Then GoTo ErrorLabel
        colNumber = colNumber + (asciiCode - 64) * 26 ^ (i - 1)
    Next

    ConvertColLetterToColNumber = colNumber
    Exit Function
ErrorLabel:
    
    ConvertColLetterToColNumber = "ERROR"
End Function

使用方法

列番号→列文字 変換
第一引数に列番号を渡す

列文字→列番号 変換
第一引数に列文字を渡す

仕様等

共通
エラーで弾かれたものはErrorLabelに飛ばされるので適宜処理を記入して下さい
(とりあえず文字列"ERROR"を返します)

列文字→列番号 変換
引数で文字を入力するときは小文字でも大丈夫です

注意点

以下はエラーになります。(以下、第一引数を引数と言う)

列番号→列文字 変換

  • 引数で空白("")を渡す
  • 引数(数字)が小数
  • 引数(数字)が0以下
  • 引数(数字)が最大列の数字(現状16384)より大きい

列文字→列番号 変換

  • 引数で空白("")を渡す
  • 引数(文字)がアルファベットではない
  • 引数(文字)が最大列の文字列(現状"XFD")より大きい

あと、引数で#REF!とか#DIV/0!とか渡すのはダメ

その他

将来的にExcelの仕様変更で最大列数が増えた場合も
Const MAXIMUM_COL_LETTERConst MAXIMUM_COL_NUMBERの変更で対応可能なはずです。

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?