Events in VBA

Masha92

Board Regular
Joined
Jan 27, 2019
Messages
51
Office Version
  1. 365
Hi all,

I am trying to run a macro based on a cell value. I inserted the below code in the sheet where the cell exists. The code never runs and I added a msgbox just to double check and it doesnt fire up. Can you please help me out?

Many thanks!
Masha
Code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet, Exists As Boolean: Exists = False
Application.DisplayAlerts = False
Application.EnableEvents = True
Set Target = Sheets("Ref").Range("W3")
MsgBox "Changed: " & Target.Address




For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Client File" Then
Exists = True
Exit For
End If
Next ws





If Target.Value = 1 And Exists = False Then


Worksheets.Add(after:=Worksheets.Count) = "Client File"

ElseIf Target.Value = 2 And Exists = True Then


Sheets("Client File").Delete
End If









End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Just to add to this. What I am trying to achieve is when the user select an item via a drop down list (sheet A), the reference cell(sheet B) of this drop down list will change and it exists in a different sheet.

So when the user select a new item from the drop down list, I need a macro to be triggered. (Ideally because the reference cell changes)

Thank you all for the help in advance.
 
Upvote 0
Just to add to this. What I am trying to achieve is when the user select an item via a drop down list (sheet A), the reference cell(sheet B) of this drop down list will change and it exists in a different sheet.

So when the user select a new item from the drop down list, I need a macro to be triggered. (Ideally because the reference cell changes)

Thank you all for the help in advance.

First we will send the following code in a module, to verify that the events are active:

Code:
Sub test()
  Application.EnableEvents = True
End Sub

Now, put the following code in the events of your sheet:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Value = "" Then Exit Sub
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        MsgBox "Changed: " & Target.Address
        Call your_macro
    End If
End Sub
 
Upvote 0
Thank you Dante Amor. After I restarted the excel and ended all processes, it worked.

Thanks though for your help!
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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