Function to convert column number to letter?

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

    And a solution using recursion:

    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 07:42

    what about just converting to the ascii number and using Chr() to convert back to a letter?

    col_letter = Chr(Selection.Column + 96)

    0 讨论(0)
  • 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=> ""
    
    0 讨论(0)
  • 2020-11-22 07:43

    Column letter from column number can be extracted using formula by following steps
    1. Calculate the column address using ADDRESS formula
    2. Extract the column letter using MID and FIND function

    Example:
    1. ADDRESS(1000,1000,1)
    results $ALL$1000
    2. =MID(F15,2,FIND("$",F15,2)-2)
    results ALL asuming F15 contains result of step 1

    In one go we can write
    MID(ADDRESS(1000,1000,1),2,FIND("$",ADDRESS(1000,1000,1),2)-2)

    0 讨论(0)
  • 2020-11-22 07:48

    Here's another way:

    {
    
          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
    
    }
    
    0 讨论(0)
提交回复
热议问题