Private Sub Worksheet_Change(ByVal Target As Range) Not working

HarryP96

New Member
Joined
Oct 20, 2020
Messages
11
Office Version
  1. 2010
Platform
  1. MacOS
Hi,

I am trying to trigger Macros according to the value cell A2 of Sheet1 is changed to. After changing the value in A2 nothing seems to be happening. What am I missing? Cell A2 is merged with everything up to cell B7, but I have also tried without the cell being merged.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If Not Intersect(Target, Sheets("Sheet1").Range("A2")) Is Nothing Then
    If Target.Cells.Value = " " Or IsEmpty(Target) Then Exit Sub
    If Target.Value = "1" Then Call Athlete1
    If Target.Value = "2" Then Call Athlete2
    End If
End Sub

Sub Athlete1()
    At1 = ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1
    ThisWorkbook.Sheets("Sheet2").Cells(At1, 1) = Time
End Sub

Sub Athlete2()
    At2 = ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row + 1
    ThisWorkbook.Sheets("Sheet2").Cells(At2, 2) = Time
End Sub

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
The only reason the event doesn't fire is because the macro Worksheet_Change isn't in the Sheet1's module.
It's useless to indicate in the macro Sheets("Sheet1").Range("A2"), Range("A2") will do as long as the macro is in the right sheet module.
 
Upvote 0
Solution
The only reason the event doesn't fire is because the macro Worksheet_Change isn't in the Sheet1's module.
It's useless to indicate in the macro Sheets("Sheet1").Range("A2"), Range("A2") will do as long as the macro is in the right sheet module.
Thanks, I have removed Sheets("Sheet1") but unfortunately, it is still not firing. I have run athlete1 and athlete2 on their own and they are running as expected.
 
Upvote 0
In A2 are you entering the numbers 1 and 2 as text ?
If not then lose the quotation marks in your If statements.
eg If Target.Value = 1
 
Upvote 0
In A2 are you entering the numbers 1 and 2 as text ?
If not then lose the quotation marks in your If statements.
eg If Target.Value = 1
Thanks for your response. I have removed the quotation marks as suggested. I have also ensure A2 is formatted as a number, but still no luck. Formatting as "General" also did not seem to work.
 
Upvote 0
The sub is definitely in the "Worksheet module" of Sheet 1 right ?
Do you know how to set a breakpoint ?
Set a breakpoint on the first if statement in the code. (Click in the left margin next to the if statement, you should see a big dot, brown on my machine).
Enter 1 or 2 in A2. Switch to vba and see if the first line is yellow. If yes the macro is triggering. <F8> through the code to see which if statement path it takes.
 
Upvote 0
If your macro is in the right place try launching from the 'Immediate' panel the function:
Application.EnableEvents = True
Maybe it has been temporarily turned off (False).
 
Upvote 0
Thanks everyone for your help. I was under the false impression that by right clicking on sheet1 and selecting insert module had done enough to put this "within" sheet 1. In reality, I needed to right click sheet1 within the project explorer and select "View Code". After pasting the code in there, it worked. Thanks again for your help!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top