+ Reply to Thread
Results 1 to 9 of 9

Shape coloring macro tied to formula

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-18-2013
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    139

    Shape coloring macro tied to formula

    Hey guys,

    This is probably something easy but I can't figure it out. I have this macro to color shapes depending on the value

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    
    Set rng1 = [T5]
    Set rng2 = [U5]
    Set rng3 = [V5]
    
    If Intersect(Target, Union(rng1, rng2, rng3)) Is Nothing Then Exit Sub
    
    Dim shp1 As Shape, shp2 As Shape, shp3 As Shape
    Dim bytColor1 As Byte, bytColor2 As Byte, bytColor3 As Byte
    Set shp1 = ActiveSheet.Shapes("Oval 38")
    Set shp2 = ActiveSheet.Shapes("Oval 47")
    Set shp3 = ActiveSheet.Shapes("Oval 65")
    
    Select Case rng1
        Case 57 To 100
            bytColor1 = 2
        Case 26 To 56
            bytColor1 = 5
        Case 1 To 25
            bytColor1 = 3
        Case Else
            bytColor1 = 1
    End Select
    Select Case rng2
        Case 68 To 100
            bytColor2 = 2
        Case 34 To 67
            bytColor2 = 5
        Case 0 To 33
            bytColor2 = 3
        Case Else
            bytColor2 = 1
    End Select
    Select Case rng3
        Case 0 To 25
            bytColor3 = 2
        Case 26 To 56
            bytColor3 = 5
        Case 57 To 100
            bytColor3 = 3
        Case Else
            bytColor3 = 1
    End Select
    shp1.Fill.ForeColor.SchemeColor = bytColor1
    shp2.Fill.ForeColor.SchemeColor = bytColor2
    shp3.Fill.ForeColor.SchemeColor = bytColor3
    End Sub
    Works fine when I enter values from 1 to 100 at T5, U5, V5. However these cells should be formulas and when I change them, coloring stops working.(for example this is the formula for T5: =IFERROR((($T$2/$T$3)*6.25)/100; "")

    How do I fix this?

    Thank you!

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Shape coloring macro tied to formula

    formula calculations do not trigger the change event so you would have to use the calculate event and check all the cells each time it triggers (there is no Target argument for the Calculate event)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Contributor
    Join Date
    03-18-2013
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Shape coloring macro tied to formula

    I think you overestimated my knowledge on VBA What I understand from your response is that this macro wont work with formulas, and I should look for a macro that starts with Worksheet_calculate to do the job, right? Oh, well I will look for one now.

    Thank you Josie.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Shape coloring macro tied to formula

    Private Sub Worksheet_Calculate()
    
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    
    Set rng1 = [T5]
    Set rng2 = [U5]
    Set rng3 = [V5]
    
    Dim shp1 As Shape, shp2 As Shape, shp3 As Shape
    Dim bytColor1 As Byte, bytColor2 As Byte, bytColor3 As Byte
    Set shp1 = ActiveSheet.Shapes("Oval 38")
    Set shp2 = ActiveSheet.Shapes("Oval 47")
    Set shp3 = ActiveSheet.Shapes("Oval 65")
    
    Select Case rng1
        Case 57 To 100
            bytColor1 = 2
        Case 26 To 56
            bytColor1 = 5
        Case 1 To 25
            bytColor1 = 3
        Case Else
            bytColor1 = 1
    End Select
    Select Case rng2
        Case 68 To 100
            bytColor2 = 2
        Case 34 To 67
            bytColor2 = 5
        Case 0 To 33
            bytColor2 = 3
        Case Else
            bytColor2 = 1
    End Select
    Select Case rng3
        Case 0 To 25
            bytColor3 = 2
        Case 26 To 56
            bytColor3 = 5
        Case 57 To 100
            bytColor3 = 3
        Case Else
            bytColor3 = 1
    End Select
    shp1.Fill.ForeColor.SchemeColor = bytColor1
    shp2.Fill.ForeColor.SchemeColor = bytColor2
    shp3.Fill.ForeColor.SchemeColor = bytColor3
    End Sub
    for instance :-)

  5. #5
    Forum Contributor
    Join Date
    03-18-2013
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Shape coloring macro tied to formula

    This is awesome, works like a charm!

    Thank you so much Josie!

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Shape coloring macro tied to formula

    you're welcome :-)

  7. #7
    Forum Contributor
    Join Date
    03-18-2013
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Shape coloring macro tied to formula

    Hmm, one more question.
    When I open another excel file I get an error, as it seems the new file runs the macro from this workbook.

    How do I limit this macro only to this file? (Also I change the names all the time like V22, V23 etc., if that can be taken into account).

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Shape coloring macro tied to formula

    perhaps
    Private Sub Worksheet_Calculate()
    
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    
    Set rng1 = Range("T5")
    Set rng2 = Range("U5")
    Set rng3 = Range("V5")
    
    Dim shp1 As Shape, shp2 As Shape, shp3 As Shape
    Dim bytColor1 As Byte, bytColor2 As Byte, bytColor3 As Byte
    Set shp1 = Me.Shapes("Oval 38")
    Set shp2 = Me.Shapes("Oval 47")
    Set shp3 = Me.Shapes("Oval 65")
    
    Select Case rng1
        Case 57 To 100
            bytColor1 = 2
        Case 26 To 56
            bytColor1 = 5
        Case 1 To 25
            bytColor1 = 3
        Case Else
            bytColor1 = 1
    End Select
    Select Case rng2
        Case 68 To 100
            bytColor2 = 2
        Case 34 To 67
            bytColor2 = 5
        Case 0 To 33
            bytColor2 = 3
        Case Else
            bytColor2 = 1
    End Select
    Select Case rng3
        Case 0 To 25
            bytColor3 = 2
        Case 26 To 56
            bytColor3 = 5
        Case 57 To 100
            bytColor3 = 3
        Case Else
            bytColor3 = 1
    End Select
    shp1.Fill.ForeColor.SchemeColor = bytColor1
    shp2.Fill.ForeColor.SchemeColor = bytColor2
    shp3.Fill.ForeColor.SchemeColor = bytColor3
    End Sub
    not sure what you meant about changing the names-what names?

  9. #9
    Forum Contributor
    Join Date
    03-18-2013
    Location
    Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    139

    Re: Shape coloring macro tied to formula

    Now it is perfect!

    Thank you once again for your help

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Use a button to control a macro that inserts an image into a shape or resets the shape
    By nwb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2013, 04:41 PM
  2. Replies: 0
    Last Post: 11-30-2012, 01:29 PM
  3. Need help programming a Macro tied to a button
    By lucidriceball in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-21-2012, 03:15 PM
  4. Replies: 0
    Last Post: 04-27-2012, 04:22 PM
  5. Replies: 7
    Last Post: 04-03-2010, 03:48 PM

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