How do I get my Macro to run when Sheet Changes?

andybason

Board Regular
Joined
Jan 7, 2012
Messages
217
Office Version
  1. 2016
Hello,

I've put together the macro below and it runs fine when it's run manually.

I am now trying to get it to run when any change is made to Sheet1 without luck. Can anyone point me in the right direction?

Thank you

VBA Code:
Sub m()

Dim ThisCell1 As Range
Dim ThisCell2 As Range
    For Each ThisCell1 In ThisWorkbook.Sheets("Sheet1").Range("A5:A1000")
    'This is the range of cells to check
        For Each ThisCell2 In ThisWorkbook.Sheets("Sheet2").Range("A1:A10")
        'This is the range of cells to compare
            If ThisCell1 <> "" And ThisCell1.Value = ThisCell2.Value Then
                ThisCell1.Offset(, 16).Value = "BF5"
                Exit For
                End If
            Next ThisCell2
        Next ThisCell1

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
In the Sheet module, there is a "Worksheet_Change" event procedure, which is VBA code that is fired whenever a change is manually made to that worksheet.
So we can call your other macro from there.

Place this code in the Sheet1 module (do NOT put it in a Standard/General module, like Module1 - it MUST go in the appropriate sheet module):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Call m
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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