Function to convert column number to letter?

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

    Sub GiveAddress()
        Dim Chara As String
        Chara = ""
        Dim Num As Integer
        Dim ColNum As Long
        ColNum = InputBox("Input the column number")
    
        Do
            If ColNum < 27 Then
                Chara = Chr(ColNum + 64) & Chara
                Exit Do
            Else
                Num = ColNum / 26
                If (Num * 26) > ColNum Then Num = Num - 1
                If (Num * 26) = ColNum Then Num = ((ColNum - 1) / 26) - 1
                Chara = Chr((ColNum - (26 * Num)) + 64) & Chara
                ColNum = Num
            End If
        Loop
    
        MsgBox "Address is '" & Chara & "'."
    End Sub
    

提交回复
热议问题