sound alert in Excel when cell changes - excel

I use a DDE feed to display a value in a cell. I would like to create an alarm to alert when the value in the cell changes. I am not comparing the cell with any other cell and I don't know whether the value will be higher or lower. I just want to hear a sound when the cell receives a new piece of data from the DDE feed.
Is there anyway to do this.
I know to create an alarm...I just don't know how to trigger it on a single cell changing.
Help appreciated.
Thanks.
S

You can use something like this code (just add it in the sheet module):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 Then
'call your alarm here
End If
End Sub
this code will triggered each time something changed in the sheet. So, you can use IF statement to determine whether only one cell changed and call alarm in this case

Related

How to determine what VBA code is changing the value of an Excel cell

I have a workbook written by someone else and a particular cell is being changed by some code. I cannot figure out the code that is changing the cell value.
I cannot even figure out a strategy to narrow down the code possibilities apart from setting a breakpoint in hundreds of procedures. So I would be happy with strategy ideas.
I am very experienced using VBA in Microsoft Access and a little bit experienced using VBA in Excel.
EDIT
Gotta larf. I cannot even F8 throught the code by starting at the start. What I mean is..
The cell in question changes after I change a value (a year actually) in the worksheet. So I set up a subroutine and breakpoint as below.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub '<== set breakpoint here
Then I press F8 and no other code is entered by the debugger and yet the cell changes.
The problem may be because there is an awful lot of dynamic formula creation going on in the code. Not sure. Don't know enough about Excel quirks.
EDIT 2
There is a missing formula in the cell in question. Sorry to have wasted everyone's time.
So how can a missing formula change a cell's value. It can't!
The question should have been "How to determine why a cell is NOT changing."
So why didn't I ask that?
I did not notice that other cells in the same column as this cell contain a formula. Because the other cells were changing values correctly and this cell was not, I presumed it was some VBA code not working so I tried to track down the rogue code. I guess not being an experienced Excel person I did not rule out the bleeding obvious and went straight to VBA. Phew!
Add a change event handler on the sheet containing the cell you want to track.
In that Event Handler, add code to break when a change in the tracked cell takes place
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Cells(1, 1)) Is Nothing Then
Stop
End If
End Sub
Chnage Me.Cells(1, 1) to cell of your choosing.
When the code breaks, open the Call Stack, to see where the change came from
Here's a proof of concept. I ran ZX. Call stack shows the Change event at the top. The next function is what changed to cell.

Macro "Doesn't Work" From Call on Separate Sheet

This macro works as intended, from a button on the Batch Input sheet:
Sub BatchTriggerOFF()
Sheets("Batch Input").Unprotect
Sheets("Batch Input").Range("G3:J3").Value = "Off"
Sheets("SQL LOGIC").Calculate
Sheets("Batch Input").Range("A12").Select
Sheets("Batch Input").Shapes.Range(Array("Group 12")).ZOrder msoSendToBack
Sheets("Batch Input").Protect
End Sub
However, when BatchTriggerOFF is called from a different sheet in the same workbook, the macro neither changes the Range("G3:J3").Value nor Shapes.Range(Array("Group 12")).ZOrder msoSendToBack. There is no error message.
If Sheets("SQL LOGIC").Range("B1") = "On" Then Call BatchTriggerOFF
I've tried unprotecting the Batch Input sheet beforehand, messing with Sheets("Batch Input").Activate, Sheets("Batch Input").Select, and even tried pasting the BatchTriggerOFF line by line VBA directly into the second macro, to no avail.
What is causing BatchTriggerOFF to seemingly not run when called from the second macro/sheet?
[...] something is inherently wrong with the second code I've provided, likely not actively running when the value in Range("B1") is changed?
Exactly. A procedure that's in a standard module needs something, somewhere to invoke it. Could be a shape or button on the worksheet, could be other VBA code, but something needs to invoke it somehow.
No procedure is going to just know to run when Range("B1") is changed on Sheets("SQL LOGIC"): you need to have code that's "triggered" when a cell is changed on that sheet.
The way to do this, is to handle the worksheet module's Change event. Find your "SQL LOGIC" sheet in the VBE's Project Explorer (Ctrl+R), double-click it. In the code-behind module for that specific worksheet, select Worksheet from the left-side dropdown at the top of the code pane; the right-side dropdown should say SelectionChange, and the VBE should have added a private procedure that looks like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
Select Change from the right-side dropdown; the VBE creates a private procedure that looks like this:
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Now delete the SelectionChange handler, you don't need it unless you need to track cells that the user has selected. Since we want to track cells that have changed, we'll use the Change worksheet event. This procedure will be invoked whenever the user or your code changes anything on that sheet.
Since we only care to run code when a specific cell is changed, we need a condition here, involving the Target parameter. Using the Application.Intersect function, we can get a Range object reference that's Nothing if the two specified ranges don't intersect; we can use this information to bail out if it's not B1 that changed:
If Application.Intersect(Me.Range("B1"), Target) Is Nothing Then Exit Sub
Any code written after that condition inside the Worksheet.Change event handler procedure, will only run after the value of cell B1 was modified - either by the user typing in a value, or by any other code writing to that cell (you need to toggle Application.EnableEvents off if you have to prevent firing that event when it's code doing the changes and you don't want the handler to run).
Now, it looks like cell B1 isn't going to change, rather, it looks like it contains a formula whose result might change after making changes to the "Batch Input" sheet.
If that's the case, then the Change event will not be fired when B1 recalculates and now evaluates to a new value, because the cell didn't change, only its result.
If that's your scenario, then you want to handle the Calculate worksheet event, and have that be your trigger:
Private Sub Worksheet_Calculate()
If Me.Range("B1").Value = "On" Then BatchTriggerOFF
End Sub
If you need your sub to be called from any (sheet) module, move it in a module! The function/sub in the sheet module cannot be called without specifying the module name where it belongs, like you will be able to do in a module.

How to get an excel cell to show the time it was selected

I am trying to create a table that tracks downtime for a production machine. the operator will be using a table with the columns down time start and down time stop. each time something happens that they have to leave the station I want them just to have to click the empty cell under downtime stat title and the time will appear/ log itself in the cell then the same for downtime stop.
I under stand how =now() works but then it shows the time when the file is opened, i want it to display only when the cell is selected.
Any help is much appreciated! I have never had to do anything like this in excel before.
You'll need some VBA for this. In your VBE (alt+f11) go to your worksheet and use something like the following:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Check to see if the click/selected cell is in columns A or B
If Not Intersect(Target, Range("A:B")) Is Nothing Then
'Make sure just one cell is selected:
If Target.Cells.Count = 1 Then
'Update the value
Target.Value = Now()
End If
End If
End Sub
Here we are using the Worksheet_SelectionChange() event. This event will fire any time a selection change is detected on the worksheet in which this code is placed. When the change is detected it will test to see if the selection was in columns A or B. It will also test to insure that only one cell was clicked (otherwise highlighting those columns would cause every row in the column to update with the time, which would be bad). If that all passes, then it just sets the selected cell's (target) value to the current time.

Construct a pair of mirrored cells in two different worksheets

I'm trying to create a pair of cells on two different sheets of my workbook, such that anything you enter into one cell gets copied across to the other. If this was one way, it would obviously be trivial. I want to make it so that the link is two-way, though. That's going to involve VBA scripting, and I'm something of a novice at that. Does anyone have some pointers on how to learn what I need to know? I have Googled, but that just produced some code samples without explanation, and I don't want to put in code that I don't understand.
Let's assume you have Sheet1 and Sheet2 and their names are exactly that.
At Sheet1, go to Developer and click on View Code. You should be looking at a blank coding sheet for Sheet1
On the left drop down of the coding area, select Worksheet instead of the default (General)
On the right drop down. select Change. You would automatically be given the following:
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Changes to any cell in Sheet1 will trigger the code above.
The cell that has been changed is referred to as the Target range.
You can easily identify the address and content of the Target range using Target.Address and Target.Value. You can then use this address and value to update Sheet2.
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.Sheets("Sheet2").Range(Target.Address).Value = Target.Value
End Sub
To copy data from Sheet2 to Sheet1, just repeat the steps above but at Sheet2.
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.Sheets("Sheet1").Range(Target.Address).Value = Target.Value
End Sub
However, if change to Sheet1 will trigger a change to Sheet2, which in turn will trigger a change to Sheet1, the changes will happen for an indefinite time which does not make sense. Therefore, we need to add some code to apply the change only when the values on both Sheet1 and Sheet2 are different. If the values are the same, there is no need to apply change. Therefore, the code for Sheet2 should appear as below:
Private Sub Worksheet_Change(ByVal Target As Range)
If ThisWorkbook.Sheets("Sheet1").Range(Target.Address).Value <> Target.Value Then
ThisWorkbook.Sheets("Sheet1").Range(Target.Address).Value = Target.Value
End If
End Sub
You would use a pair of event macros to accomplish this. Here is an example that you can adapt:
mirror changes

Excel Worksheet_Change not working when macro changes cell

I'm trying to finish up a project of mine and I right now have a Form Control that when pressed adds to the value of a number and another button will subtract that value.
Another value has the two different buttons for the same thing, but the value is also dependent on the first value and other things than just the buttons modify that value. I tried implementing this code for validation
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F19")) Is Nothing Then
If Range("E19") = 2 And Range("F19") < 12 Then
Range("E20") = 1
End If
End If
End Sub
but Excel apparently doesn't recognize that cell F19 has changed when the change is caused by the button, only when it is caused by user input. So, what this is saying is, if F19 updates and 19 is 2 and F19 is less than 12 (the prerequisite for E12 being 2 is F19 being 12 or greater) then set E20 to 1 (E20 is a modifier for E19 which also has other modifiers going into it). This method works on other values that aren't button controlled, but how can I get excel to realize when the Form Control button changes the value (or at least monitor when the form control is pressed.)
Edit: The macro actually doesn't work if the cell changes by formula either. I don't think I can use Worksheet_Calculate to monitor the change in a specific cell.
Why are you doing this with code anyway? You could have Cell E20 have a formula like: =IF(AND(E19=2,F19<12),1,"") which would make the cell blank unless the condition is met.
If you really want to do it with code, you should take this into account: The Worksheet_Change event "Occurs when cells on the worksheet are changed by the user or by an external link."
I would recommend instead of having
If Range("E19") = 2 And Range("F19") < 12 Then
Range("E20") = 1
End If
In your Worksheet_Change event that you add it as a separate sub, that you call from Worksheet_Change. You would also call the sub from the code for your button, after you've performed whatever action your button does. That way, you're guaranteed the check gets run and do not try to rely on events.
Daniel is partialy right, Worksheet_Change "Occurs when cells on the worksheet are changed by the user or by an external link." But this includes changes caused by VBA, and Excludes changes by a formula.
Your problem may be caused (or at least exacerbated) by the only partial qualification of your ranges:
Range("E19") will refer to 'E19' on the active sheet, which may or may not be be the sheets Target is on. You got it right with Target.Worksheet.Range("F19")
Try (note the .'s)
With Target.Worksheet
If Not Intersect(Target, .Range("F19")) Is Nothing Then
If .Range("E19") = 2 And .Range("F19") < 12 Then
.Range("E20") = 1
End If
End If
End With
BTW. I'm with Daniel in that your whole solution seems a little off, but we may not be getting the whole picture...

Resources