Excel Formula Flag Triggering - excel

What I'm trying to do in Excel is to figure out a way to trigger a flag after a certain condition has been met.
E.g. I have numbers A and B. A is static while B changes. I also have a Flag f that is initially false. When B>A, f changes to true and stays true regardless of any future value of B.
I was trying to do this with a circular reference, but couldn't get an initial value out of it. Is this possible while only using formulas? I've been trying to avoid using VBA. Any help or point in the right direction would be appreciated.
Thanks

A formula based solution with circular references and allowing iterations will be messy. If you can find someone who can create it in the first place, after a while nobody will remember what it does and how to maintain it.
VBA that writes the value into the cell as soon as the condition is met is a much more reliable and maintainable way.
The code would be along the lines of
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
If Range("F1") = False Then
If Range("B1") > Range("A1") Then Range("F1") = True
End If
End If
End Sub

Related

Excel: if cell value increases -> play sound

I have a cell in excel that increases randomly through the day. This is an API from my trading platform which counts the total number of trades a day.
What I need to do is the following:
Build an IF statement that each time this cell value increases it plays a sound.
I have the sound part covered with a Macro I found online. But the IF statement is giving me trouble.
Anyone could help me out?
You can try using the Calculate() event, depending on how the update occurs this may work for you. This needs to be placed in the worksheet's code module - NOT a standard module.
Option Explicit
Private priorVal As Currency
Private Sub Worksheet_Calculate()
Rem Change this range to be the range that needs to be looked at
If Range("A1") <> priorVal Then
Beep
priorVal = Range("A1")
End If
End Sub

Excel 2013 deleting rows with specific text without leaving blank rows

I'm trying to clarify logs where there are comments in some of the entries and in some there aren't. So what i would like to be able to do is to delete entire rows where there are comments in a column such as NULL and so that it wouldn't leave a blank row behind it because it messes up my other conditional formatting rules.
I'm guessing this kind of automation requires VBA but since i'm not a full time coder, I have no idea how to even begin solving this.
Thank you for all responses in advance!
This isn't a very sophisticated answer, but neither is your question so hopefully it'll get you going. This assumes your evaluating is in column A. You can obviously change that. If you've got tens of thousands of rows, you might want to turn off some functions like screen updating, but that's a different type of question.
Sub ClearThatOut()
Dim rCell As Range
restart:
For Each rCell In Intersect(Columns("A"), ActiveSheet.UsedRange).Cells
If UCase(rCell.Value2) = "NULL" Then
rCell.EntireRow.Delete
GoTo restart
End If
'Example of secondary text, you could put as many of these as needed.
If UCase(rCell.Value2) = "OH NO!" Then
rCell.EntireRow.Delete
GoTo restart
End If
Next rCell
End Sub

Find Address of cell from Index match VBA

The below Code finds the value of a certain cell without any problem. However I am not interested in the value. What I need is the Cell Address. I wish to later in the code use this new found address to adjust it value a few times depending on different factors.
I could use the Address function if this was not in VBA but I have not found a way to use it or anything similar in VBA.
Sub IndexMatch()
A = Application.WorksheetFunction.Index(Workbooks("AllSwipes.xlsx").Worksheets("Backend").Range("H1:CY1"), 1, Application.WorksheetFunction.Match(SomeGlobalVariable, Workbooks("AllSwipes.xlsx").Worksheets("Backend").Range("H1:CY1"), 0)).Offset(1, 0)
MsgBox A
End Sub
Turns out it was as simple as adding .Address to my code Thanks #ScottCraner for pointing me in the right direction.

How to insert a 1 automatically in an excel cell?

I want to be able to put a value of 1 in an excel cell when it is selected. Cells that are not selected remain blank.
ActiveCell will use the current cell that is selected then, as you can see, it assigns the value of 1 to that cell.
ActiveCell.value = 1
If you would like it to be more modular if it is perhaps occurring many times, then consider having a look at change events here, as suggested by #Tom
I really don't recommend you doing this especially if this is for monitoring your stock and so for this time and this time only I've written it to make it a bit more safer if you're actually to do this BUT I DO NOT RECOMMEND IT.
you're going to need to paste this into the worksheet module that it relates to
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Columns.Count = 1 And Target.Rows.Count = 1 And Target.Column = 2 Then
Target.Value = 1
End If
End Sub
To get to the worksheet module you need to go into the vba editor (Shortcut Alt+F11 on windows) and paste in the correct worksheet module (Notice highlighted sheet)
I warn you though this could very quickly lead to your stock levels being inaccurate as this will run whenever you click on a cell or move around a worksheet with the arrow keys. This could mess up your whole system and there will be no undo (vba wipes the undo memory)
What would be much better to do is actually monitor your exact stock levels in excel, then use a formula such as (say you keep your stock level in column c)
=IF(C1>0, B1=1, B1=0)
This would then automatically accurately represent whether it was in stock or not.

Excel is calculating a formula with a VBA function as an error unless it is re-entered

I've got a simple if statement set up in a worksheet where the if condition is VBA user defined function:
Function CellIsFormula(ByRef rng)
CellIsFormula = rng(1).HasFormula
End Function
This function seems to work fine:
But for some reason that I can't figure out, the cell is evaluating to an error. What's worse, is when evaluating the formula, excel is attributing the error to a calculation step that doesn't produce an error:
To top it all off, and what really blows my mind, is that if I simply re-enter the formula, or force a full recalculation (Ctrl+Alt+F9) - the formulas evaluate no problem!
I've tried making the formula volatile by adding Application.Volatile to the function code, but it didn't change anything. Other methods to refresh the calculation, such as setting calculation to manual and then back to automatic, hidding "recalculate sheet", or just using F9 or Ctrl+F9 do not work, only re-entering the formula or Ctrl+Alt+F9 will cause the function to recalculate properly.
Changing one of the cells referenced in the if statement will not fix the problem, but, changing the cell referenced by the "CellIsFormula" function, does fix the problem. Every time the sheet is re-opened though, the error is back.
I discovered the exact problem but I want to up-vote you all for trying to help me figure this out, and give GSerg the credit because, while I wasn't completely out of luck, he was dead on with his suggestion that
Excel does like to make certain properties of a range unavailable during certain stages of calcualtion.
Good find GSerg.
The problem was with Event Handlers. The workbook contains a series of event handlers like Workbook_Open, Worksheet_Change, etc. Every now and then, one of the actions taken by these event handlers will cause some cells in the workbook to recalculate. If excel triggers a recalculation while a macro is running, any cells containing this UDF will result in an error. This is because for some reason, during the VBA triggered recalculation, the .HasFormula property was unavailable, just like #GSerg said:
Presumably - the next bit is an oversight on Excel's part, but once the macro is done running, if a recalculation has been done, resulting in errors because UDFs didn't run properly, excel will not try to run the UDFs again. The resulting error value will be assumed to be the return value of the call, and will not change unless it thinks the parameter to that UDF has changed. Excel will cache the result of the User Defined Function call until the cell its parameter references is changed.
That is why stepping through 'Evaluate Formula' will show everything working until the very last step, where it doesn't actually evaluate the last step, it just shows the value from the spreadsheet as was last calculated.
Solution
There were actually two possible solutions. The first solution I found was to disable automatic calculation at the beginning the Event Handlers, and re-enable it afterwards. For some reason, even though a macro is running at the time calculation is set back to xlCalculationAutomatic, it will cause the UDFs to be successfully re-evaluated, and the property is available.
The second solution, which I prefer because it prevents this from accidentally ever happening again, is to use a different method to check for a formula:
Function CellIsFormula(ByRef rng As Range) As Boolean
CellIsFormula = Left(rng(1).Formula, 1) = "="
End Function
The .Formula property is never unavailable. So this problem never occurs.
I couldn't reproduce this error, but:
The signature should be:
Public Function CellIsFormula2(ByVal rng As Range) As Boolean
CellIsFormula2 = rng.Cells(1).HasFormula
End Function
Excel does like to make certain properties of a range unavailable during certain stages of calcualtion. I've many times seen the .Text property being suddenly unavailable. So if changing the signature does not work, then you are probably out of luck.
I think your problems are because the 'HasFormula' Property returns a variant, not a boolean. If the range has mixed formulas and values, HasFormula will return null. Plus your not defining the rng as a Range object, and not specifying output type. I suggest an approach like this. It can be modified to return a boolean pretty easily.
Public Function CellIsFormula(rng As Range) As String
Application.Volatile
Dim testVal As Variant
testVal = rng.HasFormula 'HasFormula returns variant type
'testval is null if cells are mixed formulas and values
If IsNull(testVal) Then
testVal = "Mixed"
End If
Select Case testVal
Case True
CellIsFormula = "All Cells in Range Have formula"
Case False
CellIsFormula = "No Cells in Range Have formula"
Case "Mixed"
CellIsFormula = "Some Cells in Range Have formula"
Case Else
CellIsFormula = "Error"
End Select
End Function

Resources