アプリ版:「スタンプのみでお礼する」機能のリリースについて

Excel VBAで(1)のように列(A)に数字(1~)を加算して
列(B~)を求めておりましたが、このやり方では列数がZ以上になると
AA,AB,ACになりません。(3)の結果が得られるような関数はありますでしょうか?

(1)Chr(Asc("A") + 1) = B

(2)Chr(Asc("Z") + 1) = [

(3) Z + 1 = AA

どなたか良い知恵がありましたら思います。
よろしくお願いします。

A 回答 (4件)

以前にもこういう質問に答えたことがあって、その時は、質問者のように、文字列をインクリメントするような方法をやっていましたが、


エクセルのことはエクセルに聞けということで、
Public Function 列名(col As String, offset As Integer) As String
Dim cellAddress As String
cellAddress = Range(col & "1").offset(0, offset).AddressLocal(False, False)
列名 = Left(cellAddress, Len(cellAddress) - 1)
End Function
のようにすれば、
result = 列名("Z",1)
でresult = "AA" になります。
    • good
    • 0
この回答へのお礼

いつもお世話になっております。
今回はVBAで作成しておりますのでこの方法を
採用します。この度は有難う御座いました。

お礼日時:2006/04/19 11:29

AA1セルの値を求めるのに


Sub test03()
Cells(1, 1) = Range("z1").Offset(0, 1)
End Sub
こういうこと(Offset)ができ増すが、そればよいのではないですか。
勘違いならすみません。
    • good
    • 0

VBAでどのように使用されているかわかりませんが、列番号で指定するのはだめでしょうか。



例えば
Columns("A")

Columns(1)
は両方とも列の一番目(A列)を指します。

同じようにZ列は
Columns(26)
AA列は
Columns(27)
となります。
    • good
    • 1

 仕事でそのような機能を持つものを探しました。


 結果、見つからなかったので自作しました(^_^;
 A~Zを26進数の数値に変換し、演算を行ったのちにAA形式に再変換する手法をとりました。
 下記は、CellRight にセル名と増分を与えると、移動先のセル名が返る関数です。(ちょっと見づらいかな?)

Private Function CellRight(ByRef CellName As String, _
ByRef Movement As Integer) As String

Dim CellID

CellID = Split(DivedeCell(CellName), ",")
CellID(2) = CellID(2) + Movement
Do While CellID(2) > 26
CellID(1) = CellID(1) + 1
CellID(2) = CellID(2) - 26
Loop
Do While CellID(2) < 1
CellID(1) = CellID(1) - 1
CellID(2) = CellID(2) + 26
Loop

If (CellID(1) = 9 And CellID(2) > 22) Or CellID(1) > 9 Then
CellID(1) = 9
CellID(2) = 22
End If
If CellID(1) < 0 Then
CellID(1) = 0
CellID(2) = 1
End If

CellRight = ConbineCell(CInt(CellID(1)), CInt(CellID(2)), CInt(CellID(3)))

End Function

Private Function DivedeCell(ByRef CellName As String) As String

Dim Result(3) As Integer
Dim C As String

Result(1) = 0
Result(2) = 0
Result(3) = 0
For I = 1 To Len(CellName)
C = UCase(Mid(CellName, I, 1))
If Asc(C) >= &H41 And Asc(C) <= &H5A Then
Result(I) = Asc(C) - &H40
Else
Result(3) = Right(CellName, Len(CellName) - I + 1)
Exit For
End If
Next

If Result(2) = 0 Then
Result(2) = Result(1)
Result(1) = 0
End If

DivedeCell = "," & Result(1) & "," & Result(2) & "," & Result(3)

End Function

Private Function ConbineCell(ByRef CellID1 As Integer, _
ByRef CellID2 As Integer, _
ByRef CellID3 As Integer) As String

Dim Result As String

Result = ""
If CellID1 <> 0 Then Result = Chr(CellID1 + &H40)
Result = Result & Chr(CellID2 + &H40)
Result = Result & CellID3

ConbineCell = Result
End Function
    • good
    • 0
この回答へのお礼

次回VBで作成する場合の関数として使用させて
頂きます。この度は有難う御座いました。

お礼日時:2006/04/19 11:30

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています


このQ&Aを見た人がよく見るQ&A