Function to convert column number to letter?

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

    LATEST UPDATE: Please ignore the function below, @SurasinTancharoen managed to alert me that it is broken at n = 53.
    For those who are interested, here are other broken values just below n = 200:

    Please use @brettdj function for all your needs. It even works for Microsoft Excel latest maximum number of columns limit: 16384 should gives XFD

    END OF UPDATE


    The function below is provided by Microsoft:

    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
    

    Source: How to convert Excel column numbers into alphabetical characters

    APPLIES TO

    • Microsoft Office Excel 2007
    • Microsoft Excel 2002 Standard Edition
    • Microsoft Excel 2000 Standard Edition
    • Microsoft Excel 97 Standard Edition
    0 讨论(0)
  • 2020-11-22 07:34

    There is a very simple way using Excel power: Use Range.Cells.Address property, this way:

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

    This will return the address of the desired column on row 1. Take it of the 1:

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

    Note that it so fast and powerful that you can return column addresses that even exists!

    Substitute lngRow for the desired column number using Selection.Column property!

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

    This will work regardless of what column inside your one code line for cell thats located in row X, in column Y:

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

    If you have a cell with unique defined name "Cellname":

    Mid(Cells(1,val(range("Cellname").Column)).Address, 2, instr(2,Cells(1,val(range("Cellname").Column)).Address,"$")-2)
    
    0 讨论(0)
  • 2020-11-22 07:34

    So I'm late to the party here, but I want to contribute another answer that no one else has addressed yet that doesn't involve arrays. You can do it with simple string manipulation.

    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
    

    After you have the input in the format $A$1, use the Mid function, start at position 2 to account for the first $, then you find where the second $ appears in the string using InStr, and then subtract 2 off to account for that starting position.

    This gives you the benefit of being adaptable for the whole range of possible columns. Therefore, ColLetter(1) gives back "A", and ColLetter(16384) gives back "XFD", which is the last possible column for my Excel version.

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

    robertsd's code is elegant, yet to make it future-proof, change the declaration of n to type long

    In case you want a formula to avoid macro's, here is something that works up to column 702 inclusive

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

    where A1 is the cell containing the column number to be converted to letters.

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

    Here is a simple one liner that can be used.

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

    It will only work for a 1 letter column designation, but it is nice for simple cases. If you need it to work for exclusively 2 letter designations, then you could use the following:

    ColumnLetter = Mid(Cells(Row, LastColA).Address, 2, 2)
    
    0 讨论(0)
提交回复
热议问题