+ Reply to Thread
Results 1 to 5 of 5

Cell reference in VBA code with Excel Functions

Hybrid View

  1. #1
    Insomniac53
    Guest

    Cell reference in VBA code with Excel Functions

    I don't use Excel that often, and VBA even less often, so I suspect I'm missing some simple concept. I have this line of code in a macro:
    ActiveCell.Offset(0, 7).Formula = "=VLOOKUP($C21,Lists!$C$1:$E$99,3,FALSE)"

    It works, except that it doesn't update the $C21 to $C22, $C23 etc. when called to create new rows. I can see that this is probably because the VLOOKUP arguments are quotes rather than dynamic cell references, but I'm not sure how to get around it. Any ideas?

    Thanks.
    Last edited by Insomniac53; 07-21-2011 at 02:59 AM.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Cell reference in VBA code with Excel Functions

    I use R1C1 notation when putting formulas in cells via VBA. Try

    With ActiveCell.Offset(0,7)
        .FormulaR1C1 = "=VLOOKUP(" & .Parent.Range("C7").Address(false, true, xlR1C1, False, .Cells) & ", R1C3:R99C5, 3, False)"
    End With
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Insomniac53
    Guest

    Re: Cell reference in VBA code with Excel Functions

    Thanks Mikerickson.

    I'm going to have to study what you've sent me and get back tomorrow. It's advanced VBA code, at least for me, so I need to work out what it's doing. If there are any simpler solutions out there, I'd be interested to see them.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Cell reference in VBA code with Excel Functions

    Once you understand R1C1 notation (in some senses, its easier than A1 notation), the tricky part is the RelativeTo argument of the .Address property.

  5. #5
    Insomniac53
    Guest

    Smile Re: Cell reference in VBA code with Excel Functions

    Using the R1C! you suggested, I did this, which seems to work:

    ActiveCell.Offset(0, 7).FormulaR1C1 = "=VLOOKUP(RC3,Lists!R1C3:R99C5,3,FALSE)"

    I've looked up some of the elements you used in your answer, like ".address" but can't find good explanations. Anyway, you pointed me in the right direction so much appreciated and rated. Thanks.

+ 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