「(1,1)」のセルならば「A1」と表示したいのですが、これを行うには、1ならばA、2ならばB、27ならばAAというように、列を英字に変換する必要があります。
ヘルプを調べましたが見付からなかったのでユーザー定義関数を作ってみました。
Function num2xlcol(num)
'エクセルの列番号を英字に変換
Dim pos(3) As Long
pos(1) = num Mod 26
If pos(1) = 0 Then pos(1) = 26
pos(2) = ((num - pos(1)) / 26) Mod 26
If pos(2) = 0 Then pos(2) = 26
pos(3) = (num - pos(2) * 26 - pos(1)) / 26 / 26
If num <= 26 Then 'A-Z
num2xlcol = chr(64 + pos(1))
ElseIf num <= 702 Then 'AA-ZZ
num2xlcol = chr(64 + pos(2)) & chr(64 + pos(1))
ElseIf num <= 18278 Then 'AAA-ZZZ
num2xlcol = chr(64 + pos(3)) & chr(64 + pos(2)) & chr(64 + pos(1))
Else
num2xlcol = "Error"
End If
End Function
'エクセルの列番号を英字に変換
Dim pos(3) As Long
pos(1) = num Mod 26
If pos(1) = 0 Then pos(1) = 26
pos(2) = ((num - pos(1)) / 26) Mod 26
If pos(2) = 0 Then pos(2) = 26
pos(3) = (num - pos(2) * 26 - pos(1)) / 26 / 26
If num <= 26 Then 'A-Z
num2xlcol = chr(64 + pos(1))
ElseIf num <= 702 Then 'AA-ZZ
num2xlcol = chr(64 + pos(2)) & chr(64 + pos(1))
ElseIf num <= 18278 Then 'AAA-ZZZ
num2xlcol = chr(64 + pos(3)) & chr(64 + pos(2)) & chr(64 + pos(1))
Else
num2xlcol = "Error"
End If
End Function
Excel2007だと「XFD」(16384列)までありますが、「ZZZ」(18278列)まで対応しています。
コメント