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
Related
Suppose I am having two sheets where on sheet 1 is this kind of data (different length of information within one row):
It might be lot of text that is "wrapped" to fit in a row (decently)
But, when I try to reference the same text, or try to use some formula for instance INDEX/MATCH to get me the same text, I am failing to get proper ROW Height...so I must adjust manually sheet 2 rows to fit nicely from heights...
Is there a way in Excel to automatically make these row expansions? Without taking a manual action every time because I won't be always sure how long it will be my original text...
You could add something into the Worksheet_Change event
Private Sub Worksheet_Change(ByVal Target As Range)
Target.WrapText = True
Target.EntireRow.AutoFit
End Sub
It might get annoying if it runs after every change for every cell, so you should add more if statements to limit which cells trigger the code.
I'm new to using VBA, I understand how to autofill a predefined number of columns/rows, however, I'd like the user to be able to input a number into a cell, and starting with a static column/row, the code autofills however many columns the user wants(in this instance how many pay periods an employee would work.) This is what I have so far.
Option Explicit
Sub AutoFill_Test()
'A1 here being where the total number of payperiods are found
Range("A1").AutoFill Destination:=Range("A2:D2"), Type:=xlFillCopy
End Sub
How would I modify this to achieve the desired result?
many thanks,
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
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.
Respected Sir/Madam
I want to key 60 records in first sheet, and 61th records key on next sheet. I want after
60 records cursur is go to automaticully 61th records.
means after every 60th records curuser is going to next sheet.
Thankyou
From your question I think you need to use the SelectionChange event to detect when a cell on any row >60 is selected.
Private Sub Worksheet_SelectionChange(ByVal target As Range)
If target.Row > 60 Then
Sheets(ActiveSheet.Index + 1).Activate
ActiveSheet.Range("A1").Activate
End If
End Sub
This code would need to be placed in each worksheet code module that you wanted the automatic jump to the next worksheet to happen in. It makes the following assumptions:
It fires any time a cell with row number >60 is selected
It just increments the worksheet index to get the next sheet
It assumes your data starts in cell A1
This should allow you to get started though.
With some presto code I would say the logic would be something like this
If ActiveSheet=”Sheet1” AND ActiveRow>=61 then
ActiveSheet=”Sheet2”
End if
I’m sorry that I’m not at a computer with office installed so cant generate the actual code but I think you see where I’m going with this