Excel, VBA macro functions to convert column number to letter, or letter to column number

I’ve tested these in Excel 2010 in sheets with over 16,000 columns and the functions work even for large numbers of columns.

Function ColumnLetterToNumber(ColumnLetters As String) As Long
    ColumnLetterToNumber = Range(ColumnLetters & "1").Column
End Function
Function ColumnNumberToLetters(ColumnNumber As Integer) As String
    Dim strLetters As String
    strLetters = Cells(1, ColumnNumber).Address(1, 0)
    ColumnNumberToLetters = Left(strLetters, InStr(1, strLetters, "$") - 1)
End Function

These functions are not necessary usually, because almost all things in Excel VBA can be done using ranges specified as numbers, for example,

Sheets("Sheet1").Cells(2,3).Value = "Hello"

Where 2 is the row number, and 3 is the column number, where column 1 corresponds to column A.

Leave a Reply

Your email address will not be published. Required fields are marked *