Run macro based on cell condition

E

EMoe

Hello,

How do I run a simple macro, based on a cell condition.

Ex. If A1 is equal to 1, then run a macro that changes a shape from the
drawing tool box, like the cloud to red. If A1 returns to 0, then run
macro to change the cloud color to blue.

Thanks,
EMoe
 
N

Norman Jones

Hi EMoe,

Try:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Me.Shapes(1).Fill.ForeColor
Select Case Range("A1").Value
Case 1: .SchemeColor = 2
Case 0: .SchemeColor = 12
Case Else: .SchemeColor = 11
End Select
End With
End Sub

Amend the numeric in Shapes(1) to the index number or name of the cloud.

This event code needs to br inserted in the sheet module: right-click the
sheet tab | View Code | paste.
 
N

Norman Jones

Hi EMoe,

I intended to use the Change event ( rather than the SelectionChange event):

Private Sub Worksheet_Change(ByVal Target As Range)
With Me.Shapes(1).Fill.ForeColor
Select Case Range("A1").Value
Case 1: .SchemeColor = 2
Case 0: .SchemeColor = 12
Case Else: .SchemeColor = 11
End Select
End With
End Sub
 
N

Norman Jones

And, preferable would be:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1")) Is Nothing Then
With Me.Shapes(1).Fill.ForeColor
Select Case Me.Range("A1").Value
Case 1: .SchemeColor = 2
Case 0: .SchemeColor = 12
Case Else: .SchemeColor = 11
End Select
End With
End If
End Sub
 
E

EMoe

Sorry, but I tried all 3 codes in ''this workbook", as well as in module
1, but the code didn't change the color of the shape.

The name of the cloud is AutoShape 1.

Here's how the code looked in my window:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1")) Is Nothing Then
With Me.Shapes(AutoShape1).Fill.ForeColor
Select Case Me.Range("A1").Value
Case 1: .SchemeColor = 2
Case 0: .SchemeColor = 12
Case Else: .SchemeColor = 11
End Select
End With
End If
End Sub

I even tried ("AutoShape 1") in parenthesis, but still no dice.

What did I miss?
EMoe
 
D

Dave Peterson

Put it behind the worksheet that contains the shape.

(rightclick on the worksheet tab, select view code and paste it there.)

(Remove it from the ThisWorkbook and the Module1 modules.
 
E

EMoe

Works like a charm now.

By the way...

Is there a way, by looking at a code, to know where it goes; in
module, in this workbook, or this worksheet?

Thanks a bunch again for your help.

Regards,
EMo
 
N

Norman Jones

Hi EMoe,
Is there a way, by looking at a code, to know where it goes; in a
module, in this workbook, or this worksheet?

I tried to direct you by including, in my first post, the statement:

For clarity, I should have appended this to my follow up posts.

Without such direction you would need to recognise it as an event procedure.
Having done so, all worksheet event procedures are all of the form:

Sub Worksheet_SomeEvent

and all workbook event procedures are of the form

Sub Workbook_SomeEvent

Worksheet event code is placed in the relevant worksheet module; workbook
event code goes in the workbook's ThisWorkbook module.

For a good guide to event procedures, see Chip Pearson at:

http://www.cpearson.com/excel/events.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top