Find Column Number from Column Name in Excel using C++

Today, we are gonna write a simple C++ program to find Column Number from Column Name in Excel sheets.

Excel sheets are one the most commonly used functionalities with regards to keeping information and keeping up records that are to be utilized frequently.

Each excel sheet has cells where the information is entered. Each one of these cells is identified with the assistance of their unique row and column number combination.

The rows in an excel sheet are numbered with values starting from 1, 2, 3,….., and so on.

The columns, however, are identified with their character names. These start from A, B,………, Y, Z, AA, AB,……., AY, AZ, BA, BB,………, and so on.

get Column Number from Column Name

For example,

Column Name

Column Number

Z

26
AY

51

CB

80

ZZ

702

AAC

705

Method to get Column Number from Column Name

The method which we use to find the column number from column name in Excel is very similar to that of conversion from one number system to another.

Since we have 26 alphabets in total, hence, we can take the base of the number system as 26 here.

Let us take an example to understand better. 

For example, if we have to find the column number for CDA, we will do the following steps.

CDA = 3*26*26 + 4*26 + 1

         = 26(3*26 + 4) + 1

         = 26(0*26 + 3*26 + 4) + 1

         =2133

Same as a number system conversion with base 26.

C++ code to get Column Number from Column Name in Excel

#include<bits/stdc++.h>
#include<iostream>
using namespace std;

//function to display the column number form column name
int Convert_to_Column_Number(string s)
{
  //variables used
  int i, result = 0;

  //loop for iterating each alphabet
  for(i = 0 ; i<s.length() ; i++)
  {
      //multiplying by 26 updating result
      //process similar to a conversion with base 26 system
    result *= 26;
    result += s[i] - 'A' + 1;
  }

  //returning the answer after conversion
  return result;
}

//int main/driver function
int main()
{
    //variables used
    int column_number;
    char column_name[100];

    //taking the input from the user
    cout<<"Enter column name for which you want the column number: ";
    cin>>column_name;

    //calling the function for conversion
    column_number=Convert_to_Column_Number(column_name);

    //displaying result
    cout<<"\nThe column number for column name "<<column_name<<" is: ";
    cout<<column_number<<endl;

  return 0;
}

Output

Enter column name for which you want the column number: ASD

The column number for column name ASD is: 1174

Note

  • In this code, we have not checked if the input from the user is valid or not.
  • Validation of the input could be done by adding a few more lines of code.
  • We can also add error handling/messages in the same program.

Hope this will help!

Take a look at these also, you might find ‘em interesting!

Count number of rows and columns in an excel file in Python

How to find the first empty row of an excel file in Python

Leave a Reply

Your email address will not be published.