Call VBA method every time Excel cell is updated - vba

I am working on a trading algorithm in Excel and VBA. A cell in the Excel document gets updated from a trader client with the time remaining. I want to call a method every time that cell changes its value. How is this done?

See the solution on this page:
automatically execute an Excel macro on a cell change
To trigger an event when cell A1 changes,
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A1")) Is Nothing Then Macro
End Sub

Related

How to trigger VBA Workbook_SheetCalculate Event?

I tried Workbook_SheetCalculate Event and tried to trigger it, but it did not work, although I recalculated the worksheet!
How to trigger this Event?
here is an example, in the worksheet for the event have the following code:
Private Sub Worksheet_Calculate()
MsgBox "Calculating"
End Sub
Then in the sheet, in any cell, enter =RAND()
The formula causes a recalculation and triggers the event.
Or from a standard module use the following:
Public Sub Test()
'Application.Calculate ''could use this event for the workbook
With Worksheets("Sheet5") 'sheet containing the event code
.Calculate
End With
End Sub
The key seems to be that there is something in the sheet to calculate e.g. =RAND().
I remembered from another post, at some point, a link to the following Excel’s Smart Recalculation Engine
A quick extract says:
Excel normally only calculates the minimum number of cells possible.
Excel’s smart recalculation engine normally minimises calculation
time by tracking changes and only recalculating
Cells, formulae, values or names that have changed or are flagged as needing recalculation.
Cells dependent on other cells, formulae, names or values that need recalculation.
So, if you just had constants in the sheet, even if you issue a Worksheet.Calculate the msgbox wouldn't appear. You could test this by removing the =RAND() from the sheet and just putting 1 in the cell.
If I have two sheets each with a single non-volatile formula, and this in the workbook module:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Debug.Print Sh.Name
End Sub
I see both sheets names on calling:
Application.CalculateFull
or:
Application.CalculateFullRebuild
but no output with:
Application.Calculate
If I add a volatile formula to one of the sheets then I get that sheet when calling Application.Calculate.
If you're still having problems then you'd need to post a few more details including your event code and what types of formulas you have on your sheets.

Run macro when linked cell changes value (Excel VBA)

I am currently trying to obtain historical information about how the backlog is developing.
My Excel file is based on queries from an Access Database which can give me a view of the current situation.
I would like to automatically run a macro every time the week number changes. I am currently using the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("D3")) Is Nothing Then
Call KPIupdate
End If
End Sub
The macro that should fire is called KPIupdate
My problem is that the Macro only fires if I click the cell. I would like it to just fire when the number changes. The cell "D3" is linked to another cell with the formula =Weeknum(Today();21)
I hope you can help me
According to the MSDN entry for Worksheet_Change:
This event does not occur when cells change during a recalculation. Use the Calculate event to trap a sheet recalculation.
To use Worksheet_Calculate to trap the change in a cell that is set by a formula looking at another cell, you need to set a variable to hold the value of the 'Target' and then check if it has changed after the Calculate event fires.
Here is a simple example:
Option Explicit
Private strCurrentWeek As String
Private Sub Worksheet_Calculate()
If Me.Range("A1").Value <> strCurrentWeek Then
'the linked cell changed
Debug.Print "Sheet1!A1 was changed"
'call another macro
End If
'update the new current week
strCurrentWeek = Me.Range("A1").Value
End Sub
To test this, just set the formula in A1 to be =B1 and then change the value of B1 and check the output in the Immediate window.
You can adapt this code to call KPIupdate where my Debug.Print... statement is.

Running a code in Excel VBA whenever a cell with RTD-function is updated

I use VBA in MS Excel 2007 and I have cells with RTD-function that are updated every minute. My code is running just fine, but despite searching for an answer, I have not figured out how to make the code loop whenever new data are introduced in the RTD cells. Is there any simple answer to this question?
In Excel VBA there is an event called Worksheet_Change. This event is fired when a cell value is changed. I think this event is what you need.
Private Sub Worksheet_Change(ByVal Target As Range)
'write your code here (Target is the cell)
End Sub

Change a date in EXCEL based on changes to other cells

I am trying to automate the updating of a status date in an Excel Worksheet based on if any changes have taken place within certain cells. In my example, I want the date in cell "S6" to equal today's date is any of the data in cells "B6:L34" have been changed/ deleted/ info added. I am not sure what VBA code to use or how. Any clues? This would be for only changes within those cells on that worksheet; not changes throughout the Workbook. Thank you.
I would look into workbook/worksheet events throughout the internet. You can create private sub functions that automatically runs whenever a change is made. It sounds like you need a private sub worksheet_selection or worksheet_change event handler. Look up something simple like "workbook_open VBA excel" and you can get A great feeler for how event handlers work.
Then I suggest you to research the INTERSECT function and play around with it. It allows you to declare a TARGET variable and range for you to manipulate, basically saying that if anything happens to that range, this is how I want to manipulate the TARGET. The TARGET is basically the cell that was being manipulated within the range.
There might be other factors - but this will start you off very quickly
This code will update the cell S6 with today's date when anything inside the range B6:L34 has been edited.
Private Sub Worksheet_Change(ByVal Target As range)
If Not Intersect(Target, Target.Worksheet.range("B6:L34")) Is Nothing Then
Sheets("Sheet1").Range("S6") = Date
End If
End Sub
EDIT:
For this to work, make sure this code is placed within the sheet that you're using (see below):

VBA function to alert when value is changed within a time frame

I have an excel cell where its value is updated every minute.
What I need is an alert message box; while that cell value moves to a particular time.
Suppose a cell has value 10:
if it reaches 7 in one minute then I need a message box to alert that.
if it is not reaching 7 in one minute then I don't need any alert.
Please help write me a macro for this.
Inside VBA editor for ThisWorkBook, you could write the following code
Dim WithEvents SheetToWatch As Worksheet
Private Sub SheetToWatch_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
If Target.Value = 7 Then
MsgBox "changed to 7"
End If
End If
End Sub
Private Sub Workbook_Open()
Set SheetToWatch = Sheets("Sheet1")
End Sub
Basically the code sets the reference of Sheet1 to the variable SheetToWatch when the workbook opens. Declaring the variable using WithEvents lets one capture the events.
I am using Change event of the worksheet and checking if the cell that was changed is A2. If so, I am checking or comparing the value with 7 (it can be dynamic and depends on how you want to handle it).
See if this helps.
Cannot be done with a function, any way that I can think of.
If you control the mechanism that is updating the cell, then you could it call a VBA subroutine that you wrote, instead, have to send the alert and then update the cell from that routine.
If you do not control the updating mechanism, then the only thing that I can think of that might work is of the Cell, Range or Worksheet classes have a ChangedDate event that you could catch from VBA and do your alerting from there.