Consulting

Results 1 to 5 of 5

Thread: Colored Shapes based on cell value

  1. #1
    VBAX Regular
    Joined
    Oct 2008
    Posts
    26
    Location

    Colored Shapes based on cell value

    Any help would be appreciated.

    I would like to create a color heatmap with shapes. The shapes should be linked to a cell value and a certain color according to value (1,2,3,4,5)

    I've tried to adapt p45calc's code with no luck:

    [VBA]Private Sub Worksheet_Change(ByVal Target As range)
    With Target
    If IsNumeric(.Value) And .Value > 1 <= 2 Then
    Set TheCircle = ActiveSheet.Shapes.AddShape(msoShapeOval, _
    .Left, _
    Top:=.Top, _
    Width:=.Width, _
    Height:=.Height)
    ' Optional stuff:
    With TheCircle
    LockAspectRatio = msoTrue
    With .Line
    Weight = 3.5
    .ForeColor.SchemeColor = 10
    .BackColor.RGB = RGB(255, 255, 255)
    End With
    With .Fill
    .Solid
    .ForeColor.SchemeColor = 43
    .Transparency = 0.69
    End With
    End With
    End If
    End With
    End Sub[/VBA]
    Last edited by Bob Phillips; 02-28-2011 at 09:20 AM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
    If IsNumeric(.Value) And .Value > 1 <= 5 Then
    Set TheCircle = ActiveSheet.Shapes.AddShape(msoShapeOval, _
    .Left, _
    Top:=.Top, _
    Width:=.Width, _
    Height:=.Height)
    ' Optional stuff:
    With TheCircle
    LockAspectRatio = msoTrue
    With .Line
    Weight = 3.5
    .ForeColor.SchemeColor = 10
    .BackColor.RGB = RGB(255, 255, 255)
    End With
    With .Fill
    .Solid
    Select Case Target.Value
    Case 1: .ForeColor.RGB = Me.Range("M1").Interior.Color
    Case 2: .ForeColor.RGB = Me.Range("M2").Interior.Color
    Case 3: .ForeColor.RGB = Me.Range("M3").Interior.Color
    Case 4: .ForeColor.RGB = Me.Range("M4").Interior.Color
    Case 5: .ForeColor.RGB = Me.Range("M5").Interior.Color
    End Select
    .Transparency = 0.69
    End With
    End With
    End If
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Oct 2008
    Posts
    26
    Location
    Brilliant , thanks xld

    the case statements can also act as the legend

    Last q, is there a way to automaticaaly size the shape a bit smaller than cell (to overcome shapes being to close to each other)?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
    If IsNumeric(.Value) And .Value > 1 <= 5 Then
    Set TheCircle = ActiveSheet.Shapes.AddShape(msoShapeOval, _
    Left:=.Left + 2, _
    Top:=.Top + 2, _
    Width:=.Width - 4, _
    Height:=.Height - 4)
    ' Optional stuff:
    With TheCircle
    LockAspectRatio = msoTrue
    With .Line
    Weight = 3.5
    .ForeColor.SchemeColor = 10
    .BackColor.RGB = RGB(255, 255, 255)
    End With
    With .Fill
    .Solid
    Select Case Target.Value
    Case 1: .ForeColor.RGB = Me.Range("M1").Interior.Color
    Case 2: .ForeColor.RGB = Me.Range("M2").Interior.Color
    Case 3: .ForeColor.RGB = Me.Range("M3").Interior.Color
    Case 4: .ForeColor.RGB = Me.Range("M4").Interior.Color
    Case 5: .ForeColor.RGB = Me.Range("M5").Interior.Color
    End Select
    .Transparency = 0.69
    End With
    End With
    End If
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Oct 2008
    Posts
    26
    Location
    thanks works excellently!

Posting Permissions

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