Worksheet_Change doesn't fire up when streaming live data - vba

I have read many posts around this topic, however nothing seems to work for my scenario
I would like to Call Sub upon cell change (B2) which contains live data feed from external source -last updated:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("B2")) Is Nothing Then
Call SubName
End If
End Sub
I went through numerous posts suggesting to check if Äpplication.EnableEvents = True, or create function to detect the change ( which does work, however I cannot call sub within function) - with no success.
Interestnigly enough, when I click on B2 and press enter - it executes the sub
Thanks

Say cell A1 is updated by streaming. The update will not trigger either:
Worksheet_SelectionChange
Worksheet_Change
What you need to do is setup an equation somewhere:
=A1
When A1 is refreshed, the formula cell will re-calculate and you can detect it with the Calculate Event.

you can simply go
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then SubName
End Sub

Related

Automatic execution of Macro based on cell change

I'm trying to run a macro every, time the selection of a dropdown menu in a cell changes. I've tried to follow this solution: automatically execute an Excel macro on a cell change
by entering the following code:
Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then Call Testsub
End Sub
Public Sub Testsub()
MsgBox "Testmessage", , "Testbox"
End Sub
Cell A1 contains the Dropdown menu that should trigger the macro "Testsub". However, nothing happens when I change the selection. I'd be very grateful for any idea, what might cause the problem.
The comment of user11138753 nailed it. I edited the code at the wrong place.
Thank you very much!

Run macro when cell changes pass value

I have this certain code on a worksheet event cell change. It finds certain data that I have on a Worksheet when I type an id_parameter on cell A1.
After data is found, it writes it on this worksheet.
Now I'd like to run a different macro when I change the values on column C of the data.
I'm not able to find the proper procedure. Do_something() receives the value changed in column C. I hope I explained it clearly enough.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
Call Search_data
End If
'Data is written in B2:E10
If Not Intersect(Target, Range("C2:C10")) Is Nothing Then
Call Do_something(Target.Value)
End If
End Sub
The code you have posted works as intended. The issue is most probably due to Search_Data and or Do_Something.
You change the cell A1 which then changes all cells from B2:E10. Including the changed C2:C10 so make sure that the change_event cannot be triggered again by this.
Option Explicit
Public EventsDisabled As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not EventsDisabled Then
EventsDisabled = True
If Not Intersect(Target, Range("A1")) Is Nothing Then
Call search_data
End If
'Data is written in B2:E10
If Not Intersect(Target, Range("C2:C10")) Is Nothing Then
Call do_something(Target.Value)
End If
End If
End Sub
The Public variable EventsDisabled will make sure that the change event runs when a user input is given but not if a Worksheet_Change event changes the worksheet. Otherwise you will run into all sorts of weird loopings which might be infinite or at least take a long time. Your issue will most probably also be related to this. Make sure that your Do_Something sub has the right input type. You should set a few breakpoints and open the local window under:
view>local window here you can see all variables and objects and their type. You can also type:
debug.print typename(Target.value)
into the direct window and then see what type you need to type into the Do_Something signature.

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.

Get notification for all cell changes in VBA

I want to get a notification for all cell updates in excel.
My current code is something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
...
End Sub
My problem is, that it only runs when a direct cell change happens. If I modify a cell, some other cells might change if their values relies on the modified cell.
Is there a way to detect these changes too? I would like to avoid the mirror copy method.
The following code will allow you to access all cells containing formulae which depend upon Target.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
...
On Error Resume Next
For Each cell In Target.Dependents
' Do something
Next cell
On Error GoTo 0
...
End Sub
The On Error Resume Next statement is necessary because the loop will throw an error if there are no dependent cells.
You may also want to call Application.Calculate before that For Each loop in order to force re-calculation of those cells.

Run macro on cell being changed by user

I am trying to make a macro automatically run if any cells in a range is changed (C5 to c25).
As you see in the code below, it should automatically bring up a message box asking the user whether or not to continue (if the user says yes then runs the macro).
I cannot get the code to start running though once I have changed any one of the cells (from c5 to c25).
Here is the code - it is not all of my own:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("C5:C25")) Is Nothing Then Reminder
End Sub
Sub Reminder()
'
' Reminder Macro
'
response = MsgBox("Do you want to set a reminder in Outlook for when the next update is required? If yes, make sure your Microsoft Outlook is open.", vbYesNo)
If response = vbNo Then
MsgBox ("You selected 'No'")
Exit Sub
End If
'Rest of my macro code goes here...
End sub
Thank you!
Ensure your code is in the Worksheet's code module. From the comments above, you indicate it's in Module 3. It needs to be moved to the worksheet's code module.
Try:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("C5:C25"), Target) Is Nothing Then
Call reminder
End If
End Sub
Your code works fine, make sure you put it in the Worksheet object. Also I believe the code won't be active until you save as an xlsm, close, and reopen.