vba 从列号获取列名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35350858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get column name from column number?
提问by jmaz
I have several named columns on a sheet. I want to send the column number to a function that will return the column name.
我在一张纸上有几个命名的列。我想将列号发送到将返回列名的函数。
For example, if column 1 is named "apple", I want to pass the column number 1 to a function that returns the column name "apple". My attempt:
例如,如果第 1 列名为“apple”,我想将第 1 列传递给返回列名称“apple”的函数。我的尝试:
Function getColName(colNumber As Integer) As String
'return column name when passed column number
getColName = Cells(1, colNumber).Column.Name
End Function
How can I get this code to work?
我怎样才能让这个代码工作?
回答by Kavyajeet Bora
Try this one :
试试这个:
set cell_address = Worksheet("Sheet1").Cells(1,1)
MsgBox cell_address.address(RowAbsolute:=False, ColumnAbsolute=False)
'returns A1You can use it in Range("A1")as well and get more functionality.
'returnsA1您也可以使用它Range("A1")并获得更多功能。
**Note**: RowAbsolute:=False, ColumnAbsolute=False are optional
for more information:
想要查询更多的信息:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-address-property-excel
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-address-property-excel
for non-vba: https://exceljet.net/formula/convert-column-number-to-letter
对于非 vba:https: //exceljet.net/formula/convert-column-number-to-letter

