+ Reply to Thread
Results 1 to 4 of 4

Replace formula in range of cells (with/without if then statement)

  1. #1
    Registered User
    Join Date
    06-28-2016
    Location
    X
    MS-Off Ver
    X
    Posts
    7

    Replace formula in range of cells (with/without if then statement)

    Hi,

    I just started out with VBA so I am new to all of it. So my question is how can I replace a formula in a column range.

    So I want to change the formula's in the cells M4:M20. The current formula in M4 is really simple; =SUM(C4:E4;G4:H4;J4:K4) now I want to replace this formula with another one namely; =SUM(C4:F4;H4:I4). Can someone show me the syntax for this macro? I know I can change it manually but I would like to learn how to build this in VBA so I can use it as a macro.

    And if you are awesome with VBA I would like to know how to use it in an if then syntax like below. So if F2 has a value of Actual Q3 then the old formula is changed into the new one.

    Private Sub CommandButton1_Click()

    If Range("F2").Value = "Actual Q3" Then
    Range("G:G, H:H").Replace ?????[don't know if this is possible]
    End If

    End Sub


    Thanks in advance ,

    Bart
    Attached Images Attached Images

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: Replace formula in range of cells (with/without if then statement)

    This macro inserts the formula in m4 according to the value in f2. if the range M4:m20 needs to be changed then it can be embedded into a loop as shown in the second part. Only one of the sections is needed.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-28-2016
    Location
    X
    MS-Off Ver
    X
    Posts
    7

    Re: Replace formula in range of cells (with/without if then statement)

    Awesome dude works like a charm. Just needed to add the End If in the second section but that was easy to figure out!!

    Tnx

  4. #4
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: Replace formula in range of cells (with/without if then statement)

    Glad you caught my mistake. Happy to have been able to help. Please mark your thread as SOLVED

+ 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. [SOLVED] Add IF Statement formula to cells in varying range and paste to used range
    By Webbers in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-26-2015, 01:28 PM
  2. Replies: 0
    Last Post: 06-27-2013, 08:38 AM
  3. statement / formula to return text from a range of cells
    By nellyc in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-30-2013, 07:55 AM
  4. Using VBA on a named range to replace select statement
    By Tosca1978 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-20-2012, 05:24 AM
  5. Formula: Drop Down List -> Choose Option Finds Cells & Replace Cells with Cells
    By g00glethis1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2010, 01:10 PM
  6. Replace range of cells with a formula
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2010, 07:48 PM
  7. [SOLVED] How can I replace a range of blank cells with a 0
    By Replacing blank cells in forum Excel General
    Replies: 1
    Last Post: 08-18-2005, 08:05 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