simple thing:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, ThisWorkbook.ActiveSheet.Range("C1")) Then
debug.print "value changed"
If IsEmpty(Target.Value2) Then
debug.print "ie empty"
else
debug.print "is not empty"
end if
end if
end sub
I have added the:
debug.print "value changed"
to see if the "delete" key is triggering the Worksheet_Change() and it does NOT, it is triggered only on changing the value (or add if was empty)! But once there is something, and I select the cell and press DELETE on keyboard, nothing happens :(
what is wrong with the code?
Replace:
If Intersect(Target, ThisWorkbook.ActiveSheet.Range("C1")) Then
with:
If Not Intersect(Target, Range("C1")) Is Nothing Then
EDIT#1:
One possible issue is that If expects a Boolean and Intersect() is returning a Range. Excel is trying to interpret the Range and succeeds some of the time.
Related
I am new to vba and I am running into a Type mismatch error when I delete multiple cells in my workbook, and it is failing at the If statement here:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = "Multiple Choice - Multiple Answer" Then
MsgBox "You selected MC-MA"
End If
End Sub
I am wondering if there's a way to check if a user is deleting contents of a cell and NOT run this if statement if that is the case?
I have tried If Trim(Target.Value) <> Empty but it throws the error I'm guessing because Im trying to run an If statement on a value that doesn't exist.
I expect If Trim(Target.Value) <> Empty to make it skip the above code but it throws the Type mismatch error.
Try the SelectionChange Event like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cel As Range
For Each cel In Target
If cel.Value = "Multiple Choice - Multiple Answer" Then
MsgBox "You selected MC-MA"
End If
Next
End Sub
In Change event even if you try and check cells one by one, you won't spot the cell with that match because the code runs after the delete has been executed. So by the time it comes to the If statement, the cell is already blank.
I figured out a way around it. If I just do an If statement that checks if Target.Count = 1 then run the code it works!
Update:
I have removed the original question text as that is resolved, but has lead to a new question.
My code is
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A15")) Is Nothing Then
MsgBox "something changed"
End If
End Sub
This triggers the MsgBox when I click on the cell for the first time, which is great, but subsequent changes or clicks to that cell also triggers, and I'm trying to make it so it only triggers on the first click/first entry of data, subsequent changes or clicks to the cell should do nothing
You have not indicated if this is the only Event macro you are using or if there are others. If this is the only one, then:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A15")) Is Nothing Then
MsgBox "something changed"
Application.EnableEvents = False
End If
End Sub
EDIT#1:
The code above acts like a mousetrap. Once it snaps, it must be reset manually. So after you have completed edits to the event code, run this manually:
Sub ReEnableEvents()
Application.EnableEvents = True
End Sub
If you want a MsgBox to appear the first time you click on a cell, an easy way to do that would be to check if the cell is empty before calling the MsgBox.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A1000")) Is Nothing _
And IsEmpty(Target) Then
MsgBox "something changed"
End If
End Sub
But then what happens if the user clicks on the cell, the MsgBox shows, but the user doesn't enter any value into the cell? Do you want to show the MsgBox again the next time they click on it or not?
If you don't want to show it again, then rather than testing if the cell is empty, I would fill a global range with all the cells that have been clicked so far. Then you would only need to check if the cell is already contained in the range.
Public clickedCells As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' if the selected cell is one of the relevant cells
If Not Intersect(Target, Range("A1:A1000")) Is Nothing Then
' usual procedure if the range of clicked cells is still empty
If clickedCells Is Nothing Then
MsgBox "something changed"
Set clickedCells = Target
Else
' if the range of clicked cells is not empty, then check if selected cell is contained in it
If Intersect(Target, clickedCells) Is Nothing Then
MsgBox "something changed"
' write selected cell into range
Set clickedCells = Union(clickedCells, Target)
End If
End If
End If
End Sub
p.s.: Do you want to show the MsgBox after a value has been typed in a cell for the first time, or at the time of selecting the cell for the first time? Because if it's the former, you should use the 'Change' event instead of 'SelectionChange'.
Private Sub Worksheet_Change(ByVal Target As Range)
I want a msgbox to show up only once when formula result changes in my table range ("K2:K5"). Right now it shows twice.
In this range I have excel-formulas. Formula: H12*10
These formulas is refering to a dropdown-list (a list that I've created from "data validation" on the excel menu Data-tab).
The dropdown-list is located in cell H12.
The values in the dropdown is refering to range(D15:D17)
I've noticed though that the msgbox shows up once when I remove the dropdown and manualy type in values in H12.
Thankful for any help on this
Private Sub Worksheet_Calculate()
Dim Xrg As Range
Set Xrg = Range("K2:K5")
If Not Intersect(Xrg, Range("K2:K5")) Is Nothing Then
MsgBox "Hi"
End If
End Sub
I have also tried to add Application.enableEvents to the code but no success.
Private Sub Worksheet_Calculate()
Dim Xrg As Range
Set Xrg = Range("K2:K5")
Application.EnableEvents = False
If Not Intersect(Xrg, Range("K2:K5")) Is Nothing Then
MsgBox "Hi"
End If
Application.EnableEvents = True
End Sub
Your current code has no added value on a Worksheet_Calculate event due to comparing two exact same ranges and see if they intersect. That would always be the case. Your code would have the same effect as:
Private Sub Worksheet_Calculate()
MsgBox "Hi"
End Sub
Might you have more functions in your worksheet that could trigger this event unwanted outside range K2:K5, you should look into the Worksheet_Change event and return a messagebox when your referenced cell has changed value.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$H$12" Then MsgBox "Hi"
End Sub
Choose either one, but don't use both cause you'll end up with two msgbox's
I want to my macro to send a message just when the second cell have a value. For now the seconde I put something in the first cell my msgBox pop up!
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("G557") <> Range("G555") Then
MsgBox "It has to be equal"
End If
End Sub
Target in that subroutine parameter is the Range (cell) that had the change that triggered the worksheet_change() event. Just test that target to see if it is the cell you want to track. The most common way to do this is using the INTERSECT() function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G555")) Is Nothing Then
If Range("G557") <> Range("G555") Then
MsgBox "It has to be equal"
End If
End If
End Sub
Now the rest of your code will only execute if G555 was changed.
Just add additional condition to your If statement:
If Range("G557") <> Range("G555") And Range("G557") <> "" And Range("G555") <> "" Then
I created a dropdown list like in this instruction. Now this cell needs to be triggered in my macro.
I already read some of the other entries to this topic, but in my VBA excel macro this code doesn't work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
Call Macro1
End If
End Sub
Sub Macro1()
MsgBox "The value entered in Range B2 is " & Range("B2").Value
End Sub
If I change the value of the cell B2, this code doesn't get executed and no messagebox will be displayed.
I copied your code and put it into the sheet that I was editing and it works fine. If you try to put this code somewhere else, e.g. into ThisWorkbook, then it won't work because the eventhandler Worksheet_Change won't fire.
In addition to the above answer, if this is a merged cell and b2 isn't the first cell in the merge, this also won't work. However if that isn't the case and your code is in the right place, it's working fine
If you plan on changing any range values make sure and toggle EnableEvents. If you don't you risk recursively firing Worksheet_Change and crashing your project.
Here is a common design pattern
Turn off EnableEvents
Check if Target (cell that was changed) is in the range that you are validating
Pass Target as a parameter to another sub routine
Turn on EnableEvents
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Macro1 Target
End If
Application.EnableEvents = True
End Sub
Sub Macro1(Target As Range)
MsgBox "The value entered in Range " & Target.Address(True, True) & " is " & Target.value, vbInformation, "You Entered"
End Sub