列名と列番号の変換

列名のアルファベットと列番号の数字を変換

Aを1、Bを2、Cを3とする変換と、1をA、2をB、3をCとする変換。

' ------------------------------------------------------------
' 説明:列番号 → 列名 の変換
' 引数:1:列番号
' 戻値:列名
' 補足:列番号は、左から1、2、3と数える。対応する列番号はA、B、B
' ------------------------------------------------------------
Function ColumnIdxToStr(columnNumber As Long) As String
	On Error GoTo ErrLabel

	ColumnIdxToStr = Split(Columns(columnNumber).Address, "$")(2)
	Exit Function

ErrLabel:
    ColumnIdxToStr = ""
End Function
' ------------------------------------------------------------
' 説明:列名 → 列番号 の変換
' 引数:1:列名
' 戻値:列番号
' 補足:列名は、左からA、B、B。対応する列番号は1、2、3
' ------------------------------------------------------------
Function ColumnStrToIdx(columnStr As String) As Long
	On Error GoTo ErrLabel

	ColumnStrToIdx = Columns(columnStr).column
	Exit Function

ErrLabel:
	ColumnStrToIdx = 0
End Function

コメント