vba code to convert number into a column letter

AlphaRisk

New Member
Joined
Mar 22, 2012
Messages
3
I am using some code which currently converts a number into the corrisponding column letter. eg 1 = A, 2 = B, 27 = AA, 56 = BD etc.

Here is the code:

If mycolumn > 26 Then
Mcl = Chr(Int((mycolumn - 1) / 26) + 64) & Chr(Int((mycolumn - 1) Mod 26) + 65)
Else
Mcl = Chr(mycolumn + 64)
End If

Where "mycolumn" = number & "Mcl" = letter.

This code will only calculate correctly for numbers up to 702 as this is ZZ.
How can i add onto this code to calculate for 3 letter column. eg AAA = 703, 4206 = EFT.

I cant seem to get my head around it.
 
Here is the first hit I got on google:

http://support.microsoft.com/kb/833402

I wonder why they didn't test it properly before publishing it?

It works fine up through Column IV, which is where the xl95 - xl03 limit was. It will actually go to IZ before it hiccups, but anything after that is subject to error. SHG posted a code that appears to work without a glitch.

The last code that I posted was from an old method that relies on the cell address rather than manipulating the ASCII characters based on the column number. Anyhow, you now have a couple of choices that will work.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This has always worked for me.
Code:
Function AlphaColumn(colNumber As Long) As String
    
    AlphaColumn = Split(Cells(1, colNumber).Address, "$")(1)
    
End Function
 
Upvote 0
OK, I came to my senses. Here is the code:

Code:
Sub colLtr()
Dim mycolumn
mycolumn = 1450
Mcl = Left(Cells(1, mycolumn).Address(1, 0), InStr(1, Cells(1, mycolumn).Address(1, 0), "$") - 1)
MsgBox Mcl
End Sub

That is exactly what i was looking for! Great job! Thanks for your help!
 
Upvote 0
It works fine up through Column IV, which is where the xl95 - xl03 limit was. It will actually go to IZ before it hiccups, but anything after that is subject to error.
No it doesn't. It works fine up to column AZ. On column 53 it returns "A[" instead of "BA", so it's pretty useless.
 
Upvote 0
Actually ...

Code:
Function ColLtr(ByVal iCol As Long) As String
    ' shg 2012
    If iCol Then ColLtr = ColLtr((iCol - 1) \ 26) & Chr(65 + (iCol - 1) Mod 26)
End Function

I have taken the time to register so I can tell you how delighted I was to discover your recursive code after slogging through so many incredibly verbose solutions. Thank you.
 
Upvote 0
My answer actually checks to see if it is a valid column in the version of Excel that you are using, but other than that the basics of my answer is this:

ColLtr = Replace(Cells(1, iCol).Address(0, 0), 1, "")

This seems less VERBOSE than the answer you used.

and Norie's is about the same length:

AlphaColumn = Split(Cells(1, colNumber).Address, "$")(1)
 
Upvote 0
My answer actually checks to see if it is a valid column in the version of Excel that you are using, but other than that the basics of my answer is this:

ColLtr = Replace(Cells(1, iCol).Address(0, 0), 1, "")

This seems less VERBOSE than the answer you used.

and Norie's is about the same length:

AlphaColumn = Split(Cells(1, colNumber).Address, "$")(1)

Are you offended? Shall I commend your code as well?

There are no "incredibly verbose solutions" in this thread. The principal example to which I referred was Microsoft's. I found shg's post most interesting because it does so elegantly what Microsoft tried to do.

But I did not use shg's code. I used yours instead.
 
Upvote 0
It doesn't really matter, it just sounded like you were criticizing everybody's posts as being verbose. You didn't mention Microsoft in your comment. I'm sorry, I misunderstood.
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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