Thursday, December 10, 2015

How to return column letter of a cell in excel?

Excel is cell based data processing application. Mostly, used for finance and reporting purpose in daily life. It uses various Functions/Formulas to generate output from various measures. Most of the functions in excel make use of cells which is intersection of row and column. Thus, in excel formula requires column and row index as a input which in nothing but cell Address. Many times, we need column letter as a input in calculation.

On other hand, Excel does not have any function which will provide column letter as a result. Therefore. someone needs to write complex formulas to get column letter as a output. And writing complex formula in excel is quite time consuming task. which results into slowdown of excel calculation task and make excel heavy. As a result excel take more time to open the file.


Hence to make this task simple, I have a user defined function which will return column letter just providing column number as a input parameter.

Here is the function which will make your task easy and save time to create complex formulas to get column letter:

Function Column_Letter(ColumnNumber As Long) As String
' Author: Dreams24
' Written for VBA Tricks and tips blog
' https://vbatricksntips.com


    If ColumnNumber < 26 Then

        ' Columns A-Z
        ColumnLetter = Chr(ColumnNumber + 64)
    ElseIf ColumnNumber > 26 And ColumnNumber <= 702 Then
        ' Columns AA-ZZ
        ColumnLetter = Chr(Int((ColumnNumber - 1) / 26) + 64) & Chr(((ColumnNumber - 1) Mod 26) + 65)
    Else
        ' Columns AAA-XFD
 Column_Letter=Left(Cells(1,ColumnNumber).Address(1,0),InStr(1,Cells(1,ColumnNumber).Address(1, 0), "$") - 1)
    End If

End Function

No comments: