函数将列号转换为字母?

是否有人有一个 Excel VBA 函数,可以返回从一个数字的列(s) ?

例如,输入 100应该返回 CV

477571 次浏览

此函数返回给定列号的列号。

Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function

第100栏的测试代码

Sub Test()
MsgBox Col_Letter(100)
End Sub

如果不想使用 range 对象:

Function ColumnLetter(ColumnNumber As Long) As String
Dim n As Long
Dim c As Byte
Dim s As String


n = ColumnNumber
Do
c = ((n - 1) Mod 26)
s = Chr(c + 65) & s
n = (n - c) \ 26
Loop While n > 0
ColumnLetter = s
End Function

对我有用的是:

Cells(Row,Column).Address

这将为您返回 $AE $1格式引用。

以及一个使用递归的解决方案:

Function ColumnNumberToLetter(iCol As Long) As String


Dim lAlpha As Long
Dim lRemainder As Long


If iCol <= 26 Then
ColumnNumberToLetter = Chr(iCol + 64)
Else
lRemainder = iCol Mod 26
lAlpha = Int(iCol / 26)
If lRemainder = 0 Then
lRemainder = 26
lAlpha = lAlpha - 1
End If
ColumnNumberToLetter = ColumnNumberToLetter(lAlpha) & Chr(lRemainder + 64)
End If


End Function

Robertsd 的代码 非常优雅,但是为了防止将来出现问题,需要将 n 的声明更改为 long 类型

如果你想要一个避免宏的公式,这里有一些工作到702列包括

=IF(A1>26,CHAR(INT((A1-1)/26)+64),"")&CHAR(MOD(A1-1,26)+65)

其中 A1是包含要转换为字母的列号的单元格。

这是一个基于上面 @ DamienFennelly 的回答的函数。如果你给我竖起大拇指,也给他竖起大拇指!校对: P

Function outColLetterFromNumber(iCol as Integer) as String
sAddr = Cells(1, iCol).Address
aSplit = Split(sAddr, "$")
outColLetterFromNumber = aSplit(1)
End Function

还有一个办法。布雷特的回答让我想到了这一点,但是如果你使用这个方法,你不必使用变量数组,你可以直接转到一个字符串。

ColLtr = Cells(1, ColNum).Address(True, False)
ColLtr = Replace(ColLtr, "$1", "")

或者可以让它更紧凑一点

ColLtr = Replace(Cells(1, ColNum).Address(True, False), "$1", "")

注意,这取决于您是否引用 cell 对象中的第1行。

最新更新 : 请忽略下面的函数,@SurasinTancharoen 设法提醒我它在 n = 53处出现故障。
对于那些感兴趣的人来说,下面是 n = 200以下的其他破损值:

Certain values of

请使用@brettdj 函数来满足您的所有需求。它甚至适用于 MicrosoftExcel 的最新最大列数限制: 16384应该给出 XFD

enter image description here

更新完毕


以下功能由微软提供:

Function ConvertToLetter(iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function

资料来源: 如何将 Excel 列号转换为字母字符

适用于

  • MicrosoftOfficeExcel2007
  • MicrosoftExcel2002标准版
  • MicrosoftExcel2000标准版
  • MicrosoftExcel97标准版

还有一个办法:

{


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


}

有一个非常简单的方法使用 Excel 电源: 使用 Range.Cells.Address属性,这样做:

strCol = Cells(1, lngRow).Address(xlRowRelative, xlColRelative)

这将返回第1行所需列的地址:

strCol = Left(strCol, len(strCol) - 1)

请注意,它是如此快速和强大,您可以返回列地址,甚至存在!

使用 Selection.Column属性将所需的列号替换为 lngRow

下面是一个可以使用的简单的一行程序。

ColumnLetter = Mid(Cells(Row, LastColA).Address, 2, 1)

它只适用于1个字母的列指定,但是对于简单的情况很好。如果你需要它的工作只有2个字母的指定,那么你可以使用以下:

ColumnLetter = Mid(Cells(Row, LastColA).Address, 2, 2)

对于位于 X 行 Y 列中的单元格,不管代码行中的哪一列都可以使用:

Mid(Cells(X,Y).Address, 2, instr(2,Cells(X,Y).Address,"$")-2)

如果您有一个定义名称为“ Cellname”的单元格:

Mid(Cells(1,val(range("Cellname").Column)).Address, 2, instr(2,Cells(1,val(range("Cellname").Column)).Address,"$")-2)

获取列名的简单方法

Sub column()


cell=cells(1,1)
column = Replace(cell.Address(False, False), cell.Row, "")
msgbox column


End Sub

我希望这有所帮助

这可以通过使用一个公式得到:

=SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")

也可以按照要求写成 VBA 函数:

Function ColName(colNum As Integer) As String
ColName = Split(Worksheets(1).Cells(1, colNum).Address, "$")(1)
End Function

这是 Robartsd 的回答的一个版本(类似于 Jan Wijninckx 的单行解决方案) ,使用递归代替循环。

Public Function ColumnLetter(Column As Integer) As String
If Column < 1 Then Exit Function
ColumnLetter = ColumnLetter(Int((Column - 1) / 26)) & Chr(((Column - 1) Mod 26) + Asc("A"))
End Function

我使用以下输入对此进行了测试:

1   => "A"
26  => "Z"
27  => "AA"
51  => "AY"
702 => "ZZ"
703 => "AAA"
-1  => ""
-234=> ""

Brettdj 提供的解决方案非常有效,但是如果你遇到这个潜在的解决方案的原因和我一样,我想我会提供我的替代解决方案。

我遇到的问题是基于 MATCH ()函数的输出滚动到特定的列。我没有将列号并行转换为列号字母,而是选择临时将引用样式从 A1切换到 R1C1。这样我就可以直接滚动到列号,而不必使用 VBA 函数。要在两个引用样式之间轻松切换,可以使用以下 VBA 代码:

Sub toggle_reference_style()


If Application.ReferenceStyle = xlR1C1 Then
Application.ReferenceStyle = xlA1
Else
Application.ReferenceStyle = xlR1C1
End If


End Sub

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

例如:
1. 地址(1000,1000,1)
结果 $ALL $1000
2. = MID (F15,2,FIND (“ $”,F15,2)-2)
结果 ALL 假设 F15包含步骤1的结果

我们可以一气呵成
MID (ADDRESS (1000,1000,1) ,2,FIND (“ $”,ADDRESS (1000,1000,1) ,2)-2)

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

在 brettdj 回答的基础上,这里使列号的输入成为可选的。如果省略了列号输入,则函数返回调用该函数的单元格的列号。我知道这也可以实现仅仅使用 ColumnLetter(COLUMN()),但我认为这将是很好的,如果它可以聪明地理解这样。

Public Function ColumnLetter(Optional ColumnNumber As Long = 0) As String
If ColumnNumber = 0 Then
ColumnLetter = Split(Application.Caller.Address(True, False, xlA1), "$")(0)
Else
ColumnLetter = Split(Cells(1, ColumnNumber).Address(True, False, xlA1), "$")(0)
End If
End Function

这个函数的优缺点是,由于 IF测试,它会比 brettdj 的答案慢一点点。但是,如果这个函数被重复使用很多次,就可以感觉到这一点。

这只是为 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

第 A 章是第65条,因此:

MsgBox Chr(ActiveCell.Column + 64)

发现地: http://www.vbaexpress.com/forum/showthread.php?6103-Solved-get-column-letter

这里是一个后期的答案,只是为了简化使用 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

如果只是转换为 ascii 数字并使用 Chr ()来转换回一个字母呢?

Col_ mail = Chr (选择. 列 + 96)

Function fColLetter(iCol As Integer) As String
On Error GoTo errLabel
fColLetter = Split(Columns(lngCol).Address(, False), ":")(1)
Exit Function
errLabel:
fColLetter = "%ERR%"
End Function

这里是 Pascal (Delphi)中的一个简单函数。

function GetColLetterFromNum(Sheet : Variant; Col : Integer) : String;
begin
Result := Sheet.Columns[Col].Address;  // from Col=100 --> '$CV:$CV'
Result := Copy(Result, 2, Pos(':', Result) - 2);
end;

这个公式将给出基于范围(即 答1)的列,其中 range 是单个单元格。如果给定一个多单元格范围,它将返回左上角的单元格。注意,两个单元格引用必须相同:

中间(单元格(“地址”,答1) ,2,搜索(“ $”,单元格(“地址”,答1) ,2)-2)

工作原理:

CELL (“ property”,“ range”)根据所使用的属性返回范围的特定值。 Address 属性返回一个值 $[ col] $[ row ] ,即 A1-> $A1。 MID 函数解析 $符号之间的列值。

I'm surprised nobody suggested:   Columns(Column Index).Address

  • 例如: MsgBox Columns( 9347 ).Address 报税表 < img src = “ https://i.stack.imgur.com/A2y11.png”alt = “ $MUM: $MUM”/> < img src = “ https://i.stack.imgur.com/A2y11.png”alt = “ $MUM: $MUM”/>

返回 仅此而已列字母 (s) : Split((Columns(Column Index < strong > ).Address(,0)),":")(0)

  • 例如: MsgBox Split((Columns( 2734 ).Address(,0)),":")(0) 报税表 < img src = “ https://i.stack.imgur.com/e0c5y.png”alt = “ DAD”/> < img src = “ https://i.stack.imgur.com/e0c5y.png”alt = “ DAD”/>

More Examples


所以我来晚了,但我想贡献另一个答案,没有人已经解决了,还没有涉及数组。您可以通过简单的字符串操作来完成。

Function ColLetter(Col_Index As Long) As String


Dim ColumnLetter As String


'Prevent errors; if you get back a number when expecting a letter,
'    you know you did something wrong.
If Col_Index <= 0 Or Col_Index >= 16384 Then
ColLetter = 0
Exit Function
End If


ColumnLetter = ThisWorkbook.Sheets(1).Cells(1, Col_Index).Address     'Address in $A$1 format
ColumnLetter = Mid(ColumnLetter, 2, InStr(2, ColumnLetter, "$") - 2)  'Extracts just the letter


ColLetter = ColumnLetter
End Sub

输入格式为 $A$1后,使用 Mid函数,从位置2开始计算第一个 $,然后使用 InStr找到第二个 $出现在字符串中的位置,然后减去2来计算该起始位置。

这使您能够适应所有可能的列。因此,ColLetter(1)返回“ A”,ColLetter(16384)返回“ XFD”,这是我的 Excel 版本的最后一列。