+ Reply to Thread
Results 1 to 8 of 8

Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

  1. #1
    Registered User
    Join Date
    01-20-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    24

    Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    Hey all,

    I have a fairly simple nested IF function judging a couple of ISBLANKs as well as some LEN and RIGHT. The problem is that I'd like to put it in VBA. Can do that easily. Here's the catch: I'd like to make the column references dynamic in case the columns get re-arranged. So if "Title 1" gets moved to column C, I'd like the formula to use column C. Does that make sense? Here's the formula in a regular cell.

    =IF(ISBLANK(A3),"",IF(ISBLANK(C3),IF(LEN(A3)=13,RIGHT(A3,11),A3),IF(LEN(A3)=13,RIGHT(A3,11)&"-"&C3,A3&"-"&C3)))

    The code is easy, but making it so the column can shift is a bit harder. I was trying to include Match("Title 1",$A$1:$Z$1,0) inside, but that doesn't return letters and it gets too complicated for the macro to record.

    Any help?

    Thank you so much. I appreciate the help.
    Last edited by rmunsun1; 02-17-2011 at 10:25 AM. Reason: Solved thanks to DonkeyOte

  2. #2
    Valued Forum Contributor
    Join Date
    10-13-2010
    Location
    Sunderland, England
    MS-Off Ver
    Excel 2007 (Home) / 2003 (Work)
    Posts
    740

    Re: Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    If the row that "Title 1" would appear on remains static (for instance always would appear on row 3 but the column is variable) you can use

    ADDRESS(3,Match("Title 1",$A$1:$Z$1,0))

    where 3 is the row number. This would bring back the cell reference i.e. $A$3

    Does that help?

  3. #3
    Registered User
    Join Date
    01-20-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    Well the row would remain "static" insomuch as it is the same row that the formula is on. So in VBA, it just an R in an R1C1 formula. So, not R3 or R[-2], but just R. The C part is what I need to be changing.

    So in VBA, how would I do, say, the ISBLANK(ADDRESS(R,Match("Title 1",$A$1:$Z$1,0))?

    Does this question make any sense?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    If you can it makes sense to calculate the positions in the sub routine and load as constants into the formula

    However, if you need the formula to persist in a dynamic sense forever more then use INDEX (ADDRESS warrants Volatile INDIRECT wrapper)

    On that basis:

    Please Login or Register  to view this content.
    In VBA R1C1 form

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-20-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    Quote Originally Posted by DonkeyOte View Post
    If you can it makes sense to calculate the positions in the sub routine and load as constants into the formula
    I like the sounds of this. Are you saying something like:

    ColumnMatch1 = Match("Title 1", $A1,$Z$1,0)?

    How would that fit into the equation I'm looking at?

    IF(ISBLANK(Cells(R,ColumnMatch1))...?

    Something along those lines?

    I'm focusing on the R1C1 situation just so I can leave in an "R" and be able to fill down with the rows updating to the same row as formula.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    Along the lines of:

    Please Login or Register  to view this content.
    use of Sheet1 & D2:D10 purely hypothetical

    also you don't have to use R1C1 you can use A1 (formula) if preferred you just need to ensure you use mixed references correctly (and adjust vMatch approach obviously)

  7. #7
    Registered User
    Join Date
    01-20-2011
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    This is awesome. Thank you so much DonkeyOte. I never can get all the syntax right in regards to "s and &s, not to mention separating out Applications like that.

    A lot to learn.

    Thanks for another lesson. I am very grateful.

  8. #8
    Registered User
    Join Date
    01-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Use Match or Lookup to create dynamic cell reference in VBA R1C1 formula

    Hi,
    Found this post by google and is almost what I need..
    I have this piece of code:
    Please Login or Register  to view this content.
    It does set the hyperlink but when I change the sorting all links are messed up.
    How should the code be changed to be linking to the correct cell?
    Data on rows doesn't change, just are re-arranged by column headers.
    Would be a live saver if I would get this right!
    Thanks
    Last edited by HV_L; 01-20-2012 at 06:27 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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