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
Related
Good afternoon,
I have cells the first has a date and the second has a date, and the third checks the first two. If the first cell has a value it returns "OPEN", if the second cell has a value it instead returns "WAITING",
eg. Cell F4 "=IF(D4=0,"",IF(AND(D4>0,E4=0),"OPEN","WAITING"))"
and then I put a date in that cell replacing the formula.
Then another cell checks the whole column and if it contains "OPEN" or "WAITING" it's value is "OPEN", if not it's value is "CLOSED",
eg. Cell M2 "=IF(SUM(COUNTIF(F2:F1000,{"OPEN","WAITING"})),"OPEN","CLOSED")"
Then I have VBA on the tab colour, which changes to either green if that cell is "CLOSED" or red if it's "OPEN".
eg.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M$2" Then
Select Case Target.Value
Case "OPEN"
Me.Tab.Color = vbRed
Case "CLOSED"
Me.Tab.Color = vbGreen
Case Else
Me.Tab.Color = vbOrange
End Select
End If
End Sub
This all works except for the tab colour. I have to open the M2 cell (which has calculated correctly) and press enter to activate the tab colour change.
The cells that are checking previous cells are not always column F, some are different. The final value is always M2 in every sheet.
The reason I tried to do this was so I wouldn't need to change the tab colour manually. Is there a better way to do this? I'm currently achieving no automation, if anything it's slightly more work.
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.
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.
I have a cell whose value is constantly changing through a feed. I want to develop a macro which when activated records the cell value at that instant and pastes it in another cell. Tried finding some formula to do that but no success.
You won't be able to do that with a formula. However, you can write a macro that checks if your cell was changed:
Private Sub Worksheet_Change(ByVal Target As Range)
...
End Sub
If you don't want this type of automation, you could simply add a button to your worksheet which triggers a macro when clicked and does what you are looking for.
To copy the value of a cell into another one, you could use this simple solution:
Range("B1").Value = Range("A1").Value
This will copy the value of cell A1 into cell B1.
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)