Run macro on change within target range | Change (ByVal Target As Range) - excel

I'm working with the Worksheet_Change(ByVal Target As Range) event.
I wanted to run a macro if either on of cells A1 or A2 are changed.
So the target range is set to [A1, A2]
Then, if A1 is changed, I want to clear A2, or the other way around (A2 is changed: clear A1).
Now the problem:
If A1 value is changed. the macro clears A2. That's seen as a change in A2, so it clears A1, which is a change, etc...
I'm sure it's something simple, but I can't see how I can have cell A1 be changed without triggering the change macro if A2 is being cleared.
Anyone who has experience in this?

You can accomplish this by toggling events off and on, like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1:A2")) Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
If Target.Address = "$A$1" Then
Me.Range("A2").Clear
ElseIf Target.Address = "$A$2" Then
Me.Range("A1").Clear
End If
SafeExit:
Application.EnableEvents = True
End Sub

There are a few edge cases here, around changing more than one cell eg via copy pasting a range (possibly include other cells too).
Here's a version thats more robust in those cases
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Me.Range("A1:A2"))
If rng Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
Select Case rng.Address(0, 0)
' If both changed, what now? Default to clearing A2
Case "A1", "A1:A2"
If Not IsEmpty(Me.Cells(1, 1)) Then
Me.Cells(2, 1).Clear
End If
Case "A2"
If Not IsEmpty(Me.Cells(2, 1)) Then
Me.Cells(1, 1).Clear
End If
End Select
SafeExit:
Application.EnableEvents = True
End Sub

Related

run macro to an entire column

working on this macro on a dependent drop down menu
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M$10" Then
Range("O10").Value = "--select--"
End If
End Sub
I need to run this macro for all the cells in the column. It just work in the first cell
Can anyone help me please?
thanks!
You would need to use Application.Intersect in combination with .Offset() instead of the Target.Address method.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Application.Intersect(Target, Me.Range("B:B")) 'find all cells that were changed in column B
If AffectedRange Is Nothing Then Exit Sub 'exit if nothing in column B was changed
Application.EnableEvents = False 'make sure our value change doesn't trigger another Worksheet_Change event (endless loop)
On Error GoTo ENABLE_EVENTS 'make sure events get enabled even if an error occurs
Dim Cell As Range
For Each Cell In AffectedRange.Cells 'loop through all changed cells in column B
Cell.Offset(ColumnOffset:=1).Value = "" 'move from B one column to the right and reset value
Next Cell
ENABLE_EVENTS: 'in case of error enable events and report the error
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub
This will observe column B and delete the value in C whenever a cell in B was changed.

Clear cell content based on another cell for each row

How to clear a cell based on a change of other cell but for every row separately?
Say, I change AI5 and it clears AQ5, I change AI17 it clears AQ17?
I've got this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("AI5")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("AQ5").ClearContents
Application.EnableEvents = True
End Sub
And it works only for the cells in the code.
If I change both to a range of the column, it clears all column AQ, which is not what I need.
Thanks
Like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("AI" & Target.Row)) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("AQ" & Target.Row).ClearContents
Application.EnableEvents = True
End Sub
You need to tell excel which row, and the row will be the same as the Target.Row so you can use that.

VBA getting the column number from the Target in a worksheet change

I have a table of values that I need to fill out through a worksheet change function.
What I am trying to do is change a cell in columns B-G, depending on where the target is.
Private Sub Worksheet_Change(ByVal Target As Range)
If (Not Intersect(Target, Range(Cells(12, 2), Cells(14, 7))) Is Nothing) Then
Cells(16,Application.WorksheetFunction.Column(Target))="Hello"
End If
End Sub
I have similar bits of code in the same worksheet_change sub that work fine when I use Target.Offset(1,0) but since my possible target range is in more than 1 Row, I don't know how to make it so that it is always row 16 and the same column as the target....
You need to deal with situations where Target is more than a single cell and disable event handling so when you change a value on the worksheet, the Worksheet_Change doesn't try to run on top of itself.
This will put 'hello' into the cell immediately to the right of any cell within B:G that changes; essentially you would be adding 'hello' to columns C:H on the associated row of each cell in Target.
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target, Range(Cells(12, "B"), Cells(14, "G"))) is nothing then
on error goto safe_exit
application.enableevents = false
dim t as range
for each t in intersect(target, Range(Cells(12, "B"), Cells(14, "G")))
t.Offset(1,0) = "hello"
next t
End If
safe_exit:
application.enableevents = true
End Sub

How do I write VBA code that will allow me to jump from cell to cell as I hit the enter key

I have to do a lot of repetitive and I'm trying to automate things as rough as possible. Let say I want to do the following:
Enter an integer in cell F2 hit enter and have the cursor jump to cell F9
Enter an integer in cell F9 hit enter and have the cursor jump to cell F15
Enter an integer in cell F15 hit enter and have the cursor jump to cell F21
I created the following code in the WorkSheet Module:
Dim OldAddress As String ____________________________________________________________
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If OldAddress = Range("F2").Address Then
Range("F9").Select
ElseIf OldAddress = Range("F9").Address Then
Range("F15").Select
ElseIf OldAddress = Range("F15").Address Then
Range("F21").Select
Else
OldAddress = ActiveCell.Address
End If
End Sub
When I enter data in cell F2 and click Enter the Cursor jumps to cell F9, but then it seems to get stuck there.
Does anyone know what I'm doing wrong?
I would use the change event like that
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rg As Range
On Error GoTo EH
Set rg = Union(Range("F2"), Range("F9"), Range("F15"), Range("F21"))
If Intersect(Target, rg) Is Nothing Then
Exit Sub
End If
Application.EnableEvents = False
Select Case Target.Address
Case "$F$2"
Range("F9").Select
Case "$F$9"
Range("F15").Select
Case "$F$15"
Range("F21").Select
End Select
EH:
Application.EnableEvents = True
End Sub
We can use a different event:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim F2 As Range, F9 As Range, F15 As Range, F22 As Range
Set F2 = Range("F2")
Set F9 = Range("F9")
Set F15 = Range("F15")
Set F21 = Range("F21")
If Not Intersect(Target, F2) Is Nothing Then
F9.Select
Exit Sub
End If
If Not Intersect(Target, F9) Is Nothing Then
F15.Select
Exit Sub
End If
If Not Intersect(Target, F15) Is Nothing Then
F21.Select
Exit Sub
End If
End Sub

Add value from one cell to another then reset cell value. Excel

Hope someone can answer us.
We want to do the following:
When a value is entered in A1, this value is added to the value of B1.
We then want the value of A1 too reset to 0, but keep the value of B1
Is this possible in excel?
On the worksheet's VBA private module (right-click the report tab and hit "View Code"), enter the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = Range("a1").Address Then
Range("b1") = Range("b1") + Range("a1")
Range("a1").ClearContents
End If
Application.EnableEvents = True
End Sub
Worksheet_Change is called whenever a change is made to the worksheet.
Application.EnableEvents=False prevents the code from running continuously (without it, the change to B1 would also call Worksheet_change).
This code assumes that the value in B1 is always numeric (or blank). If it may contain character text, then a check will need to be put in place to either reset its value or not do the increment.
Using simoco's suggestion:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A1 As Range
Set A1 = Range("A1")
If Intersect(Target, A1) Is Nothing Then
Else
Application.EnableEvents = False
With A1
.Offset(0, 1) = .Offset(0, 1) + .Value
.ClearContents
End With
Application.EnableEvents = True
End If
End Sub

Resources