+ Reply to Thread
Results 1 to 7 of 7

Reference Column by Header Name

  1. #1
    Registered User
    Join Date
    12-02-2018
    Location
    Nova Scotia, Canada
    MS-Off Ver
    Office365
    Posts
    9

    Reference Column by Header Name

    I have a table "Index" in a worksheet and I would like to reference the column by the header name, "Client Tag", not "l". Tryin to avoid having to change my code when a job requires me to insert a column.

    Currently, my code looks like;
    Please Login or Register  to view this content.

  2. #2
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Reference Column by Header Name

    Use a named range for your column, such as "ClientTag"

    Then you can use Range("ClientTag")(aRow,1).Value ....

    (To create a named range highlight your column and type your name in the top left box...)
    Last edited by Croweater; 07-03-2020 at 10:36 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Reference Column by Header Name

    Maybe :
    Please Login or Register  to view this content.
    I found the code in this link
    Last edited by karmapala; 07-05-2020 at 04:40 AM.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Reference Column by Header Name

    Hi Jarubell,

    @Croweater's suggestion to use 'Named Ranges' works well in theory but, 'Named Ranges' in my opinion are more trouble than they are worth when:
    a. Trying to remember the name
    b. When copying Sheets to a New Workbook, I often have to delete the 'Named Ranges' and recreate them. When I do use 'Named Ranges', I always create two routines in VBA: one to create them, and one to delete them.

    I prefer to use constants declared in a code module I call ModConstantsAndGlobals. This way:
    a. I know where to find the value
    b. When a row or column changes, it is necessary to modify the values, but they are all in one place.

    For example:
    Please Login or Register  to view this content.
    When using the item in the code I prefer to use .Cells instead of .Range:
    Please Login or Register  to view this content.
    Lewis

  5. #5
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Reference Column by Header Name

    Yes, all well and good, but then, when you insert a column in your worksheet (like the OP wants) you have to change the column id in your code.

    Easy enough if you know what to do but if he is writing a spreadsheet for his sales manager say, who knows nothing about VBA.....

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Reference Column by Header Name

    when you insert a column in your worksheet (like the OP wants) you have to change the column id in your code.
    I can live with that limitation, which has served me well over the years.

    ----------------------------------

    See the attached file containing the following code excerpts which uses a Scripting Dictionary to dynamically identify input and output columns.
    Please Login or Register  to view this content.
    Lewis

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Reference Column by Header Name

    But if you're dealing with a Table, you can refer to the columns by name (as karmapala showed earlier) so it's all moot, no?
    Rory

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reference Column by specific Text in the Header row
    By snuffnchess in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-26-2020, 06:35 PM
  2. Replies: 3
    Last Post: 07-17-2017, 07:19 AM
  3. Dynamic reference to Table Column header
    By aiyathomas in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-08-2017, 10:30 PM
  4. Structured Reference formula changing Column header
    By immortalrebirth in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-06-2015, 06:46 AM
  5. [SOLVED] Reference in a row with value to return the column header
    By rpinxt in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-01-2014, 08:05 AM
  6. Replies: 6
    Last Post: 05-25-2010, 08:44 AM
  7. Excel Cell Reference and Column Header
    By santhu123 in forum Excel General
    Replies: 3
    Last Post: 04-21-2009, 03:51 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1