Update:
Sorter version of the VBA function:
Function Number2Char(c As Integer) As String Number2Char = Split(Cells(1, c).Address, "$")(1) End Function
Courtesy of The Plaid Cow.
OR
Function Number2Char(ByVal vNumber)
Dim addr As String
If vNumber 256 Then Exit Function
addr = Range("A1").Offset(0, vNumber - 1).Address
Number2Char = Replace(Replace(addr, "$", ""), Range(addr).Row, "")
End Function
Courtesy of Stan Scott.
Original post:
Converting Excel column number into column name, for example 1 into A or 52 into AZ, is needed occasionally, and the following function will do the convertion for us.
Function Number2Char(ByVal vNumber)
Dim iDiv As Double, iMod As Integer
If vNumber < 1 Then Exit Function
iDiv = vNumber
While iDiv > 26
iMod = iDiv Mod 26
If iMod = 0 Then
iMod = 26
iDiv = iDiv - 1
End If
Number2Char = Chr(64 + iMod) & Number2Char
iDiv = iDiv \ 26
Wend
Number2Char = Chr(64 + iDiv) & Number2Char
End Function
Use this function as formula in the Excel formula bar, ex: =NUMBER2CHAR(52)
FIN.
Save me a couple of hours !!
Thank you for sharing the code.
Andre.
you are welcome andre
This version is a bit shorter:
Function Number2Char(ByVal vNumber) Dim addr As String If vNumber 256 Then Exit Function addr = Range("A1").Offset(0, vNumber - 1).Address Number2Char = Replace(Replace(addr, "$", ""), Range(addr).Row, "") End FunctionWaw, thanks Stan >:D<
Great macro, thanks for sharing
Good job, Stan. I am going to use this with only slight changes. One suggestion – since Excel 2007 supports 16384 columns, I changed the If line to
If vNumber > Cells.Columns.Count Then Exit Function
That makes Excel count the number of columns in the worksheet and then Exits if vNumber is bigger than that.
Thanks Stan Scott your code is very helpfull and shorter than Admin.
And thank Admin for your kick of this topic.
How soon will you update your blog? I’m interested in reading some more information on this issue.
Hai Gary, at least once a week, can be more. What kind of information do need more?
Hi Admin,
I am trying to understand your code above and it fits in perfectly with what I am looking for. But I was wondering if line 13 is incomplete ?
iDiv = iDiv 26
Thanks,
Salma
Salma, there was an error when displaying html code. it should be iDiv \ 26.
Even shorter…
.-= The Plaid Cow´s last blog .. A Strangely Silent Microsoft =-.
Thanks The Plaid Cow
Pingback: Elektrische Zahnbuerste
Pingback: debt consolidation companies
Pingback: what is reverse mortgage
Pingback: debt consolidation companies
Pingback: what is a reverse mortgage