Allow two cells to depend on each other - excel

I am trying to write a formula that allows me to calculate a formula that has two variables. I want to be able to enter either variable A and have it calculate B, OR enter variable B and have it calculate A.
Formula to calculate my variables:
Value = NOI / CAP
CAP = NOI / Value
Value is in cell A1
NOI is in cell A2
CAP is in cell A3
NOI is a known value. I want to have separate cells for Value and CAP, and be able to enter data into either cell and have the other cell compute based on my data entry.
I was going to try an IF function with an ISBLANK, but I'm not sure how to do this that won't overwrite the formula.

You can have a formula, or enter a value, but you can't do both. The workaround is to use the Worksheet_change event to monitor each of the two cells and set the other one according to any entered value.
This would go in the worksheet code module:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Goto haveError:
If Target.Address = "$A$2" Then
Application.EnableEvents = False
Me.Range("A3").Value = 'set value in A3
Application.EnableEvents = True
End If
If Target.Address = "$A$3" Then
Application.EnableEvents = False
Me.Range("A2").Value = 'set value in A3
Application.EnableEvents = True
End If
Exit sub
haveError:
'make sure events are re-enabled if an error occurs
Application.EnableEvents = True
End Sub

As an alternate approach you can enter the number in cell A1. In Cell A2 State if it is Cap or Value. In Cell A3 place your NOI. Since the formula is essentially the same for both cases you can simply use the following in A4:
=A3/A1
In B4 I would place the following to indicate which value it was:
=IF(A2="Value", "CAP", IF(A2="CAP","Value", "Enter Type in A2"))
However if you absolutely want data entry in A1 or A2 and then have the other cell get calculated then your only approach is VBA as Tim Williams has pointed out.

There is a way to do this without macros using the ISFORMULA() function, which is available in Excel 2013 or later. The trick is ISFORMULA() can query the state of a cell without actually evaluating it's value, and so can be used to direct a formula to avoid referring to that cell and causing a circular reference.
To wit:
A1 is Value, A2 is NOI (which is given) and A3 is CAP
A1: =IF(NOT(ISFORMULA(A3)), A2 / A3, "")
A2: 123.456
A3: =IF(NOT(ISFORMULA(A1)), A2 / A1, "")
Both A1 and A3 will remain blank until you enter a value in either one of them, at which time the other will be calculated appropriately.

Related

IF statement with 3 variables and show blank

0
Cell A1 can be filled in with whole number values
I want that if Cell A1 is less than 10 then B1 says'<10' I want that if Cell A1 is between 10 and 25 then B1 says'10-25' I want that if Cell A1 is more than 25 then B1 says'>25' however if A1 is blank then B1 should also be blank
I have the formula working to show the correct value however I cannot get B1 to show blank if A1 is blank.
I will be happy to use normal formulas or even VBA for this to work.
Any help is appreciated
How about this formula?
=IFERROR(INDEX({"<10","10-25",">25"},MATCH(A1,{1,10,25})),"")
The above formula will show a blank for numbers <1. If you need to evaluate such numbers as "<10" start the MATCH() array at 0 instead of 1.
Test blank first:
=IF(A1="","",IF(A1<10,"<10",IF(A1>25,">25","10-25")))
for vba you could try this:
Private Sub Worksheet_change(ByVal Target As Range)
Set Target = Application.Intersect(Target, Range("A1"))
If Not Target Is Nothing Then 'if action is not NOT happening in A1, do something...
Select Case Range("A1")
Case "": ActiveSheet.Range("B1").Clear
Case Is < 10: ActiveSheet.Range("B1").Value = "<10"
Case Is < 25: ActiveSheet.Range("B1").Value = "10-25"
Case Else: ActiveSheet.Range("B1").Value = ">25"
End Select
End If
End Sub
Try this -
Cell B1 formula:
=IF(ISBLANK(A1),"",IF(A1<10,"<10",IF(AND(A1>=10,A1<=25),"10-25","")))
To also show "25+" when A1 > 25, use this instead.
=IF(ISBLANK(A1),"",IF(A1<10,"<10",IF(AND(A1>=10,A1<=25),"10-25",IF(A1>25,"25+",""))))

Auto-populate cell, based on another cell's value, without formulae

I've seen a couple of spreadsheets over the years that had a blank, unpopulated, non-formula cell, that would populate when another cell was populated correctly. I am wondering if there is a way to do this without using add-ons, or VBA.
Scenario:
User is asked to enter a value in cell A1.
If the value is X, cell B1 populates with a value.
If the value is Y, cell B1 remains blank.
I know that this can be done with a formula such as =IF(A1="","",IF(A1=1234,"Hello 1234","")).
However, I am wondering if it is possible to do this without a formula in cell B1, but still have cell B1 populated?
From your description, it sounds like this might be what you witnessed. Macros can be set to trigger automatically given a certain event & criteria met. In this instance, the macro will fire when you make a Worksheet_Change in cell A1.
Note that the change to A1 must be manual to fire macro - a change due to formula will not suffice to trigger macro
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target = "X" Then
Range("B1") = "X Result"
ElseIf Target = "Y" Then
Range("B1") = "Y Result"
End If
End If
End Sub

excel copy/paste cell to cell using address in another cell

How to Copy/Paste a value from 1 cell to 2nd Cell using a 3rd Cell which holds the address of the 2nd Cell. I want to trigger the copy/past action by a 4th Cell going 'true'. I do not want to use VB as I am not competent with it.
You will need to use VBA I think. The only way to get a cell to show a value other than by programming is to enter a formula into your 2nd cell - but by definition you don't know what that is. Assume that A1 holds the value to copy, A3 is the cell holding the cell address and A4 is the true/false cell. Then you need this code in the module page of the sheet you wish to affect:
Private Sub Worksheet_Calculate()
If range("a4") then
range(range("a3").text).value = range("a1").value
End If
End Sub
Note this will error if A3 doesn't hold a valid cell address

Excel - using a flag to control cell level calculation

I'm trying to figure out if there is a way to control calculation at a cell level. Here is what I'm basically trying to do in an If statement... I don't know how to "UseTheLastValue" of "DoThisBigLongCalc" though....
Cell A1 =IF(CalculateFlag = True, DoThisBigLongCalc, UseTheLastValueOfBigCalc)
Any help would be appreciated! The reason I'm doing this is I have a lot of formulas that I do want to calculate every time I hit F9, but also a whole lot that I don't because of their long calc time..... Thanks
This approach might help you,
1) Whatever cells you want to do a recalculate with F9, enter the formulas in the excel sheet directly. Even if you set the formula calculate method to manual, the data will be refreshed and recalculated when you press F9
2) For the cells which you do not want to do a recalculate, enter the formulas for those cells through VBA, so that whenever the code is executed, the formulas will be calculated. If not, the old value would still stay there on the cell (UseTheLastValueOfBigCalc)
This code could be triggered through a worksheet_change event which can detect a True/False on a particular cell and execute accordingly. If its true, just recalculate the formulas, if false just ignore.
Below is a code snippet which detects True in cell A1 and calculates accordingly,
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And Range("A1") = True Then
Range("B3") = Application.WorksheetFunction.Sum(Range("B1:B2"))
End If
End Sub
This code sums the values in B1 and B2 and prints in B3 only if True is entered in A1
Even if you change values in cells B1 or B2 the sum will not be recalculated, which means the old value can still be used unless A1 is changes to true.
Once True is entered, the formulas are calculated,
Hope this helps.

Automatically Copying Value & Format - Excel

Automatically Copying one/many cell's value & colour format to another cell/range of cells specified by myself. The other cell also needs to update every time the first cell changes its value or format.
I've been able to see just one or two threads with answers to this question but it still doesn't serve my purpose. Can any genius help me out in here? I don't quite understand why a basic thing like this has taken me 1 full day to figure out (the answer shouldn't be that I'm not smart :D)
Attaching an Excel example of what I want to achieve.
I want to be able to;
Update the Sheet 2 with the data from Sheet 1. (C4 in sheet 2 has to be green and have the value 5). Also, if C4 in Sheet 1 changes its value and colour, I want C4 in Sheet 2 to change automatically)
Now, the above is not only my concern. I have many cells in Sheet 1 which I want to be able to select and have them copied into Sheet 2 in respective places. (eg - c4, d4,e4 from sheet 1 ,.etc to be copied into c4, d4, e4 in sheet 2). Not necessarily I would want to choose the cells in a sequential fashion, but if there's a way for me to specify which cell needs to be copied into which cell of the other sheet, I'll be even more convinced.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ping As Boolean
If Intersect(Target, Range("A3")) Is Nothing Then
If ping = False Then
Range("A3").Copy
Range("C10").PasteSpecial Paste:=xlPasteFormats
End If
ping = True
Exit Sub
Else
ping = False
End If
End Sub
One solution can be with a user defined function. I could not try the code, but just to show the idea:
Public Function CopyFromTo(rngFrom As Range, rngTo As Range)
Application.Volatile True ' "A volatile function must be recalculated whenever calculation occurs in any cells on the worksheet"
rngFrom.Copy rngTo
CopyFromTo = rngFrom ' I am not sure what the function should return
End Function
For example, formula in cell B2 would be =CopyFromTo(A2, B2)

Resources