Function to convert column number to letter?

后端 未结 28 1533
灰色年华
灰色年华 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
    
    0 讨论(0)
  • 2020-11-22 07:50

    The solution from brettdj works fantastically, but if you are coming across this as a potential solution for the same reason I was, I thought that I would offer my alternative solution.

    The problem I was having was scrolling to a specific column based on the output of a MATCH() function. Instead of converting the column number to its column letter parallel, I chose to temporarily toggle the reference style from A1 to R1C1. This way I could just scroll to the column number without having to muck with a VBA function. To easily toggle between the two reference styles, you can use this VBA code:

    Sub toggle_reference_style()
    
    If Application.ReferenceStyle = xlR1C1 Then
      Application.ReferenceStyle = xlA1
    Else
      Application.ReferenceStyle = xlR1C1
    End If
    
    End Sub
    
    0 讨论(0)
  • 2020-11-22 07:51

    If you'd rather not use a range object:

    Function ColumnLetter(ColumnNumber As Long) As String
        Dim n As Long
        Dim c As Byte
        Dim s As String
    
        n = ColumnNumber
        Do
            c = ((n - 1) Mod 26)
            s = Chr(c + 65) & s
            n = (n - c) \ 26
        Loop While n > 0
        ColumnLetter = s
    End Function
    
    0 讨论(0)
  • 2020-11-22 07:51

    Cap A is 65 so:

    MsgBox Chr(ActiveCell.Column + 64)

    Found in: http://www.vbaexpress.com/forum/showthread.php?6103-Solved-get-column-letter

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

    • For example: MsgBox Columns( 9347 ).Address returns **<code>$MUM:$MUM</code>**.

    To return ONLY the column letter(s): Split((Columns(Column Index).Address(,0)),":")(0)

    • For example: MsgBox Split((Columns( 2734 ).Address(,0)),":")(0) returns **<code>DAD</code>**.

      More Examples


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

    This is available through using a formula:

    =SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")
    

    and so also can be written as a VBA function as requested:

    Function ColName(colNum As Integer) As String
        ColName = Split(Worksheets(1).Cells(1, colNum).Address, "$")(1)
    End Function
    
    0 讨论(0)
提交回复
热议问题