Function to convert column number to letter?

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

    This formula will give the column based on a range (i.e., A1), where range is a single cell. If a multi-cell range is given it will return the top-left cell. Note, both cell references must be the same:

    MID(CELL("address",A1),2,SEARCH("$",CELL("address",A1),2)-2)

    How it works:

    CELL("property","range") returns a specific value of the range depending on the property used. In this case the cell address. The address property returns a value $[col]$[row], i.e. A1 -> $A$1. The MID function parses out the column value between the $ symbols.

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

    Something that works for me is:

    Cells(Row,Column).Address 
    

    This will return the $AE$1 format reference for you.

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

    This is a function based on @DamienFennelly's answer above. If you give me a thumbs up, give him a thumbs up too! :P

    Function outColLetterFromNumber(iCol as Integer) as String
        sAddr = Cells(1, iCol).Address
        aSplit = Split(sAddr, "$")
        outColLetterFromNumber = aSplit(1)
    End Function
    
    0 讨论(0)
  • 2020-11-22 07:32

    Here, a simple function in Pascal (Delphi).

    function GetColLetterFromNum(Sheet : Variant; Col : Integer) : String;
    begin
      Result := Sheet.Columns[Col].Address;  // from Col=100 --> '$CV:$CV'
      Result := Copy(Result, 2, Pos(':', Result) - 2);
    end;
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 07:33

    this is only for REFEDIT ... generaly use uphere code shortly version... easy to be read and understood / it use poz of $

    Private Sub RefEdit1_Change()
    
        Me.Label1.Caption = NOtoLETTER(RefEdit1.Value) ' you may assign to a variable  var=....'
    
    End Sub
    
    Function NOtoLETTER(REFedit)
    
        Dim First As Long, Second As Long
    
        First = InStr(REFedit, "$")                 'first poz of $
        Second = InStr(First + 1, REFedit, "$")     'second poz of $
    
        NOtoLETTER = Mid(REFedit, First + 1, Second - First - 1)   'extract COLUMN LETTER
    
    End Function
    
    0 讨论(0)
提交回复
热议问题