+ Reply to Thread
Results 1 to 6 of 6

Relative formula's by VBA

  1. #1
    Registered User
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    8

    Relative formula's by VBA

    Hi Guys,

    I've got a work related problem I can't seem to solve. I've set up a spreadsheet as a user input form, that copies data to a database. Method:

    Please Login or Register  to view this content.
    What i need is two coloums in the database in order to report on the input data.

    - In column BT I need the formula "=SUMPRODUCT($BV$2:$EH$2;E2:BQ2)", where BV2:EH2 is an absolute reference, but the tricky part (for me) is that I want E2:BQ2 to be relative to the row the data gets placed in.
    - In column BU I need the forumula "=VLOOKUP(MONTH(D2);$BW$5:$BX$16;2;FALSE)&" "&YEAR(D2)", again with relative references to the row that the data gets placed in.

    I'm stuck trying to get the relative references to work. I've tried

    Please Login or Register  to view this content.
    Plus a few variations on this, but I seem to be going nowhere. Can you help me out?
    Last edited by LeBaron; 10-26-2012 at 05:31 AM. Reason: Figured it out

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Relative formula's by VBA

    In youur code change this line

    Target.Offset(0, 72).ActiveCell.FormulaR1C1 = "=SUM(OFFSET(R1C5,COUNTA(C[-72])-1,,,65))"

    By this

    Target.Offset(0, 72).ActiveCell.Formula = "=SUMPRODUCT($BV$2:$EH$2,E2:BQ2)"

  3. #3
    Registered User
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Relative formula's by VBA

    Quote Originally Posted by kvsrinivasamurthy View Post
    In youur code change this line




    By this
    Thanks for the input. When trying this I get an error message: "Application-defined or object defined error".
    Debugging shows this line of code:

    Please Login or Register  to view this content.
    highlighted.

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Relative formula's by VBA

    In your line you have to remove R1C1. It should br like this.

    Target.Offset(0, 72).ActiveCell.Formula = "=SUMPRODUCT($BV$2:$EH$2,E2:BQ2)"

  5. #5
    Registered User
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Relative formula's by VBA

    Quote Originally Posted by kvsrinivasamurthy View Post
    In your line you have to remove R1C1. It should br like this.
    Thanks. I've figured out the formula, using "ActiveCell" messed up the process. Instead:

    Please Login or Register  to view this content.
    gets the formula in there.

    The problem I'm now facing is this: When a new row of data is put into the sheet, the formula is static as "=SUMPRODUCT($BV$2:$EH$2,E2:BQ2)", yet I need it to be relative. So if data is imported to cell A10, I need the formula to refer to E10:BQ10, if data is imported to A20 the range needs to be E20:BQ20 and so on.

    Any advice?

  6. #6
    Registered User
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Relative formula's by VBA

    Got it working! R1C1 helped me out in the end, thanks to kvsrinivasamurthy for pointing me in the right direction with the syntax.

    Final sub:

    Please Login or Register  to view this content.

+ 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