+ Reply to Thread
Results 1 to 6 of 6

Insert dynamic formulas into cells

Hybrid View

adriano.r.marques Insert dynamic formulas into... 07-03-2015, 02:25 AM
millz Re: Insert dynamic formulas... 07-03-2015, 02:37 AM
adriano.r.marques Re: Insert dynamic formulas... 07-03-2015, 09:58 AM
millz Re: Insert dynamic formulas... 07-05-2015, 09:47 PM
FDibbins Re: Insert dynamic formulas... 07-05-2015, 09:51 PM
adriano.r.marques Re: Insert dynamic formulas... 07-05-2015, 09:58 PM
  1. #1
    Registered User
    Join Date
    10-07-2014
    Location
    Salvador da Bahia, Brazil
    MS-Off Ver
    2013
    Posts
    39

    Insert dynamic formulas into cells

    Hi guys.

    I am trying to write a VBA code that would insert a formula in a cell if another cell meets a condition. E.g. If R3 = "Academic" Then M3 = "n/a". Otherwise, M3 assumes the following formula:
    Formula: copy to clipboard
    =IFERROR(IF(OR(K3=I3, AND(K3<I3,K3+L3=I3),OR(ISBLANK(K3:L3),L3=0), K3+L3>I3), 0, I3-K3-L3), "")


    Currently I managed to get this working with the following code:

    If Target.Column = 18 Then
        Select Case Target.Value
        Case "Academic"
            Target.Offset(, -5) = "n/a"
            Target.Offset(, -5).HorizontalAlignment = xlCenter
    Case Else
            Target.Offset(, -5).Formula = "=IFERROR(IF(OR(K3=I3, AND(K3<I3,K3+L3=I3),OR(ISBLANK(K3:L3),L3=0), K3+L3>I3), 0, I3-K3-L3), """")"
    End Select
    End If
    So what happens is that cell, say, M10 is using the formula based on row 3 instead of updating the formula to the current row, which is 10 in this case.

    What's the best way of doing it?

    I appreciate any help.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Insert dynamic formulas into cells

    Try:
    Target.Offset(, -5).FormulaR1C1 = "=IFERROR(IF(OR(RC11=RC9, AND(RC11<RC9,RC11+RC12=RC9),OR(ISBLANK(RC11:RC12),RC12=0), RC11+RC12>RC9), 0, RC9-RC11-RC12), """")"
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    10-07-2014
    Location
    Salvador da Bahia, Brazil
    MS-Off Ver
    2013
    Posts
    39

    Re: Insert dynamic formulas into cells

    Jesus Christ it works!

    millz what is this formula? How did it happen?

    Thank you so very much!!

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Insert dynamic formulas into cells

    Sorry didn't have time to respond. The formula is basically the same as yours, except in R1C1 format.

    R = Row
    C = Column
    R3C11 = Row 3, Column 11 (K)
    RC11 = Column 11 (K), same row as where the formula is inserted

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Insert dynamic formulas into cells

    Why the need to do this with VBA?

    A recular formula in that cell, copied down, shoud do the same thing...
    =If(R3="Academic","n/a",IFERROR(IF(OR(K3=I3, AND(K3<I3,K3+L3=I3),OR(ISBLANK(K3:L3),L3=0), K3+L3>I3), 0, I3-K3-L3), ""))
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  6. #6
    Registered User
    Join Date
    10-07-2014
    Location
    Salvador da Bahia, Brazil
    MS-Off Ver
    2013
    Posts
    39

    Re: Insert dynamic formulas into cells

    Hi FDibbins. Thanks for your reply.
    And millz, thank you for your clarification.

    I needed a VBA code because I wanted to prevent users from either selecting or even deleting the cell's content. Since protecting sheet isn't a viable option for this matter I thought that a VBA code would do the trick. And it did well.

    I'm very happy with where I am getting with your help mates. Thanks.

+ 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. Insert cells, fill formulas down, set formulas for dynamic range
    By Snickers65 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2013, 01:43 PM
  2. Locate blank cells, insert dynamic value
    By rynofrowan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-14-2013, 07:30 AM
  3. [SOLVED] Dynamic ranges not working with cells that has formulas
    By taibe in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-24-2013, 05:21 AM
  4. Dynamic Ranges with Formulas in cells
    By realniceguy5000 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-15-2011, 04:27 PM
  5. formulas with dynamic references to cells
    By excelnerd1 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-06-2009, 06:44 PM

Tags for this Thread

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