Results 1 to 5 of 5

Thread: Excel - Return column alphabet from column number

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574

    Excel - Return column alphabet from column number

    Is there some function in Excel (VBA) that will get me the column alphabet if I provide the column number, or do I have to write one. I ask because I know that there is, yet I recall I wrote this functionality for the previous application, and I don't want to:

    (1) Reuse it, if there's something in-built
    (2) Re-invent the wheel

    Help!

    PS: I'm looking for something like:

    Code:
    Function GetAlphabet(ByVal ColumnNumber as Long) as String
    which if I call must say:

    Code:
    GetAlphabet(1)
    Output: "A"

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    No built-in function as far as I know, but here's a function I wrote:
    VB Code:
    1. Function xl_Col(ByRef Col_No) As String
    2. 'returns Excel column name from value
    3. '(eg: col_no 27 returns "AA")
    4.                                        'Only allow valid columns
    5.   If Col_No < 1 Or Col_No > 256 Then Exit Function
    6.  
    7.   If Col_No < 27 Then                  'Single letter
    8.     xl_Col = Chr(Col_No + 64)
    9.   Else                                 'Two letters
    10.     xl_Col = Chr(Int((Col_No - 1) / 26) + 64) & _
    11.              Chr(((Col_No - 1) Mod 26) + 1 + 64)
    12.   End If
    13.  
    14. End Function

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574
    Thank you so much.

  4. #4
    New Member
    Join Date
    Nov 2008
    Posts
    1

    Re: Excel - Return column alphabet from column number

    Another way:
    If you only need to return the letter number of columns A-Z in an Excel 2003 Spreadsheet:
    Use the CHAR() function:

    =char(64+column())

    Let me know if this helps

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel - Return column alphabet from column number

    columns(1).address will return $A:$A
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width