Using VBA code to "wrap" an "If" formula around a large number of existing formulas

Supasage2003

New Member
Joined
Feb 6, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi guys. I'm trying modify a large number of existing formulas such that they are "wrapped" with a new "If" formula. I was hoping to use VBA code to do this.

Maybe I should explain what I'm trying to accomplish first.... I have two nearly-identical worksheets in my Excel workbook, which contains a financial model for a company. One of the worksheets contains the forecasted financial results for the company (let's call this the forecast sheet). The other worksheet contains the company's actual financial results (let's call this the actual sheet). I would like to create a drop-down menu in row 1 of my forecast sheet that gives me two options, "Forecast" and "Actual". If I select "Actual", the values in that column will pull the data from the actual worksheet. If I select "Forecast", the figures shown will be the result of the formulas that forecast the financials.

I already know how to create the drop-down menu, but can't figure out how to use VBA code to modify my formulas. As an example, I originally had this formula in a cell AS63 in my forecast sheet "=AS20+AS27". After manually modifying the formula for the drop-down list, it now reads "=IF(AS$6="E",AS20+AS27,IF(AS$6="A",Actual!AX68,"No Data"))". I would like to "wrap" this "If" formula around the rest of the formulas in the column, but can't figure out how to do this. Any help would be really appreciated!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Why not just put the new formula in the first cell & just fill down?
 
Upvote 0
Why not just put the new formula in the first cell & just fill down?

Hi Fluff. Thanks for the quick reply and the suggestion! Every formula in the column is different (i.e., each row in the column has a different formula), which is why I can't fill it down.

As an aside, I found code that allows bulk modification of formulas (see below). However, that code is set up to add "IFERROR" to all of the formulas. I'm guessing there must be a way to modify this sub to accomplish my objective, but I'm a newbie and haven't been able to figure it out yet.

Sub InsertIFERROR()
Dim R As Range
For Each R In Selection.SpecialCells(xlCellTypeFormulas)
R.Formula = "=IFERROR(" & Mid(R.Formula, 2) & ",""error result"")"
Next R
End Sub
 
Upvote 0
If every single formula is different, then I wouldn't want to risk suggesting anything without knowing exactly what each & every formula was.
 
Upvote 0
Most of the formulas are fairly basic. Either adding, subtracting, multiplying or dividing. Nothing complex. Thanks again!
 
Upvote 0
You could try
VBA Code:
R.Formula = "=IF(AS$6=""E"","&mid(r.formula,2)&",IF(AS$6=""A"",Actual!AX68,""No Data""))"
But make sure you do it on a copy, as it could well mess everything up.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top