Function to convert column number to letter?

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

提交回复
热议问题