Excel - Excel columns: Converting numbers to letters and vice versa

Asked By Jack
23-Mar-08 04:07 PM
Hello,
My app is using Excel spreadsheet.
User has a choice of preselecting some of the sheet's columns.
Now:
Excel is using letters as the visual  indexing, of the columns but when
programming the corresponding number (A -->1, B -->2..., AA-->28, AB-->29
etc) must be used..

What will be the best method of converting those letters into numbers?
Jack
Excel 2007
(1)
Excel
(1)
ActiveCell.Column
(1)
VB
(1)
GetColNum
(1)
AsciiValue
(1)
ToNumber
(1)
ToAlpha
(1)
  Jack replied...
23-Mar-08 04:13 PM
As usual I have made a mistake.

Excel column AA is not equal to 28
A1 -->28
A2 --> 29
etc
Jack
  Jarek Kujawa replied...
24-Mar-08 06:37 AM
am not sure what you mean exactly but one way might be:

k =3D ActiveCell.Column
  Jack replied...
23-Mar-08 04:49 PM
I need to convert column index (shown as letters) to the number.
For example:
Column: ABCD
What is the corresponding number of that column?
Jack

am not sure what you mean exactly but one way might be:

k = ActiveCell.Column
  Leith Ross replied...
24-Mar-08 06:37 AM
Hello Jack,

Use the Cells method to create a Range address and return only the
column number.

For example:
C = Cells(1, "ABCD").Column

Sincerely,
Leith Ross
  Jack replied...
23-Mar-08 06:50 PM
Thank you.
That will work only, when my app is connected to Excel.
I need more general solution.
Jack
  Jeff Johnson replied...
23-Mar-08 07:07 PM
Uh, no. You HAVE made a mistake, but you were closer the first time.

AA = 27
AB = 28

There's no such thing as column A1. A1 is a cell.

First, you have to consider whether you're going to support only version of
Excel before 2007, which only supported 256 columns (IX, or something like
that) or Excel 2007 as well, which supports...I think...1024 columns.

For the first case, where you can have at most two letters in the column
name, for anything beyond Z you have to take the first letter, convert it to
1 - 26, and multiply that by 26, then add the second letter.

For example, CQ = 3 * 26 + 17.

It's the same concept for the second case (Excel 2007), but now you have to
determine if you have 3 letters and multiply the first by 26 * 26 (26
squared) and then add the second * 26 and then the third.

In other words, you've got a base 26 number system.
  Mike Williams replied...
23-Mar-08 07:07 PM
Your question does not make sense! What are you going to do wth this "number"
when you have got it? What *exactly* are you trying to do, and why?

Mike
  Gord Dibben replied...
23-Mar-08 11:32 PM
Function GetColNum(myColumn As String) As Integer
GetColNum = Columns(myColumn & ":" & myColumn).Column
End Function

=GetColNum("AA") returns 27  ("IV") returns 256

I don't run 2007 so can't test past "IV"


Gord
  Jack replied...
24-Mar-08 02:17 AM
It does make sense, just again I have made a mistake.
In Excel 2007 the maximum column index is:  XFD
There are not 4 letters long column's indexes.

I know how to do it now..
Jack
  Rick Rothstein \(MVP - VB\) replied...
24-Mar-08 04:49 AM
Not sure about "best", but the functions below should work well past any
thing you may want to handle. The ToNumber function (which is the one that
addresses your question) will convert letter combination up to BRUTMHYHIIZO
(which is converts to 9999999999999999). Likewise, the ToAlpha function
(which is the inverse of the ToNumber function) will accept values up to
9999999999999999 (which is converts to BRUTMHYHIIZO).

Rick

Function ToNumber(Value As String) As Variant
Dim x As Integer
If Format$(Value, "@@@@@@@@@@@@") > "BRUTMHYHIIZO" _
Or Value Like "*[!A-Za-z]*" Then
ToNumber = -1
Else
ToNumber = CDec(0)
For x = Len(Value) To 1 Step -1
ToNumber = ToNumber + _
(Asc(UCase$(Mid$(Value, x, 1))) - 64) * _
26 ^ (Len(Value) - x)
Next
End If
End Function

Function ToAlpha(ByVal Value As Variant) As String
Dim AsciiValue As Variant
If Len(Value) > 16 Or Value Like "*[!0-9]*" Then
ToAlpha = "###"
Else
Value = CDec(Value)
Do While Value > 0
AsciiValue = CDec(64 + Value - 26 * Int(Value / 26))
If AsciiValue = 64 Then AsciiValue = 90
ToAlpha = Chr$(AsciiValue) & ToAlpha
Value = Int(Value / 26)
If AsciiValue = 90 Then Value = Value - 1
Loop
End If
End Function
  argusy replied...
27-Mar-08 12:50 AM
Nice of you to tell the rest how you got there, but that's no mystery

I take it you finally, at last, worked out that there's 26 letters in
the alphabet, and you did some homework on powers and bases, and learnt
there's other counting systems than the decimal system

like binary, and octal, and hexadecimal, and ...

So 'A- Z' is 1 to 26

'AA' is 27, 'AB' is 28....  (like 26 + 1, 26 + 2)
The base is 26 ....
so each letter to the left is a power of 26

so 'ABC' is
26 * 26, (26^2 * 1 ('A')),
plus 26 *  2, (26^1 * 2 ('B')),
plus  1 *  3, (26^0 * 3 ('C'))

and 'XFD' then is 16384
or 2^14
or Hexadecimal 4000 - nice round number, innit?
strange that - 'XFD' being the last column .....

I think most of us in this newsgroup would know the above, but took you
a while to see it, didn't it
  Jack replied...
29-Mar-08 03:43 PM
Problem solved.
Just removed the reference, saved the project, restarted the project and
added again the same reference.
Any thoughts on that?
Jack
  Rick Rothstein \(MVP - VB\) replied...
29-Mar-08 04:24 PM
Which reference did your remove and then add back in again? In one of your
previous responses, you said you needed to be able to convert the letters to
their numerical equivalent without being connected to Excel; that is, you
said you needed a general solution. What reference is associated with the

For your stated general solution, did you look at the ToNumber function I
posted in a previous response in this thread? While I admit this function
handles much, much more than you need to for your application, I would point
out that there is no time penalty in using it for the range of letters you
are interested it (the function only loops as many times as there are
letters in the argument passed into it).

Rick
  Jack replied...
29-Mar-08 04:47 PM
Sorry, it supposed to be a reply to the other thread.
Jack
  Rick Rothstein \(MVP - VB\) replied...
29-Mar-08 05:17 PM
So, has the question you asked in **this** thread been resolved yet?

Rick
Create New Account
help
Simple Macro - I think. . . Copy - Paste on Click Excel Excel 2007 I have a workbook with 2 Sheets: Sheet1 and Sheet2 Sheet1 contains a list of populated with the contents of the cell that is clicked. Any suggestions? Thanks in advance Excel Miscellaneous Discussions Excel 2007 (1) Sheets (1) ActiveCell.Column (1) Workbook (1) VBA (1) BeforeDoubleClick (1) ActiveCell (1) Window (1) Hi, Try this code
Formel raus Wert rein -Kommentar behalten Excel [Excel 2007] Hi Leute, ich bin auf das alte Thema gesto?en in einem Excel-Blatt die Formeln durch ihre Werte zu ersetzen. Im Netz habe ich die 2 Methoden a) wobei die Kommentare bleiben? ?hm, alles andere (Formate. . .) will ich auch behalten. TIA, Hans Excel - German Discussions Office 2003 (1) Worksheets (1) Excel 2007 (1) Excel 2003 (1) Sheets (1) Vista (1) VBA (1) XP (1) Hi, Versuch mal: Markiere den
Simple code makes Excel 2007 crash Excel Hello All, I use this code to check if a value changes in a specific update the adjacent cell (one to the right) with today's date. However, it makes Excel 2007 crash. Any advice? Private Sub Worksheet_Change(ByVal Target As Range) If ((ActiveCell.Column = 6) And (ActiveCell.Row > 5)) Then ActiveCell.Offset(0, 1).Value = Str(Date) End If End Sub Thanks! Mathieu Excel Programming Discussions Excel 2007 (1) Intersect (1) ActiveCell.Offset (1) ActiveCell.Column (1) ActiveCell.Value
How to determine cursor location? Excel Excel 2007: If a user clicks in Cell 'B4' or. . . 'E12' in a worksheet, is there a only need to build 24 x 2 criteria for 24 DSUM updates. Any thoughts? - -Dan Excel Miscellaneous Discussions Excel 2007 (1) ActiveCell.Address (1) ActiveCell.Column (1) Worksheet (1) Workbook (1) Macro (1) If your plan is to have the user
Function to determine current cursor position? Excel Is there an Excel 2007 fuction where I can learn which cell a user clicks on? For example: If the can also return the Column (in this example, 'B' or '2') Thanks in advance. - -Dan Excel Worksheet Discussions Excel 2007 (1) Activecell.address (1) Activecell.column (1) Hi, Activecell.column Activecell.row Activecell.address Take your pick Mike keywords: Function, to