将列号转换为字母的函数是什么?

183

有人有一段Excel VBA函数可以根据给定的数字返回相应的列字母吗?

例如输入100,应该返回CV


4
请查看这个问题:https://dev59.com/w2kw5IYBdhLWcg3wJXIh。 - Francis Dean
@FrancisDean,这是与该问题相反的情况,该问题是从数字中查找地址。 - brettdj
2
@brettdj所提供的答案展示了数字转字母和字母转数字的方法。 - Francis Dean
2
@FrancisDean 说得好,我看了链接中的问题标题而不是被接受的答案。 - brettdj
28个回答

1
以下是一个晚些的答案,仅适用于使用 Int()If 的简单方法,对于1-3个字符列:
Function outColLetterFromNumber(i As Integer) As String

    If i < 27 Then       'one-letter
        col = Chr(64 + i)
    ElseIf i < 677 Then  'two-letter
        col = Chr(64 + Int(i / 26)) & Chr(64 + i - (Int(i / 26) * 26))
    Else                 'three-letter
        col = Chr(64 + Int(i / 676)) & Chr(64 + Int(i - Int(i / 676) * 676) / 26)) & Chr(64 + i - (Int(i - Int(i / 676) * 676) / 26) * 26))
    End If

    outColLetterFromNumber = col

End Function

1
这个公式将根据范围(即A1)给出列,其中范围是单个单元格。如果给出多个单元格范围,则返回左上角的单元格。请注意,两个单元格引用必须相同:
MID(CELL("address",A1),2,SEARCH("$",CELL("address",A1),2)-2)
其原理是:
CELL("property","range") 根据使用的属性返回范围的特定值。在这种情况下,是单元格地址。 地址属性返回值为$[col]$[row],例如A1->$A$1。 MID函数解析出$符号之间的列值。

0
Sub GiveAddress()
    Dim Chara As String
    Chara = ""
    Dim Num As Integer
    Dim ColNum As Long
    ColNum = InputBox("Input the column number")

    Do
        If ColNum < 27 Then
            Chara = Chr(ColNum + 64) & Chara
            Exit Do
        Else
            Num = ColNum / 26
            If (Num * 26) > ColNum Then Num = Num - 1
            If (Num * 26) = ColNum Then Num = ((ColNum - 1) / 26) - 1
            Chara = Chr((ColNum - (26 * Num)) + 64) & Chara
            ColNum = Num
        End If
    Loop

    MsgBox "Address is '" & Chara & "'."
End Sub

-1

可以使用以下步骤通过公式提取列号来提取列字母:
1. 使用ADDRESS公式计算列地址
2. 使用MID和FIND函数提取列字母

例如:
1. ADDRESS(1000,1000,1)
结果为$ALL$1000
2. =MID(F15,2,FIND("$",F15,2)-2)
假设F15包含步骤1的结果,则结果为ALL

一次性写入:
MID(ADDRESS(1000,1000,1),2,FIND("$",ADDRESS(1000,1000,1),2)-2)


-1

这仅适用于REFEDIT...通常在此处使用代码 简短版本...易于阅读和理解/ 它使用$的位置

Private Sub RefEdit1_Change()

    Me.Label1.Caption = NOtoLETTER(RefEdit1.Value) ' you may assign to a variable  var=....'

End Sub

Function NOtoLETTER(REFedit)

    Dim First As Long, Second As Long

    First = InStr(REFedit, "$")                 'first poz of $
    Second = InStr(First + 1, REFedit, "$")     'second poz of $

    NOtoLETTER = Mid(REFedit, First + 1, Second - First - 1)   'extract COLUMN LETTER

End Function

-2

-2

把它转换为 ASCII 编码再使用 Chr() 函数转换回字母怎么样?

col_letter = Chr(Selection.Column + 96)


-6

这里有另一种方法:

{

      Sub find_test2()

            alpha_col = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,W,Z" 
            MsgBox Split(alpha_col, ",")(ActiveCell.Column - 1) 

      End Sub

}

1
不需要创建一个字母表的字符串。ASCII 已经为我们做了这个。 - Caltor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接