The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
Convert column-number-to-letter
1. sign up log in tour help stack overflow careers
Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
VBA function to convert column number to letter?
Does anyone have an function which can return the column letter(s) from a
number?
excel-vba
For example, entering should return .100 CV
excel excel-vba excel-formula
edited Apr 23 at 7:17
brettdj
31k 11 46 86
asked Oct 9 '12 at 9:33
mezamorphic
2,728 15 55 95
3 –Check this question out: stackoverflow.com/questions/10106465/… Francis Dean Oct 9 '12 at 11:14
–@FrancisDean that is the reverse of this question which is looking for the address from the number brettdj
Mar 29 at 2:19
1 –@brettdj The answer linked shows both number to letter and letter to number. Francis Dean Mar 30 at
9:43
–@FrancisDean fair point, I looked at the question title in the link to rather than the accepted answer
brettdj Mar 30 at 10:15
16 Answers
Something like this to return the letter for column 100
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
test the code
Sub Test()
MsgBox Col_Letter(100)
End Sub
edited Dec 23 '14 at 2:16 answered Oct 9 '12 at 9:44
brettdj
31k 11 46 86
–
You can add the to the end of the Split command if you want to save yourself a variable declaration
and extra line of code. eg
(0)
Col_letter = Split(Cells(1, lngCol).Address(True, False), "$")(0)
Caltor Feb 18 at 12:21
–That is quite correct, but I thought it more readable to use several lines. brettdj Feb 18 at 14:30
–Yeah it's a trade off I guess. My version looks sort of Pythonic hehe. Caltor Feb 18 at 16:08
If you'd rather not use a range object:
Function ColumnLetter(ColumnNumber As Long) As String
Dim n As Long
Dim c As Byte
Dim s As String
n = ColumnNumber
Do
c = ((n - 1) Mod 26)
Page 1 of 5excel - VBA function to convert column number to letter? - Stack Overflow
31/05/2015http://stackoverflow.com/questions/12796973/vba-function-to-convert-column-numbe...
2. s = Chr(c + 65) & s
n = (n - c) 26
Loop While n > 0
ColumnLetter = s
End Function
edited Aug 15 '14 at 18:22 answered Mar 12 '13 at 16:37
robartsd
443 4 8
–Excellent!, great code! MiBol Aug 30 '13 at 14:22
1
–
Not clear why you posted a longer method with a loop on the basis of If you'd rather not use a range object:
brettdj Feb 7 '14 at 23:46
2
–
@brettdj I can imagine several reasons: 1) this method is around 6x faster by my testing 2) it doesn't require
access to the Excel API 3) it presumably has a smaller memory footprint. EDIT: Also, I'm not sure why I
commented on an answer over a year old :S Blackhawk May 23 '14 at 17:05
1 –@blackhawk, fair point re the speed. -1 removed. brettdj May 24 '14 at 3:02
1
–
There's a drawback to the increased speed, though. Using the range object throws an error if you pass in an
invalid column number. It works even if someone is still using Excel 2003. If you need that kind of exception,
go with the range method. Otherwise, kudos to robartsd. Engineer Toast Feb 17 at 22:10
Something that works for me is:
Cells(Row,Column).Address
This will return the $AE$1 format reference for you.
answered Nov 21 '13 at 21:00
Damian Fennelly
141 1 2
–This is fantastic! I'm going to turn it into a function. BrettFromLA Mar 20 '14 at 18:41
And a solution using recursion:
Function ColumnNumberToLetter(iCol As Long) As String
Dim lAlpha As Long
Dim lRemainder As Long
If iCol <= 26 Then
ColumnNumberToLetter = Chr(iCol + 64)
Else
lRemainder = iCol Mod 26
lAlpha = Int(iCol / 26)
If lRemainder = 0 Then
lRemainder = 26
lAlpha = lAlpha - 1
End If
ColumnNumberToLetter = ColumnNumberToLetter(lAlpha) & Chr(lRemainder + 64)
End If
End Function
answered Nov 27 '13 at 10:31
Nikolay Ivanov
1,151 11 14
–Cut-and-paste perfect to convert numbers greater than 676. Thanks! David Krider Jul 25 '14 at 14:50
–The remainder can never be more than 26 so why not an integer rather than long? Caltor Feb 18 at 12:01
The function below is provided by Microsoft:
Function ConvertToLetter(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
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function
Source: How to convert Excel column numbers into alphabetical characters
Page 2 of 5excel - VBA function to convert column number to letter? - Stack Overflow
31/05/2015http://stackoverflow.com/questions/12796973/vba-function-to-convert-column-numbe...
3. APPLIES TO
• Microsoft Office Excel 2007
• Microsoft Excel 2002 Standard Edition
• Microsoft Excel 2000 Standard Edition
• Microsoft Excel 97 Standard Edition
edited Jul 31 '14 at 18:34 answered Jul 20 '14 at 14:46
mtbink.com
61 1 5
1 –For reference, this pukes with larger column sets as Chr() doesn't handle large numbers well. Azuvector
Oct 18 '14 at 0:39
–@Azuvector will this work for values less than 100? vignesh Apr 28 at 5:55
Just one more way to do this. brettdj's answer made me think of this, but if you use this method
you don't have to use a variant array, you can go directly to a string.
ColLtr = Cells(1, ColNum).Address(True, False)
ColLtr = Replace(ColLtr, "$1", "")
or can make it a little more compact with this
ColLtr = Replace(Cells(1, ColNum).Address(True, False), "$1", "")
Notice this does depend on you referencing row 1 in the cells object.
edited May 23 '14 at 16:01 answered May 23 '14 at 15:22
OSUZorba
86 1 6
robertsd's code is elegant, yet to make it future-proof, change the declaration of n to type
long
In case you want a formula to avoid macro's, here is something that works up to column
702 inclusive
=IF(A1>26,CHAR(INT((A1-1)/26)+64),"")&CHAR(MOD(A1-1,26)+65)
where A1 is the cell containing the column number to be converted to letters.
edited Feb 17 '14 at 3:46
Undo
11.9k 11 46 74
answered Feb 17 '14 at 3:29
Jan Wijninckx
141 1 3
There is a very simple way using Excel power: Use property,
this way:
Range.Cells.Address
strCol = Cells(1, lngRow).Address(xlRowRelative, xlColRelative)
This will return the address of the desired column on row 1. Take it of the :1
strCol = Left(strCol, len(strCol) - 1)
Note that it so fast and powerful that you can return column addresses that even
exists!
Substitute for the desired column number using
property!
lngRow Selection.Column
edited Jul 29 '14 at 12:47
Niall
6,777 7 23 48
answered Jul 29 '14 at 12:39
flaviomorgado
21 1
This will work regardless of what column inside your one code line for cell thats located in row
X, in column Y:
Mid(Cells(X,Y).Address, 2, instr(2,Cells(X,Y).Address,"$")-2)
If you have a cell with unique defined name "Cellname":
Mid(Cells(1,val(range("Cellname").Column)).Address, 2, instr(2,Cells(1,val(range
("Cellname").Column)).Address,"$")-2)
Page 3 of 5excel - VBA function to convert column number to letter? - Stack Overflow
31/05/2015http://stackoverflow.com/questions/12796973/vba-function-to-convert-column-numbe...
4. answered Nov 5 '14 at 17:30
Codeplayer
21 1
This is a version of robartsd's (with the flavor of Jan Wijninckx's one line solution), using
recursion instead of a loop.
Public Function ColumnLetter(Column As Integer) As String
If Column < 1 Then Exit Function
ColumnLetter = ColumnLetter(Int((Column - 1) / 26)) & Chr(((Column - 1) Mod 26) + Asc
("A"))
End Function
I've tested this with the following inputs:
1 => "A"
26 => "Z"
27 => "AA"
51 => "AY"
702 => "ZZ"
703 => "AAA"
-1 => ""
-234=> ""
edited Feb 4 at 16:23 answered Feb 4 at 16:18
alexanderbird
61 4
–
I've just noticed that this is essentially the same as Nikolay Ivanov's solution, which makes mine a little less
novel. I'll leave it up because it shows a slightly different approach for a few of the minutia alexanderbird
Feb 4 at 19:56
This is a function based on @DamienFennelly's answer above. If you give me a thumbs up,
give him a thumbs up too! :P
Function outColLetterFromNumber(iCol as Integer) as String
sAddr = Cells(1, iCol).Address
aSplit = Split(sAddr, "$")
outColLetterFromNumber = aSplit(1)
End Function
answered Mar 20 '14 at 18:43
BrettFromLA
801 1 11
1 –Good one, but how is it different from the accepted answer? Ioannis May 23 '14 at 15:34
–
@loannis I based mine on DamianFennelly's answer, not the accepted one. But yeah, mine looks a lot like
the accepted answer, except one line is broken into two to make it more readable. BrettFromLA May 23
'14 at 17:11
Here is a simple one liner that can be used.
ColumnLetter = Mid(Cells(Row, LastColA).Address, 2, 1)
It will only work for a 1 letter column designation, but it is nice for simple cases. If you need it to
work for exclusively 2 letter designations, then you could use the following:
ColumnLetter = Mid(Cells(Row, LastColA).Address, 2, 2)
answered Aug 26 '14 at 14:15
Syd B
11 1
Easy way to get the column name
Sub column()
cell=cells(1,1)
column = Replace(cell.Address(False, False), cell.Row, "")
msgbox column
End Sub
I hope it helps =)
answered Nov 11 '14 at 12:09
Page 4 of 5excel - VBA function to convert column number to letter? - Stack Overflow
31/05/2015http://stackoverflow.com/questions/12796973/vba-function-to-convert-column-numbe...
5. cristobal
11 1
This is available through using a formula:
=SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")
and so also can be written as a VBA function as requested:
Function ColName(colNum As Integer) As String
ColName = Split(Worksheets(1).Cells(1, colNum).Address, "$")(1)
End Function
answered Dec 9 '14 at 12:08
Alistair Collins
1,210 3 15 30
The solution from brettdj works fantastically, but if you are coming across this as a potential
solution for the same reason I was, I thought that I would offer my alternative solution.
The problem I was having was scrolling to a specific column based on the output of a MATCH
() function. Instead of converting the column number to its column letter parallel, I chose to
temporarily toggle the reference style from A1 to R1C1. This way I could just scroll to the
column number without having to muck with a VBA function. To easily toggle between the two
reference styles, you can use this VBA code:
Sub toggle_reference_style()
If Application.ReferenceStyle = xlR1C1 Then
Application.ReferenceStyle = xlA1
Else
Application.ReferenceStyle = xlR1C1
End If
End Sub
answered Feb 12 at 18:07
Will Ediger
413 3 8
Page 5 of 5excel - VBA function to convert column number to letter? - Stack Overflow
31/05/2015http://stackoverflow.com/questions/12796973/vba-function-to-convert-column-numbe...