How to return a column's alpha value?

RusheR

Board Regular
Joined
Feb 18, 2008
Messages
97
I have this for a given cell.


n = ActiveCell.Column
i = ActiveCell.Row


n always returns a numeric vaule. How can I get it to return the columns alpha value (i.e. "C" instead of "3")

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Could you explain why you need the column returned as alpha?

It can be returned as a text string with Chr(n + 64), but allowances need to be made, that will only work up to column Z, it can still be done but depending on what you're going to do with the returned "C" next, there may be a more practical method.
 
Upvote 0
I needed this recently and found this code.


Function ColLetter(iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ColLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ColLetter = ColLetter & Chr(iRemainder + 64)
End If
End Function
 
Upvote 0
Which is unreliable, for example, try =colletter(53) which should return "BA".

As I mentioned previously, it can be done this way, I have a version that will work beyond the number of columns in excel, but there is more than likely a more practical way to do this depending on the desired output.
 
Upvote 0
This code could be used as Sub or Function, input column number as n output column lettter as nAlpha, works up to the current excel column limit, I didn't see any point in going beyond that

Code:
Dim n As Long, nAlpha As String
If n < 27 Then
    nAlpha = Chr(n + 64)
ElseIf n < 703 Then
    nAlpha = Chr(Int((n - 1) / 26) + 64) & Chr(((n - 1) Mod 26) + 65)
Else
    nAlpha = Chr(Int((n - 1) / (26 ^ 2)) + 64) & Chr(Int((n - 1) / 26) Mod 26 + 64) & Chr(((n - 1) Mod 26) + 65)
End If
 
Upvote 0
This is another way:

n = left(activecell.address(1, 0), instr(1, activecell.address(1, 0), "$") - 1)

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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