Convert number to Excel column name

Excel columns are named with letters: A through Z. Beyond the 26th column, the names are AA, AB, AC, etc. through AZ. After that, the column names are BA, BB, BC, through BZ. And so forth.

Here is a simple function that will convert a column index (starting at zero) to the corresponding column name. The code is in Javascript, but it can easily be adapted to other programming languages.

function excelColumnName( i ) {
    const colNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let columnName = colNames.charAt( i % 26 );
    const prefix = Math.trunc( i / 26 );
    if (prefix > 0) {
        columnName = colNames.charAt( prefix-1 ) + columnName;
    }
    return columnName;
}

excelColumnName( 0 ) returns “A”
excelColumnName( 1 ) returns “B”

excelColumnName( 25 ) returns “Z”
excelColumnName( 26 ) returns “AA”
etc.

Leave a Reply

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

Green Hosting Badge

                  Canadian Badge