Convert Excel Column Number into Column Name

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.

This entry was posted in Workbook and Worksheet and tagged , , , , , , , , , , , . Bookmark the permalink.

18 Responses to Convert Excel Column Number into Column Name

  1. Andre Eugenio says:

    Save me a couple of hours !!

    Thank you for sharing the code.

    Andre.

  2. admin says:

    you are welcome andre :)

  3. Stan Scott says:

    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 Function
    
  4. Poer says:

    Waw, thanks Stan >:D<

  5. zung says:

    Great macro, thanks for sharing

  6. diskedit says:

    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.

  7. hwsris says:

    Thanks Stan Scott your code is very helpfull and shorter than Admin.

    And thank Admin for your kick of this topic.

  8. GarykPatton says:

    How soon will you update your blog? I’m interested in reading some more information on this issue.

  9. Hai Gary, at least once a week, can be more. What kind of information do need more?

  10. salma says:

    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

  11. Poer says:

    Salma, there was an error when displaying html code. it should be iDiv \ 26.

  12. Even shorter…

    Function Number2Char(c As Integer) As String
      Number2Char = Split(Cells(1, c).Address, "$")(1)
    End Function
    

    .-= The Plaid Cow´s last blog .. A Strangely Silent Microsoft =-.

  13. Poer says:

    Thanks The Plaid Cow :D

  14. Pingback: Elektrische Zahnbuerste

  15. Pingback: debt consolidation companies

  16. Pingback: what is reverse mortgage

  17. Pingback: debt consolidation companies

  18. Pingback: what is a reverse mortgage

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>