When user inputs a column letter, convert that to number

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
So plain and simply, I ask a user that what is the column. The user might answer A or IJ or XEE.

Is there a simple way to translate this to a number? Like A = 1, B = 2, XFD = 16384 and so on.

I would need the number in my VBA code to do for example like:
VBA Code:
    Dim lastheaderrow As Integer
    lastheaderrow = controlsheet.Range("LastHeaderRow").Value

    Dim lastheadercolumn As Integer
    lastheadercolumn = 1
   
    Dim firstdatarangecell As Range
    Set firstdatarangecell = Cells(lastheaderrow + 1, lastheadercolumn + 1)
and obviously I can't count "A+1 = 2". The row part works just fine.

Edit. Actually, it doesn't make a lot of sense to have more than 26 (even that's way too many) headercolumns, so I think for this purpose
VBA Code:
    Dim lastheadercolumnletter As String
    lastheadercolumnletter = controlsheet.Range("LastHeaderColumn").Value

    Dim lastheadercolumn As Integer
    lastheadercolumn = Asc(lastheadercolumnletter) - 64
does the trick.
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Perhaps? ColNo = Cells(1, Range("LastHeaderColumn").Value).Column

Although depending on what you're doing, you might be able to get VBA to determine the last column directly?
 
Upvote 0
VBA Code:
'Convert a Column Letter to a Column Number
   lastheadercolumn = Range(lastheadercolumnletter & 1).Column
 
Upvote 0
Solution

Forum statistics

Threads
1,215,140
Messages
6,123,269
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top