Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a function from an IF statement in Excel

Status
Not open for further replies.

groovygarden

Programmer
Aug 23, 2001
63
I've been trying to figure this out for the last couple of hours. Any help appreciated!

I have a cell, say B2, and want to add an IF statement to it. If the statement returns true, I want to run a function which will change the colour of B2.

I think that IF statement would look something like:

Code:
=IF(A1=B1,colourFunction())


The function must change the colour of my cell, B2, based on the value in cell A2.

I know I should use a CASE Statement, but that's about all I know. How do I pass in the value of A2? How do I tell the function that it is cell B2 it needs to change?

I've been trying without much luck. I often find that, when the IF statement returns true and the function tries to run, I get an error message in cell B2 "#NAME"

Any help appreciated
 
Why not simply use Conditional Formatting ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Conditional Formatting is the best way to accomplsih your goal. However, if you're set on using code try something similar to the code below.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Sheets(1).Range("A3").Value = Sheets(1).Range("B3").Value Then
        Sheets(1).Range("B3").Interior.ColorIndex = 35
    Else
        Sheets(1).Range("B3").Interior.ColorIndex = xlNone
    End If

End Sub

BD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top