SlideShare a Scribd company logo
1 of 5
Download to read offline
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...
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...
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...
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...
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...

More Related Content

Similar to Convert column-number-to-letter

Number systems and Logic gates
Number systems and Logic gatesNumber systems and Logic gates
Number systems and Logic gatesRaviHN
 
designanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptxdesignanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptxarifimad15
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview QuestionsGradeup
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanationsGopi Saiteja
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming hccit
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Getachew Ganfur
 
Admissions in India 2015
Admissions in India 2015Admissions in India 2015
Admissions in India 2015Edhole.com
 
Ggplot2 work
Ggplot2 workGgplot2 work
Ggplot2 workARUN DN
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questionsGradeup
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn osalisha230390
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameAntony Stubbs
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 

Similar to Convert column-number-to-letter (18)

Number systems and Logic gates
Number systems and Logic gatesNumber systems and Logic gates
Number systems and Logic gates
 
Ch03-Computer Security
Ch03-Computer SecurityCh03-Computer Security
Ch03-Computer Security
 
designanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptxdesignanalysisalgorithm_unit-v-part2.pptx
designanalysisalgorithm_unit-v-part2.pptx
 
Grids implementation
Grids implementationGrids implementation
Grids implementation
 
MATLAB
MATLABMATLAB
MATLAB
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01
 
Admissions in India 2015
Admissions in India 2015Admissions in India 2015
Admissions in India 2015
 
Mit6 006 f11_quiz1
Mit6 006 f11_quiz1Mit6 006 f11_quiz1
Mit6 006 f11_quiz1
 
Ggplot2 work
Ggplot2 workGgplot2 work
Ggplot2 work
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 
Lecft3data
Lecft3dataLecft3data
Lecft3data
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
5 octave tutorial
5 octave tutorial5 octave tutorial
5 octave tutorial
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 

Recently uploaded (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
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...