Function to convert column number to letter?

后端 未结 28 1549
灰色年华
灰色年华 2020-11-22 07:04

Does anyone have an Excel VBA function which can return the column letter(s) from a number?

For example, entering 100 should return CV.

28条回答
  •  [愿得一人]
    2020-11-22 07:40

    Furthering on brettdj answer, here is to make the input of column number optional. If the column number input is omitted, the function returns the column letter of the cell that calls to the function. I know this can also be achieved using merely ColumnLetter(COLUMN()), but i thought it'd be nice if it can cleverly understand so.

    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
    

    The trade off of this function is that it would be very very slightly slower than brettdj's answer because of the IF test. But this could be felt if the function is repeatedly used for very large amount of times.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题