macro to run when cell changes value

cmefly

Well-known Member
Joined
May 13, 2003
Messages
683
Hi,

I've got a drop-down menu (using forms not a listbox) in A1. In B1, i've got a vlookup formula that is dependent on A1. So when A1 changes, so does the value in B1.

I need a macro that is triggered only when B1 changes.

In my case, B1 can be x, y, or z. Each one would trigger a different macro.

I've got this but it doesn't work...any ideas?


Private Sub Worksheet_Change(ByVal Target As Range)
Target = Range("b1")
If Target.Value = "x" Then
MACRO1
End If

If Target.Value = "y" Then
MACRO2
End If

If Target.Value = "z" Then
MACRO3
End If
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You were very close to making it work. Change it a bit like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$1" Then
Select Case Target.Value
Case "x"
MACRO1
Case "y"
MACRO2
Case "z"
MACRO3
End Select
End Sub
 
Upvote 0
doesn't seem to be calling the macro....

i inserted an "end if" after "end select"
 
Upvote 0
Apologies. My mistake. I did not read about form. You'd be better of with another event like:
Code:
Private Sub Worksheet_Calculate()
Select Case Range("B1").Value
Case "x"
MACRO1
Case "y"
MACRO2
Case "z"
MACRO3
End Select
End Sub
 
Upvote 0
okay...so now it works but it keeps looping my macro....i have to use control-break to exit....
 
Upvote 0
At this point, try this (we'll stop event triggers when the code runs!)
Code:
Private Sub Worksheet_Calculate()
[COLOR=blue]Application.EnableEvents = False[/COLOR]
Select Case Range("B1").Value
Case "x"
MsgBox Range("B1").Value
Case "y"
MsgBox Range("B1").Value
Case "z"
MsgBox Range("B1").Value
End Select
[COLOR=blue]Application.EnableEvents = True
[/COLOR]End Sub
 
Upvote 0
HELP!!!

for some reason the above PRIVATE macro is being applied to other sheets!!!!
 
Upvote 0
By any chance are you using the following:
Code:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
End Sub

In other case, does this sheet (the sheet in question) have links with other sheets which when updated will cause some values to change and thus will cause the event to occur?
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,707
Members
452,939
Latest member
WCrawford

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