+ Reply to Thread
Results 1 to 3 of 3

Use Header value instead of column number or column value in cell/range referencing?

  1. #1
    Registered User
    Join Date
    03-27-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    56

    Use Header value instead of column number or column value in cell/range referencing?

    I have data in four columns
    Prob1: I need to have a reference of last row in column 3
    Below can be used
    lRow = .Cells(.Rows.Count, 3).End(xlUp).Row

    But i would like to remove the hardcoding and use the value of header in column 3 i.e. "H3" in attached sheet.

    Prob2: while setting a range i used columns while i would like to replace it by the header name. i.e. "H2" in attached sheet
    Set rngData = .Range("B2:B" & lRow)


    Could someone please help with above 2 referencings so that whereever in the table the two columns appear, it will still refer them correctly? Thanks in advance.
    Attached Files Attached Files

  2. #2
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,359

    Re: Use Header value instead of column number or column value in cell/range referencing?

    Maybe something like:
    Please Login or Register  to view this content.
    If things don't change they stay the same

  3. #3
    Registered User
    Join Date
    03-27-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Use Header value instead of column number or column value in cell/range referencing?

    this worked for problem 1 for column C

    How to use these in setting Range? (as mentioned for prob 2 using column B):

    Prob2: while setting a range i used columns while i would like to replace it by the header name. i.e. "H2" in attached sheet
    Set rngData = .Range("B2:B" & lRow)


    solution:
    Sub Maybe()
    Dim rng As Range
    Set rng = Cells(2, Rows(1).Find("H2").Column).Resize(Cells(Rows.Count, Rows(1).Find("H2").Column).End(xlUp).Row - 1)
    rng.Select
    End Sub


    Thank you for solution for first part.
    Last edited by Sum; 06-28-2022 at 01:18 AM.

+ 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. Referencing a Column Header that contains '#' in its title
    By Larry Stevens in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2020, 08:42 PM
  2. [SOLVED] HELP: Column Header as a Variable(Structure Referencing)
    By nori79 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-08-2015, 04:43 AM
  3. macro to lock cell by column header based on value in another cell by column header
    By Closet Guru in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2015, 12:36 PM
  4. Replies: 4
    Last Post: 12-19-2014, 05:38 PM
  5. Replies: 1
    Last Post: 10-05-2013, 03:35 PM
  6. Replies: 10
    Last Post: 07-12-2010, 08:55 AM
  7. Referencing column header instead of column label
    By mremmenga in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2008, 01:12 PM

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