+ Reply to Thread
Results 1 to 5 of 5

VBA AutoShape Color Change Script for Formula Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    05-29-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Unhappy VBA AutoShape Color Change Script for Formula Cell

    Hi, hope you guys can help - VBA virgin here...

    I'm employing the script below to change the fill color of 'FreeForm 9' autoshape based on the value in cell 'U117'.
    I understand that if cell 'U117' has a formula in it rather than a simple value, VBA will not use the resulting value.
    How can I replace the "U117" reference in the script and use the resulting value from a formula subtracting A1 - A2 instead?
    Thanks for any advice .

    Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("U117")) Is Nothing Then
    If IsNumeric(Target.Value) Then
    If Target.Value > 0.4 Then
    ActiveSheet.Shapes("FreeForm 9").Fill.ForeColor.RGB = vbRed
    ElseIf Target.Value <= 0.4 And Target.Value > 0.05 Then
    ActiveSheet.Shapes("FreeForm 9").Fill.ForeColor.RGB = vbYellow
    ElseIf Target.Value <= 0.05 Then
    ActiveSheet.Shapes("FreeForm 9").Fill.ForeColor.RGB = vbGreen
    End If
    End If
    End If
    End Sub
    Last edited by Sean Ross; 05-29-2013 at 12:16 PM.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA AutoShape Color Change Script for Formula Cell

    VBA will pick up the value by default in a term such as:
    If Range("U117") = 1 then
    The intersect function determines whether the ranges overlap, and the arguements for the function must be ranges. Therefore you cannot have the resulting formula of A2 - A1 instead of range("U117").

    If you want to determine whether the value of "target" is equal to the value of A2-A1 you need:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target = Range("A2") - Range("A1") Then
    If IsNumeric(Target.Value) Then
    If Target.Value > 0.4 Then
    ActiveSheet.Shapes("FreeForm 9").Fill.ForeColor.RGB = vbRed
    ElseIf Target.Value <= 0.4 And Target.Value > 0.05 Then
    ActiveSheet.Shapes("FreeForm 9").Fill.ForeColor.RGB = vbYellow
    ElseIf Target.Value <= 0.05 Then
    ActiveSheet.Shapes("FreeForm 9").Fill.ForeColor.RGB = vbGreen
    End If
    End If
    End If
    End Sub

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: VBA AutoShape Color Change Script for Formula Cell

    This worked for me
    NB:- If you assume the results of formula cell is "A3" and the "Precedents" cells are "A1 & A2",
    Then "A1 :A2" will be the Actual target cells, Not "A3"
    The result of formula cell (in you case "U117" ,in Example case "A3") is not the Target cell
    (NB:- Target cell= Cell selected)
    But by setting the Cell ("U117 or "A3") as a range object "Tar" it can be used in the code as below.
    Example code below based on your Code, but with alterations:-

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Tar As Range
    Set Tar = Range("A3") 'Formula result cell
    If Not Intersect(Target, Range("A1:A2")) Is Nothing Then
        If IsNumeric(Tar.Value) Then
            If Tar.Value > 0.4 Then
                ActiveSheet.Shapes("FreeForm 1").Fill.ForeColor.RGB = vbRed
            ElseIf Tar.Value <= 0.4 And Tar.Value > 0.05 Then
                ActiveSheet.Shapes("FreeForm 1").Fill.ForeColor.RGB = vbYellow
            ElseIf Tar.Value <= 0.05 Then
                ActiveSheet.Shapes("FreeForm 1").Fill.ForeColor.RGB = vbGreen
            End If
        End If
    End If
    End Sub

  4. #4
    Registered User
    Join Date
    05-29-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA AutoShape Color Change Script for Formula Cell

    Excellent.
    Thanks for the advice guys. All working now, and I've learnt something new.
    Cheers

  5. #5
    Registered User
    Join Date
    05-29-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA AutoShape Color Change Script for Formula Cell

    If in the future I want to duplicate this script to act on additional different cells (and their corresponding freeforms) within the same worksheet, is there a VBA procedure I need to separate them?
    (Sorry, very new to this)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1