Results 1 to 3 of 3

Thread: Getting column names in Excel 2000

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Resolved Getting column names in Excel 2000

    How can I get the letter column names in Excel in code? I want to apply a formula (SUM) to a range of cells in a row, but I can't use the Cells property because that takes integers, so instead of B8 i get 28.
    I don't know all the columns in advance (varies by client), so I can't hardcode it. I can think of awkward ways -adding 65 & taking the ASCII value, creating an array of letters, etc, but it seems like there ought to be a better way - column.name or something. Thanks.
    Last edited by salvelinus; Jan 12th, 2005 at 03:17 PM. Reason: Resolved
    Tengo mas preguntas que contestas

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Getting column names in Excel 2000

    no simple way, but here's a function you can use:
    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
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Getting column names in Excel 2000

    Thanks. You'd think the letter name would be accessible through a property, but oh well.
    Tengo mas preguntas que contestas

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