Function to convert column number to letter?

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

    Easy way to get the column name

    Sub column()
    
    cell=cells(1,1)
    column = Replace(cell.Address(False, False), cell.Row, "")
    msgbox column
    
    End Sub
    

    I hope it helps =)

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

    Just one more way to do this. Brettdj's answer made me think of this, but if you use this method you don't have to use a variant array, you can go directly to a string.

    ColLtr = Cells(1, ColNum).Address(True, False)
    ColLtr = Replace(ColLtr, "$1", "")
    

    or can make it a little more compact with this

    ColLtr = Replace(Cells(1, ColNum).Address(True, False), "$1", "")
    

    Notice this does depend on you referencing row 1 in the cells object.

    0 讨论(0)
  • 2020-11-22 07:56
    Function fColLetter(iCol As Integer) As String
      On Error GoTo errLabel
      fColLetter = Split(Columns(lngCol).Address(, False), ":")(1)
      Exit Function
    errLabel:
      fColLetter = "%ERR%"
    End Function
    
    0 讨论(0)
  • 2020-11-22 07:57

    This function returns the column letter for a given column number.

    Function Col_Letter(lngCol As Long) As String
        Dim vArr
        vArr = Split(Cells(1, lngCol).Address(True, False), "$")
        Col_Letter = vArr(0)
    End Function
    

    testing code for column 100

    Sub Test()
        MsgBox Col_Letter(100)
    End Sub
    
    0 讨论(0)
提交回复
热议问题