Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel VBA Run macro when cell value changes from a formula

Posted on 2013-01-18
5
4,881 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

809 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