I'm trying to create a VBA script that fires when a value within a cell range (in this case E7:E10) is selected (from a dropdown). However, Excel does not seem to give any indication of the macro firing, and I feel it's due to to the header lines. Here is the header line code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("E7:E10")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Is there a way to be able to tell if it's running or not?
Is there a way to be able to tell if it's running or not?
Put a breakpoint or a MsgBox call inside the procedure:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
End If
If no message box pops up, then the event didn't fire. Check the value of Application.EnableEvents and also ensure macros are enabled.
Note that the Change event doesn't fire as a result of recalculation.
Try the below code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "dropdown cell location" Then
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
End If
'your macro
If Target.Address = "dropdown cell location" Then
With Application
.Calculation =xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End If
End Sub
Related
I have the following code that runs a goalseek if the defined named range "N" changes. However, I want the code to run if any of several cells changes. E.g. "N1", "N2", etc..
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Application.EnableEvents = False
If Target.Address = Range("N").Address Then
'Goalseek for force equilibrium
Range("Delta_F").GoalSeek Goal:=0, ChangingCell:=Range("h_neutral")
End If
Application.EnableEvents = True
End Sub
I tried the following, but it did not work:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Application.EnableEvents = False
If Target.Address = Range("N, N1, N2").Address Then
'Goalseek for force equilibrium
Range("Delta_F").GoalSeek Goal:=0, ChangingCell:=Range("h_neutral")
End If
Application.EnableEvents = True
End Sub
Any help is appreciated. Thanks in advance.
Testing your criteria as a Range is easier than testing the Address as a string.
Building on #Michal's solution, the below will only execute when your changed cell (Target) overlaps (Intersects) with your 3 ranges (Set as the variable TargetRange here). The main difference is the double negative in the test statement which allows you to avoid Exit Sub resulting in moderately cleaner code.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TargetRange As Range
Set TargetRange = Union(Range("B3"), Range("G19"), Range("N1"))
If Not Intersect(TargetRange, Target) Is Nothing Then
Application.EnableEvents = False
Range("Delta_F").GoalSeek Goal:=0, ChangingCell:=Range("h_neutral")
Application.EnableEvents = True
End If
End Sub
Your condition can be easily checked with Intersect function:
If Intersect(Range("N"), Target) Is Nothing Then Exit Sub
If Target doesn't lie within your range, it will exit the sub (this condition should be placed as the first command in your Sub).
I have a checkbox that changes the color of a cell if it is checked. The user can't print the page until a value is entered into the highlighted field. Both the checkbox and restrict printing modules are working properly.
How do I change the color of the highlighted field once the user enters a value? I wrote the below code, but it only works when I manually run it. I am newer to VBA so any help is appreciated!
Private Sub Can_Print()
If IsEmpty(Range("G54")) = False Then
MsgBox "You may now print."
Range("G54").Interior.Color = RGB(221, 235, 247)
End If
End Sub
Put this code under the worksheet you want it run for:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value <> "" Then
MsgBox "You may now print"
Target.Interior.Color = RGB(221, 235, 247)
End If
End Sub
if you don't want it to work for any cell then just replace target with the range you want.
In case you haven't got it yet, please check this . You need to copy your code onto the worksheet events. You need to attach your code to the worksheet change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("G5:G6")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Application.EnableEvents = False
If IsEmpty(Target) = False Then
'MsgBox "You may now print."
Target.Interior.Color = RGB(22, 235, 247)
Else
Target.Interior.ColorIndex = 0
End If
Application.EnableEvents = True
End If
End Sub
I have an Excel workbook with multiple worksheets. I have a cell in WORKSHEET A with range name TRACK1 and a cell in WORKSHEET B with range name TRACK2.
Each TRACK1 and TRACK2 are validated from a list. The user can change either cell from the drop-down list shown when the cell is selected.
I want to be able to allow the user to change either and have the other be also changed to match. Change value of TRACK1 and TRACK2 is changed, and vice versa.
I know how to do this basic macro, but how to stop the event propagating?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("TRACK1")) Is Nothing Then
Range("TRACK2") = Range("TRACK1")
End If
If Not Application.Intersect(Target, Range("TRACK2")) Is Nothing Then
Range("TRACK1") = Range("TRACK2")
End If
End Sub
In worksheet A's code module, use:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("TRACK1")) Is Nothing Then
Worksheets("WORKSHEET B").Range("TRACK2") = Range("TRACK1")
End If
Application.EnableEvents = True
End Sub
In worksheet B's code module, use:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("TRACK2")) Is Nothing Then
Worksheets("WORKSHEET A").Range("TRACK1") = Range("TRACK2")
End If
Application.EnableEvents = True
End Sub
I have a password protected sheet, with some unlocked cells that user can chage.
Once the user changes any value, it automatically should make changes to other unlocked cells by vba code. This works fine if the sheet is unlocked, but not if it's protected.
example of code:
In Workbook_Open() I set UserInterfaceOnly attribute to TRUE:
Sheets("Sheet Name").Protect Password:="123456", UserInterFaceOnly:=True, Contents:=True
Sheet code: Set date.01 value into date.02 cell if date.01 changes
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("date.01")) Is Nothing Then
Worksheets("Sheet Name").Range("date.02") = Target
End If
End Sub
cells "date.01" and "date.02" are unlocked.
Why can't I update them?
EDIT:
Is SelectionChange event the best option to change cell values? And is it ok to do the assignment like this:
Worksheets("Sheet Name").Range("date.02") = Target
I can see that the changes are applieD when the original cell get the focus back.
What I really want to do is to give a group of cells in different sheets the same value anytime any of them are changed by the user.
SOLVED.
My bad, I was using
Worksheet_SelectionChange
instead of
Worksheet_Change
I also had to use this to prevent any errors.
Application.EnableEvents = False
<CODE>
Application.EnableEvents = True
There was no need of using UserInterfaceOnly as all cells/ranges are unlocked.
you can simply check it's something to do with protection.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("date.01")) Is Nothing Then
Sheets("Sheet Name").unProtect Password:="123456"
`Worksheets("Sheet Name").Range("date.02") = Target`
`Sheets("Sheet Name").Protect Password:="123456", UserInterFaceOnly:=True`
End if
End Sub
Do you apply validation through VBA
The event method to use is Worksheet.Change Event
https://msdn.microsoft.com/en-us/library/office/ff839775.aspx
And the code was like:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("date.01")) Is Nothing Then
Application.EnableEvents = False
Worksheets("Sheet1").Range("date.01") = Target
Worksheets("Sheet2").Range("date.02") = Target
Worksheets("Sheet3").Range("date.03") = Target
Worksheets("Sheet4").Range("date.04") = Target
Worksheets("Sheet5").Range("date.05") = Target
Application.EnableEvents = True
End If
End Sub
I'm relatively new but learning quickly. I have a cell (Sheet8.Range("E15")) that produces TRUE or FALSE (boolean) based on other conditions. My goal is that when this cell = TRUE, the button is active. If cell value changes to FALSE, I want the button to be inactive. Thanks for the help!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Sheet8.Range("E15")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
If Sheet8.Range("E15").Value = True Then CommandButton1.Enabled = True
If Sheet8.Range("E15").Value = False Then CommandButton1.Enabled = False
End If
End Sub
The issue here looks to be that the cell value isn't actually changing, which is not triggering the macro.
If you change the macro from a Worksheet_Change to a Selection_Change, this macro will be triggered after every user action. This should solve your issue.
Use the KeyCells.Value instead of the sheet and change the Button name to something more description.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Sheet8.Range("E15")
If KeyCells.Value = TRUE Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub