Pieter van der Westhuizen

Convert an Excel column number to a column name or letter: C# and VB.NET examples

Excel worksheet size has increased dramatically from Excel 2000 to Excel 2007. In fact, the number of rows an Excel 2013 worksheet can support has increased 16 times and the number of columns 64 times more than the number Excel 2000 could handle. Below is a table displaying the number of columns and rows the different versions of Excel can contain:

Microsoft Excel version Number of worksheet rows Number of worksheet columns
Excel 2013 1 048 576 16 384
Excel 2010 1 048 576 16 384
Excel 2007 1 048 576 16 384
Excel 2003 65 536 256
Excel 2000 65 536 256

Getting the column letter by column index

There are a lot of examples floating around on the internet on how to convert Excel column numbers to alphabetical characters. There are a few ways to get the column letter, using either vanilla C# or VB.NET, Excel formulas or the Excel object model. Let’s take a look at some of the solutions.

Using C# or VB.NET

C# example:

static string ColumnIndexToColumnLetter(int colIndex)
{
    int div = colIndex;
    string colLetter = String.Empty;
    int mod = 0;
 
    while (div > 0)
    {
        mod = (div - 1) % 26;
        colLetter = (char)(65 + mod) + colLetter;
        div = (int)((div - mod) / 26);
    }
    return colLetter;
}

C# usage:

string columnLetter = ColumnIndexToColumnLetter(100); // returns CV

VB.NET example:

Private Function ColumnIndexToColumnLetter(colIndex As Integer) As String
    Dim div As Integer = colIndex
    Dim colLetter As String = String.Empty
    Dim modnum As Integer = 0
 
    While div > 0
        modnum = (div - 1) Mod 26
        colLetter = Chr(65 + modnum) & colLetter
        div = CInt((div - modnum) \ 26)
    End While
 
    Return colLetter
End Function

VB.NET usage:

Dim columnLetter As String = ColumnIndexToColumnLetter(85) ' returns CG

Using an Excel formula to convert a column number to column letter

Of course, if you’re in Excel and need to get the column letter based on a number, you can always use the ADDRESS function. The function is pretty straight forward; it requires a row and column number, and in our case, we need to specify the abs_num parameter, which can be one of four possible options:

abs_num Returns
1 or omitted Absolute
2 Absolute row and relative column
3 Relative row and absolute column
4 Relative

Consider the following formula:
=ADDRESS(1,5,1)

By changing the abs_num parameter, you’ll see the following result:

abs_num Formula Result Description
1 or omitted =ADDRESS(1,5,1) $E$1 Absolute
2 =ADDRESS(1,5,2) E$1 Absolute row and relative column
3 =ADDRESS(1,5,3) $E1 Relative row and absolute column
4 =ADDRESS(1,5,4) E1 Relative

Notice that in all the four cases, we get the column letter, in this case E, back as well as the row number. By setting the abs_num parameter to 4 and replacing the row number, we can effectively return the column letter using the following formula:

=SUBSTITUTE(ADDRESS(1,5,4),”1″,””)

Using the Excel Object Model to convert column numbers to alphabetic format

Of course, you could always use the Excel object model to get the column letter. Much like the ADDRESS function, you use the Address property of the Range object to determine the address of the column and then simply replace the row number as illustrated below:

C# example:

private void columnLetterRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    Worksheet sheet = null;
    Range range = null;
    string colLetter = string.Empty;
 
    try
    {
        sheet = (Worksheet)ExcelApp.ActiveSheet;
        range = sheet.Cells[1, 50] as Excel.Range;
        colLetter = range.Address[false, false, XlReferenceStyle.xlA1];
        colLetter = colLetter.Replace("1", "");
        MessageBox.Show(String.Format(
            "Column letter for column number 50 is : {0}", colLetter));
    }
    finally
    {
        if (sheet != null) Marshal.ReleaseComObject(sheet);
        if (range != null) Marshal.ReleaseComObject(range);
    }

VB.NET example:

Private Sub GetColumnLetterRibbonButton_OnClick(sender As Object, _
    control As IRibbonControl, pressed As Boolean) Handles _
    GetColumnLetterRibbonButton.OnClick
 
    Dim sheet As Excel.Worksheet = Nothing
    Dim range As Excel.Range = Nothing
    Dim colLetter As String = String.Empty
 
    Try
        sheet = DirectCast(ExcelApp.ActiveSheet, Excel.Worksheet)
        range = TryCast(sheet.Cells(1, 50), Excel.Range)
        colLetter = range.Address(False, False, Excel.XlReferenceStyle.xlA1)
        colLetter = colLetter.Replace("1", "")
        MessageBox.Show(String.Format( _
            "Column letter for column number 50 is : {0}", colLetter))
    Finally
        If sheet IsNot Nothing Then
            Marshal.ReleaseComObject(sheet)
        End If
        If range IsNot Nothing Then
            Marshal.ReleaseComObject(range)
        End If
    End Try
End Sub

Getting the column index by column letter

What should you do in order to reverse the scenario? How do you get the Excel column letter when all you have is the column index? Let’s take a look at how you can accomplish this using C#, VB.NET, an Excel formula and the Excel object model.

Using C# or VB.NET

C# example:

public static int ColumnLetterToColumnIndex(string columnLetter)
{
    columnLetter = columnLetter.ToUpper();
    int sum = 0;
 
    for (int i = 0; i < columnLetter.Length; i++)
    {
        sum *= 26;    
        sum += (columnLetter[i] - 'A' + 1);
    }
    return sum;
}

C# usage:

int columnIndex = ColumnLetterToColumnIndex("XFD"); // returns 16384

VB.NET example:

Public Function ColumnLetterToColumnIndex(columnLetter As String) As Integer
    columnLetter = columnLetter.ToUpper()
    Dim sum As Integer = 0
 
    For i As Integer = 0 To columnLetter.Length - 1
        sum *= 26
        Dim charA As Integer = Char.GetNumericValue("A")
        Dim charColLetter As Integer = Char.GetNumericValue(columnLetter(i))
        sum += (charColLetter - charA) + 1
    Next
    Return sum
End Function

VB.NET usage:

Dim columnIndex As Integer = ColumnLetterToColumnIndex("ADX") ' returns 703

Using an Excel formula to get the column number

Getting the column index directly in Excel is very easy. Excel has a built-in COLUMN() function. This function accepts a column reference, which is the column address. One catch though, is you have to combine the row number with the column address in order for it to work, e.g.:
=COLUMN(ADX1)

Using the Excel Object Model to convert a column letter to column number

Getting the column index is easily accomplished using the Excel object model. You first need to get a reference to a range object and then get the column number via the Column property of the Range object.

C# example:

private void getColumnIndexRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    Worksheet sheet = null;
    Range range = null;
    int colIndex = 0;
 
    try
    {
        sheet = (Worksheet)ExcelApp.ActiveSheet;
        range = sheet.Range["ADX1"] as Excel.Range;
        colIndex = range.Column;
        MessageBox.Show(String.Format(
            "Column index for column letter ADX is : {0}", colIndex));
    }
    finally
    {
        if (sheet != null) Marshal.ReleaseComObject(sheet);
        if (range != null) Marshal.ReleaseComObject(range);
    }
}

VB.NET example:

Private Sub GetColumnIndexRibbonButton_OnClick(sender As Object, _
    control As IRibbonControl, pressed As Boolean) Handles _
    GetColumnIndexRibbonButton.OnClick
 
    Dim sheet As Excel.Worksheet = Nothing
    Dim range As Excel.Range = Nothing
    Dim colIndex As Integer = 0
 
    Try
        sheet = DirectCast(ExcelApp.ActiveSheet, Excel.Worksheet)
        range = TryCast(sheet.Range("ADX1"), Excel.Range)
        colIndex = range.Column
        MessageBox.Show([String].Format( _
            "Column index for column letter ADX is : {0}", colIndex))
    Finally
        If sheet IsNot Nothing Then
            Marshal.ReleaseComObject(sheet)
        End If
        If range IsNot Nothing Then
            Marshal.ReleaseComObject(range)
        End If
    End Try
End Sub

Thank you for reading. Until next time, keep coding!

Available downloads:

This sample Excel add-in was developed using Add-in Express for Office and .net:

Sample add-ins and applications (C# and VB.NET)

You may also be interested in:

21 Comments

  • Henri Pellemans says:

    Hi Pieter,

    I created the following short VB.NET UDF using the Address function:

    Public Shared Function excelColumnName(ByVal colNumber As Integer) As String
    Return CurrentInstance.CallWorksheetFunction(ADXExcelWorksheetFunction.Address, 1, colNumber, 4).ToString.Replace(“1”, “”)
    End Function

    Regards,

    Henri

  • Pieter van der Westhuizen says:

    Hi Henri,

    Thank you for the suggestion! That is also a good way to get the column name.

  • mounika says:

    Thanks for the information .. It was helpful

  • Giampiero Di Ludovico says:

    Hi Pieter,
    there is a typo, rows limit for newer versions of Excel is 1048576 rather than 10485776 (you put a double seven).
    Regards
    Giampiero

  • Pieter van der Westhuizen says:

    Hi Giampiero,

    You’re right. That was a typing mistake on my part.
    Well spotted. Thank you!

  • Joseph McKinley says:

    The getting column index from column name VB .net example code does not work. Change 2 lines (as below):
    Dim charA As Integer = Asc(“A”)
    Dim charColLetter As Integer = Asc(columnLetter(i))
    that will fix the issue.

  • Andrei Smolin (Add-in Express Team) says:

    Thank you, Joseph!

  • GILBERTO COUTO PEREIRA ROSA says:

    I love you! rs

    Thank you so much, J!

  • Varun says:

    Hi Giampiero,

    can you help me, how to increase and decrease size of excel sheet column’s as per requirement using C# programming.

    Ex.
    single columnsize = 200

    Regards,
    Varun

  • Andrei Smolin (Add-in Express Team) says:

    Hello Varun,

    Record a VBA macro while changing the size of a column. The macro will show you the objects and members that you need to use in your code.

  • Varun says:

    Hello Anderi,

    Thanks…

  • Prashant says:

    Hi Pieter

    Thanks, wonderful solution…

    Regards,
    Prashant

  • Jörg says:

    Hello,
    thank you.
    Exactly i was looking for

  • Claudio says:

    Hello, thank you!!!!

  • Chuck says:

    How do I return the column letter based off a for loop that loops through the excel sheet rows to find invalid data types. Example row 2 column H (column 8), I want to return this column letter based off that row 2. I found the “Get column letter by column index” part of this page to be useful but now I want to base the return of the header letter by the row loop. Please helpppp

  • silvio says:

    Thank you!!!

  • Andrei Smolin (Add-in Express Team) says:

    Hello Chuck,

    Column indices/letters don’t have a relation to the row being scanned: whatever row you have, the column indices/letters are the same.

    If you want to get a column letter relative to column 8, pass a relative column index to ColumnIndexToColumnLetter. A relative column index would be =range1.Column – range2.Column; range1 representing a cell whose relative column letter you need to get and range2 representing a cell in column 8. Remember that relative column index shouldn’t be negative.

  • Chuck says:

    @Andrei Smolin

    Could I use a nested for loop to iterate through the columns and rows??

  • Andrei Smolin (Add-in Express Team) says:

    This isn’t forbidden. Note that I may not understand your goal.

  • Andrei Fogoros says:

    Hello,
    Under what license is the VB.NET example licensed (see below)?

    VB.NET example:

    Public Function ColumnLetterToColumnIndex(columnLetter As String) As Integer
    columnLetter = columnLetter.ToUpper()
    Dim sum As Integer = 0

    For i As Integer = 0 To columnLetter.Length – 1
    sum *= 26
    Dim charA As Integer = Char.GetNumericValue(“A”)
    Dim charColLetter As Integer = Char.GetNumericValue(columnLetter(i))
    sum += (charColLetter – charA) + 1
    Next
    Return sum
    End Function

    Thanks in advance!
    Andrei

  • Andrei Smolin (Add-in Express Team) says:

    Hello Andrei,

    You can use that code in any way without any reference to us.

    Regards from Poland (GMT+2),

    Andrei Smolin
    Add-in Express Team Leader

Post a comment

Have any questions? Ask us right now!