Solved

Excel VBA Run macro when cell value changes from a formula

Posted on 2013-01-18
5
4,597 Views
Last Modified: 2013-01-18
I have a data validation list in cell B3.  I want to run a macro when this value changes.  I have a formula in cell P2 that is =B3, so whatever the value in B3 is what shows in P2.  I have the below vba code, hoping my clearA macro will run, but it is not working.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Target, Range("P2")) Is Nothing Then
If Target.Address = "$P$2" Then
clearA
End If
End Sub

Open in new window

0
Comment
Question by:jnikodym
5 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38793407
Try

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$B$3" Then
clearA
End If
End Sub
0
 
LVL 13

Expert Comment

by:Shanan212
ID: 38793411
Try this

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$B$3" Then
MsgBox "Hi"
End If

End Sub

Open in new window

0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 38793413
Instead of checking if P2 has changed check if B3 has changed.
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("B3")) Is Nothing Then

      clearA
   End If 

End Sub 

Open in new window

0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38793458
Does that mean that the first one does not work?
0
 

Author Comment

by:jnikodym
ID: 38793474
the Worksheet_SelectionChange part of your solution was causing an issue.  When i changed it to Worksheet_Change it worked.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now