Function to convert column number to letter?

后端 未结 28 1998
灰色年华
灰色年华 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:43

    This is a version of robartsd's answer (with the flavor of Jan Wijninckx's one line solution), using recursion instead of a loop.

    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
    

    I've tested this with the following inputs:

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

提交回复
热议问题