Get notification for all cell changes in VBA - 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.

Related

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 VBA when cell changes on a different worksheet

I am very new at VBA, but have managed to make a code which links the header of my worksheet to a cell on another sheet.
The code runs every time I click a cell in the active worksheet. I'd like for the code to only run when a certain cell in the other worksheet changes.
"Report" has the header
"Input" has the cells which the code refers to (B18) and the cell I want the code to run on when changed (B3).
The following is the code that I already have.
Private Sub Worksheet_selectionChange(ByVal Target As Range)
ActiveSheet.PageSetup.RightHeader = "&28" & Format(Worksheets("Input").Range("b18").Text)
End Sub
Any help is much appreciated!!!
Not sure about your second paragraph; my understanding is that you want to monitor cell B3 on the Input worksheet, and when it changes, adjust the RightHeader of the PageSetup of worksheet Report so it reflects what's in cell B18 on the Input worksheet. If that's correct, you could put the following code behind the Input worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("B3")) Is Nothing Then
ThisWorkbook.Worksheets("Report").PageSetup.RightHeader = "&28" & Me.Range("B18").Text
End If
End Sub
...and remove your Worksheet_SelectionChange procedure.
The idea is to monitor for changes on the Input worksheet, and act upon those involving its B3 cell.
Ideally, you'd give names to the cells above, so that your code would continue working even if you add or delete rows and columns on the Input worksheet. See Define and use names in formulas. If you were to define Inputs!B3 as MyMonitoredValue and Inputs!B18 as MyPageSetupValue, the code above would become:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("MyMonitoredValue")) Is Nothing Then
ThisWorkbook.Worksheets("Report").PageSetup.RightHeader = "&28" & Me.Range("MyPageSetupValue").Text
End If
End Sub
...and you could re-organize the layout of the Input worksheet without worries.
Finally, instead of referring to worksheets by their tab's name (as seen from Excel), you can refer to them directly using their CodeName, as seen from the VBA editor, in the Properties window, under the worksheet's (Name) property (press Ctrl+R to show the Project Explorer, click on the worksheet under the Microsoft Excel Objects node, then press F4 to display the Properties window). You can change this value; I'd typically change it to shtReport. Yet another version of the code would then be:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("MyMonitoredValue")) Is Nothing Then
shtReport.PageSetup.RightHeader = "&28" & Me.Range("MyPageSetupValue").Text
End If
End Sub
...which is able to withstand both changes in the Report worksheet's tab name, and reorganization of the Input worksheet.

Set the value of a cell when unsaved changes are detected in Excel using VBA

I'm trying to detect changes within an entire workbook (doesn't matter what changes) so that any change that occurs prior to save will result a specific cell getting set to a specific value. The goal is to show a visual indicator of whether the workbook is saved using conditional formatting (non issue, already have that set up).
The code I use to set the value of the cell is Sheets("Sheet1").Range("H1").Value = 1.
I've tried to detect changes by using Sub Worksheet_Change(ByVal Target As Range), but I'm unsure of how to set the range.
Any help would be greatly appreciated.
If I'm reading your question correctly, you want to setup your worksheet so that every time a change is made by a user, update the value in the cell.
You are already most of the way there, you just need to put the following subroutine in your workbook.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Sheets("Sheet1").Range("H1").Value = 1
End Sub
Once this is saved with your workbook, that specific workbook will perform the functionality you require. Here's a link to the documentation - http://msdn.microsoft.com/en-us/library/office/ff839775(v=office.15).aspx.
Private Sub Worksheet_Change(ByVal Target As Excel.Range) 'Target is the variable name of the Range where the Change occured
If thisworkbook.saved=false then
Sheets("Sheet1").Range("H1").Value = 1
else
Sheets("Sheet1").Range("H1").Value = 0 'or other value
End if
End Sub

How do i insert a new blank cell before current cell that has just been populated

I have a two (very long) TO-DO lists- one going across and the other going down.
What i want to achieve is for a blank cell to appear at the start of the list instead of having to scroll to the end of the lists to enter a new item.
So then when i have entered an item in a cell and hit enter, i want the cell just populated to move down the list (or across if i hit tab) and a new empty cell to appear at the start of the list.
It would be useful for the new blank cell to be pre-populated with the current date but that is not essential.
Thanks for your help.
NOT FOR POINTS.
Piggy-backing on Gary's answer, the mistake is that you set A to Range("C4:C6"). What happens is, when you enter data into any of C4, C5, and C6, they are all moved to the right because of A.Insert, which refers to all the cells assigned to A.
The trick here is to fully qualify your requirements for Target. Let's say you have a table from B1:E3, like below:
Now, let's say you want to move row 1 if you enter something into A1, row 2 if A2, etc. The following macro should do it (notice the difference with Gary's macro):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim QualifyingRange As Range
'Dim OrigRng As String
Set QualifyingRange = Range("A1:A3")
If Intersect(Target, QualifyingRange) Is Nothing Then Exit Sub
Application.EnableEvents = False
'OrigRng = Target.Address
Target.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Range(OrigRng).Value = Date
Application.EnableEvents = True
End Sub
What is the difference in the above? Very simple but very important. When a Worksheet_Change is in a sheet's code, every time you do a valid change to the sheet, the macro fires. The range you just edited will be known to the macro as Target. Now, usually, if you don't declare what the qualifications for Target are, the Worksheet_Change macro just fires indiscriminately. How do we qualify Target properly then?
We use Intersect. First, we declare a range of cells that we want to track. These cells, when changed, should fire the macro. Otherwise, macro is kaput. This line: If Intersect(Target, QualifyingRange) Is Nothing Then Exit Sub basically reads: If Target is not inside my desired range, then nothing happens.
This is the reason why I declared A1:A3 as my QualifyingRange. This way, if my change is to any of the cells above, the macro will fire. HOWEVER, .Insert should not be applied to the whole range but to Target alone. This is because if we do QualifyingRange.Insert, every time a change is detected in any cells in A1:A3, all three rows will move. This is what happened when you set A to three cells and kept A.Insert.
Hopefully, this clears up the confusion. Let us know if this helps.
Here is a partial solution. The following event macro monitors entry to cell A1 . Once you have entered a value in A1, the macro "pushed" the values in column A down by one. This means that value you just entered has been pushed down to A2 and A1 is empty:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
Set A = Range("A1")
If Intersect(A, Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
A.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Application.EnableEvents = True
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
EDIT#1
To push across rather than down:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
Set A = Range("A1")
If Intersect(A, Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
A.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Application.EnableEvents = True
End Sub
To handle multiple cells, you must specify which cells get pushed across and which cells get pushed down.

Circle Invalid-data Macro

I have a worksheet with many dependant dropdowns that are liable to having invalid data if the initial drop down is altered. I would like a simple auto-macro that would automatically run the "circle invalid data" command every time a cell change within a specified range (D8:T800) is detected.
It sounds fairly straightforward but I am not sure how to do this.
Question - as this macro will run every single time a cell is amended would this macro slow down the worksheet?
EDIT:
Also: as this might be slow,is there a way we can run this command over a selected range?
Your thoughts thanks.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("D8:T800")) Is Nothing Then
Me.CircleInvalid
End If
End Sub
Note that CircleInvalid applies to the whole sheet, so while this code only triggers when a cell inside D8:T800 changes, all invalid cells on the sheet will be circled
Try placing this code inside your Worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
' Check target range has changed
If (Not Intersect(Target, Range("D8:T800")) Is Nothing) Then
' Prevent recusrive looping
Application.EnableEvents = False
' Refresh validation circles
Target.Worksheet.CircleInvalid
Application.EnableEvents = True
End If
End Sub
Note that this will NOT work if the values in those cells change due to a calculation from outside the specified range.
Also, the CircleInvalid method applies to the whole worksheet.
You could try editing the code in the conditional to 'do something' if the Target is validated — this would result in you changing the format of invalid cells instead of having the red circles around them.
**PSEUDO-CODE**
For each cell in Target.Range
cell.colour = bright red
Next cell