Earlier, we learnt how to convert the column number to the letter. But how do we convert column letter to number in excel? In this article, we learn to convert excel column to number.
So we have a function named COLUMN that returns the column number of supplied reference. We will use the COLUMN function with INDIRECT function to get the column number from a given column letter.
Generic Formula to convert letters to numbers in excel
Col_letter: it is the reference of the column letter of which you want to get column number.
Var2:
Let’s see an example to make things clear.
Example: Create an excel column letter to number converter
Here we have some column letters in B2:B5. We want to get corresponding column number (1, 2, 3, etc.) from that given letter (A, B, C, etc.).
Apply the above generic formula here to get column number of a given letter.
Copy it down. You will have the column number of the given column letter in B2:B5.
How does it work?
Well, it is very simple. The idea is to get the first cell’s reference from the given column number. And then use COLUMN function to get the column number of a given column letter.
Here, INDIRECT(B2&»1″) translates to INDIRECT(“A1″). This then gives us the cell reference of A1.
Eventually, we get COLUMN(A1), which then returns the column number of a given column letter.
So yeah, this is how to convert a column letter into a column index number. This is quite easy. Let me know if you have any doubt regarding this function or any other function in advanced excel. The comments section is all yours.
Download file:
Related Articles:
How to Convert Excel Column Number To Letter
Popular Articles:
The VLOOKUP Function in Excel
COUNTIF in Excel 2016
How to Use SUMIF Function in Excel
Return to Excel Formulas List
Download Example Workbook
Download the example workbook
This tutorial demonstrates how to convert a column letter to number in Excel.
Converting Column Letter to Number
To get the number of a column letter in Excel, we will use the COLUMN and INDIRECT Functions.
=COLUMN(INDIRECT(B3&1))
INDIRECT Function
The INDIRECT Function converts a string of text corresponding to a cell reference, into the actual cell reference.
=INDIRECT("A1")
COLUMN Function
The cell reference is then passed to the COLUMN Function, which returns the number of the column.
=COLUMN(Cell Reference)
To illustrate, let’s see some examples below.
=COLUMN(A1) has a result of 1. This is because the cell reference “A1” is in column number 1.
=COLUMN(B1) has a result of 2. This is because the cell reference “B1” is in column number 2.
=COLUMN(H1) has a result of 8. This is because the cell reference “H1” is in column number 8.
Convert Column Letter to Number in Google Sheets
The combination of COLUMN and INDIRECT Functions works exactly the same in Google Sheets as in Excel:
My Comments
ARich gives a good solution and shows the method I used for a while but Sancarn is right, its not optimal. It’s a little slower, will cause errors if the wrong input is given, and is not very robust. Sancarn is on the right track, but lacks a little error checking: for example, getColIndex(«_») and getColIndex(«AE»), will both return 31. Other non-letter characters (ex: «*») sometimes return various negative values.
Working Function
Here is a function I wrote that will convert a column letter into a number. If the input is not a column on the worksheet, it will return -1 (unless AllowOverflow is set to TRUE).
Function ColLetter2Num(ColumnLetter As String, Optional AllowOverflow As Boolean) As Double
'Converts a column letter to a number (ex: C to 3, A to 1, etc). Returns -1 if its invalid.
' @ColumnLetter - the letter(s) to convert to a number.
' @AllowOverflow - if TRUE, can return a number greater than the max columns.
On Error GoTo invalidCol
If Len(ColumnLetter) = 0 Then GoTo invalidCol
Dim thisChar As String
For i = 1 To Len(ColumnLetter) 'for each character in input
thisChar = Mid(ColumnLetter, i, 1) 'get next character
If Asc(UCase(thisChar)) >= 65 And Asc(UCase(thisChar)) <= 90 Then 'if the character is a letter
ColLetter2Num = ColLetter2Num + (26 ^ (Len(ColumnLetter) - i)) * (Asc(UCase(thisChar)) - 64) 'add its value to the return
Else
GoTo invalidCol 'if the character is not a letter, return an error
End If
If AllowOverflow = False And (ColLetter2Num = 0 Or ColLetter2Num > Columns.Count) Then
'if the value is not inside the bounds of the sheet, return an error and stop
invalidCol:
ColLetter2Num = -1 'error
Exit Function 'stop checking
End If
Next i
End Function
Examples
Sub test()
Debug.Print ColLetter2Num("A") 'returns 1
Debug.Print ColLetter2Num("IV") 'returns 256 (max columns for excel 2003 and prior))
Debug.Print ColLetter2Num("XFD") 'returns -1 (invalid because IV is the last column for .xls workbooks)
Debug.Print ColLetter2Num("XFD", True) 'returns 16384 (does not return -1 because AllowOverflow = TRUE)
Debug.Print ColLetter2Num("A_", True) 'returns -1 (invalid because "_" is not a column)
Debug.Print ColLetter2Num("132", True) 'returns -1 (invalid because "1" is not a column)
If ColLetter2Num("A") <> -1 Then
Debug.Print "The input is a valid column on the sheet."
Else
Debug.Print "The input is NOT a valid column on the sheet."
End If
End Sub
This post will guide you how to convert column letter to number in Excel. How do I convert letter to number with a formula in Excel. How to convert column letter to number with VBA macro in Excel. How to use a User Defined Function to convert letter to number in Excel.
- Convert Column Letter to Number with a Formula
- Convert Column Letter to Number with VBA Macro
- Convert Column Letter to Number with User Defined Function
- Convert Number to Column Letter
- Video: Convert Column Letter to Number
Table of Contents
- Convert Column Letter to Number with a Formula
- Convert Column Letter to Number with VBA Macro
- Convert Column Letter to Number with User Defined Function
- Convert Number to Column Letter
- Related Functions
Convert Column Letter to Number with a Formula
If you want to convert a column letter to a regular number, and you can use a formula based on the COLUMN function and the INDIRECT function to achieve the result.
For example, you need to convert a column letter in Cell B1 to number
Just like this:
=COLUMN(INDIRECT(B1&1))
Type this formula into a blank cell such as: C1, and press Enter key, and then drag the AutoFill Handle over to other cells to apply this formula.
The INDIRECT function will convert the text into a proper Excel reference and then pass the result to Column function to get the column number for the reference.
Convert Column Letter to Number with VBA Macro
You can also use an Excel VBA Macro to achieve the result of converting column letter into its corresponding numeric value. Just do the following steps:
#1 open your excel workbook and then click on “Visual Basic” command under DEVELOPER Tab, or just press “ALT+F11” shortcut.
#2 then the “Visual Basic Editor” window will appear.
#3 click “Insert” ->”Module” to create a new module.
#4 paste the below VBA code into the code window. Then clicking “Save” button.
Sub ConvertColumnLetterToNumber() Dim myRng As Range Dim cNum As Double Set myRng = Application.Selection Set myRng = Application.InputBox("select one range that contain column letters", "ConvertColumnLetterToNumber", myRng.Address, Type:=8) For Each myCell In myRng cNum = Range(myCell & 1).Column myCell.Resize(1).Offset(0, 1) = cNum Next End Sub
#5 back to the current worksheet, then run the above excel macro. Click Run button.
#6 select one range that contain column letters, such as B1:B4
#7 let’s see the result:
Convert Column Letter to Number with User Defined Function
Excel does not have a built in formula to convert column letter to a numeric number, but you can write down a User Defined Function to achieve the result. Just do the following steps:
#1 repeat the above step 1-3
#2 paste the below VBA code into the code window. Then clicking “Save” button.
Function ConvertLetterToNum(ColumnLetter As String) As Double Dim cNum As Double 'Get Column Number from Alphabet cNum = Range(ColumnLetter & "1").Column 'Return Column Number ConvertLetterToNum = cNum End Function
#3 back to the current worksheet, then type the following formula in a blank cell. press Enter key.
=ConvertLetterToNum(B1)
#4 select the cell C1, and drag the AutoFill Handle over to other cells to apply this formula.
If you want to convert column number to an Excel Column letter, you can use another formula based on the SUBSTITUTE function and the ADDRESS function. Just like this:
=SUBSTITUTE(ADDRESS(1,C1,4),"1","")
Type this formula into Cell D1, and press Enter key. And then drag the AutoFill handle over to other cells to apply this formula.
Let’s see the last result:
Video: Convert Column Letter to Number
- Excel Substitute function
The Excel SUBSTITUTE function replaces a new text string for an old text string in a text string.The syntax of the SUBSTITUTE function is as below:= SUBSTITUTE (text, old_text, new_text,[instance_num])…. - Excel ADDRESS function
The Excel ADDRESS function returns a reference as a text string to a single cell.The syntax of the ADDRESS function is as below:=ADDRESS (row_num, column_num, [abs_num], [a1], [sheet_text])…. - Excel INDIRECT function
The Excel INDIRECT function returns the cell reference based on a text string, such as: type the text string “A2” in B1 cell, it just a text string, so you can use INDIRECT function to convert text string as cell reference…. - Excel COLUMN function
The Excel COLUMN function returns the first column number of the given cell reference.The syntax of the COLUMN function is as below:=COLUMN ([reference])….
Summary
To convert a column letter to an regular number (e.g. 1, 10, 26, etc.) you can use a formula based on the INDIRECT and COLUMN functions.
In the example shown, the formula in C5 is:
=COLUMN(INDIRECT(B5&"1"))
Generic formula
=COLUMN(INDIRECT(letter&"1"))
Explanation
The first step is to construct a standard «A1» style reference using the column letter, by adding a «1» with concatenation:
B5&"1"
This results in a text string like «A1» which is passed into the INDIRECT function.
Next, the INDIRECT function transforms the text into a proper Excel reference and hands the result off to the COLUMN function.
Finally, the COLUMN function evaluates the reference and returns the column number for the reference.
Author
Dave Bruns
Hi — I’m Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.
Related Information
There are hundreds and hundreds of Excel sites out there. I’ve been to many and most are an exercise in frustration. Found yours today and wanted to let you know that it might be the simplest and easiest site that will get me where I want to go.
Get Training
Quick, clean, and to the point training
Learn Excel with high quality video training. Our videos are quick, clean, and to the point, so you can learn Excel in less time, and easily review key topics when needed. Each video comes with its own practice worksheet.
View Paid Training & Bundles
Help us improve Exceljet
This tutorial explains how to convert column number to column letter, and convert column letter to column number in Excel.
Column number refers to the column expressed in integer, while column letter refers to column expressed in alphabet.
For example, for Cell D1, column number is 4, while column letter is D.
Excel convert column number to column letter
To convert column number to column letter, make use of Address Function, which combines column number and row number and convert to a Cell address with column letter (such as A1).
Syntax of Excel Address Function
ADDRESS( row, column, [abs_num], [a1], [sheet_text] )
Example
Assume that you have a column number 4, you want to convert to letter D.
Formula | Value | Explanation | |
Step 1 | =ADDRESS(1,4,4) | D1 | Put a dummy row number 1 in address, return relative reference |
Step 2 | =SUBSTITUTE(ADDRESS(1,4,4),1,””) | D | Replace 1 as nothing |
Excel convert column letter to column number
To convert column number to column letter, make use of Indirect Function, which turns a Text into Reference.
Assume that you have a column letter D, you want to convert to number 4.
=COLUMN(INDIRECT("D1"))
VBA custom Function – convert column letter to column number
This Function takes an argument ColNm (colNm means column name, e.g. A,B,C) and return column number.
VBA code
Public Function wColNum(ColNm) wColNum = Range(ColNm & 1).Column End Function
Explanation of VBA code
Take colNm = A as example
– use dummy row number 1 to create a Range(A1)
– return column number of Range(A1)
Example
Formula | Result |
=wColNum(“D”) | 4 |
=wColNum(“AA”) | 27 |
VBA custom Function – convert column number to column letter
This Function takes an argument ColNum (colNum means column number, e.g. 1,2,3) and return column name (A,B,C)
VBA code
Public Function wColNm(ColNum) wColNm = Split(Cells(1, ColNum).Address, "$")(1) End Function
Explanation of VBA code
Take ColNum = 1 as example
– use dummy row number 1 to create a dummy Cells(1,1)
– return the address Cells(1,1), which is $A$1
– Split $A$1 by $, get the array item (1)
Example
Formula | Result |
=wColNm(1) | A |
=wColNm(27) | AA |
Outbound References
https://support.office.com/en-us/article/address-function-47657e92-81ae-47f8-87cd-62d4f30c774d?ui=en-US&rs=en-US&ad=US&fromAR=1