VBA If Statement in excel - vba

He all I need help writing an If statement that will run a macro every time the value in cell D8 is over one. No idea where to start

Here you go...
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("D1")) Is Nothing Then Exit Sub
If Not Range("D1").Value > 1 Then Exit Sub
MsgBox "D1 > 0"
End Sub
For more info on the _Change event:
http://msdn.microsoft.com/en-us/library/office/ff839775.aspx

It is a simple if statement.
If Range("D8").Value > 1 Then
'~~> Your code here
End If
You can put that in the Worksheet_Change event. I would also recommend reading this link which talks about Worksheet_Change
If the value in Range("D8") is changing because of a formula then you might have to use the _Calculate event. See this link for an example.

Related

How to Detect if a Cell is Changed by an "=IF()" Formula and not by a User

I read a lot of pages saying that, but none of them put the solution if the value change by an "if function" not by hand.
The code I get is that:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A18:A30")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error GoTo Finalize 'to re-enable the events
MsgBox "You changed THE CELL!"
Finalize:
Application.EnableEvents = True
End Sub
It only works if I change the value by hand.
Thank you in advance.
Another solution; instead of triggering your function every time when your worksheet recalculates, add a function in a module:
Function DetectChange() As Integer
MsgBox "You changed THE CELL!"
DetectChange = 0
End Function
Assuming the outcome of your formula is numeric:(otherwise outcome of function must be a empty string and the "+" must be "&")
Add to your IF-formula at the end ...+Detectchange()
Now there will be a msgbox only when your formula is recalculated
Edit by Darren Bartrup-Cook:
I found this code gave worked when the formula recalculated. It didn't fire if I changed a cell that doesn't affect the cell it's entered to and it didn't fire using Calculate Now or Calculate Sheet.
It did occasionally fire for all formula that I used the function in, but that seemed to be when I was debugging - maybe further investigation needed.
Public Function DetectChange()
MsgBox "You changed cell " & Application.Caller.Address
End Function
e.g.:
=IF(A1=1,A2,A3) & DetectChange() entered in cell A4 displays the message "You changed cell $A$4" if cells A1, A2 or A3 is changed.
Write this in Sheet1 and run the TestMe sub:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1:A30")) Is Nothing Then Exit Sub
Application.EnableEvents = False
On Error GoTo Finalize
MsgBox "You changed THE CELL!"
Finalize:
Application.EnableEvents = True
End Sub
Sub TestMe()
Range("A1") = 34
End Sub
It has worked quite ok on my PC.
If the cell is changed by a built-in Excel function, then the comment of #Vincent G states the correct answer:
Worksheet_Change event occurs when cells on the worksheet are changed by the user or by an external link. and This event does not occur when cells change during a recalculation. Use the Calculate event to trap a sheet recalculation.
If you want to track the calclulation event based on some changes at Range(A18:A30) this is a working solution:
Add a new Worksheet to your Workbook (Sheet2);
In the current Worksheet write the Calculate event:
Private Sub Worksheet_Calculate()
Dim cell As Range
For Each cell In Sheet2.Range("A18:A30")
If cell <> Sheet1.Range(cell.Address) Then
cell = Sheet1.Range(cell.Address)
End If
Next cell
End Sub
In the Sheet2 write an event, catching the changes.
As simple as #Vincent G says.
Private Sub Worksheet_Calculate()
Call YourFunction
End Sub

Worksheet_ Change doesn't work

I have a problem with Worksheet_Change. When I target the cell Excel doesn't react at all even if I put an error in the code. I put the Worksheet_Change on the right sheet, so that's not the reason.
I simplified my code as much as I could and there is still no reaction.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$c$2" Then
MsgBox "hi"
End If
End Sub
I think the event listener that you want to trigger is Worksheet_SelectionChange.
For Worksheet_Change to work, you need to type something in cell C3
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.Range("C2"), Target) Is Nothing Then
MsgBox "Great! Target is within the chosen range!"
End If
End Sub
This works. I always use the above structure.

VBA Excel: General change detection

Is there an object and/or function that can detect or represent a general change on a worksheet? Something similar to (for example) ComboBox1_Change() but could be applied to a whole worksheet. Almost something like Worksheet1_Change(). Any suggestions are welcome. Thanks.
The simplest would be to use the worksheet change event:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "Change Detected!"
End Sub
There are instances will this will throw you in to an infinite loop. Consider this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Range("A1").Value = "Change"
End Sub
Set a breakpoint on the Range("A1") code and make a change somewhere else on the sheet and count how many time that breakpoint gets hit. When you get tired of hitting F5, stop the code and try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Range("A1").Value = "Change"
Application.EnableEvents = True
End Sub
You'll see that the code only fires once. Which is probably what you're after.
It's very important that you have the Application.EnableEvents = True line in there, or else you'll get the appearance that your code isn't working (by default EnableEvents does not revert back to True at the end of the code).

Excel VBA user event

I am working with Excel VBA and I have a macro that is fired everytime a specific cell is changed. That macro affects the value and properties of many cells on the worksheet, so I used
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
[...] 'mycode
Application.EnableEvents = True
End Sub
At the beginning and end of the macro. Otherwise whenever I change the cell that fires the macro, the macro changes cell and thus fires itself over and over again.
I would like for the macro to be fired if I manually change a cell (without having to insert a command button that fires the macro or something similar)
Is that possible?
Thanks for the help
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column =4) Then
MsgBox ("mycell changed")
End If
End Sub
This basically says if the column change occurs in Index 4 (D) then msgbox.
if you didn't have the if statement the messgbox would occur anytime a change occurs to any cell in the worksheet.

How to call a Macro when a named range (cell) is changed

I am trying to call a macro named "RE_environmental" when the cell named "RE_1" is changed (i.e. they mark a X in the cell). I've tried several different variations of codes including these two and nothing is happening:
[The first code does work if I use the exact cell location and not the named cell. --> $E$62]
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "RE_1" Then
Call RE_environmental
End If
End Sub
AND
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("Name").Select = "RE_1" Then
Call RE_environmental
End If
End Sub
--Thanks in Advanced and please let me know if you need more information.
Use your first answer but make this small change:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("RE_1").Address Then
Call RE_environmental
End If
End Sub
Simple syntax mistake!
Edit: To stop RE_environmental from running once the cell is empty, put the code from RE_environmental inside a do-while (not isempty(Range("RE_1"))) as long as RE_environmental is emptying "RE_1". The user won't be able to edit cells while RE_environmental is running.
If Target is always single-cell range, you can use this one:
If Target.Address = Range("RE_1").Address Then
Call RE_environmental
End If
If Target can be multicell range use this one:
If Not Intersect(Target, Range("RE_1")) Is Nothing Then
Call RE_environmental
End If