Function to convert column number to letter?

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

    Here is a late answer, just for simplistic approach using Int() and If in case of 1-3 character columns:

    Function outColLetterFromNumber(i As Integer) As String
    
        If i < 27 Then       'one-letter
            col = Chr(64 + i)
        ElseIf i < 677 Then  'two-letter
            col = Chr(64 + Int(i / 26)) & Chr(64 + i - (Int(i / 26) * 26))
        Else                 'three-letter
            col = Chr(64 + Int(i / 676)) & Chr(64 + Int(i - Int(i / 676) * 676) / 26)) & Chr(64 + i - (Int(i - Int(i / 676) * 676) / 26) * 26))
        End If
    
        outColLetterFromNumber = col
    
    End Function
    

提交回复
热议问题