Function to convert column number to letter?

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

提交回复
热议问题