+ Reply to Thread
Results 1 to 4 of 4

identifying shape.type to change colour of line or fill

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-29-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2016 (Office 365)
    Posts
    137

    identifying shape.type to change colour of line or fill

    Hi all,

    I have a bunch of shapes on a spreadsheet including lines, squares, triangles etc. I change the colour of the shape depending on some criteria and if the shape is a line I only want to change the line colour however if it is other than a line, i.e. a triangle, I want the line colour to remain the same but the fill colour to change.

                R = Application.WorksheetFunction.VLookup(Draw_Start_Cell.Offset(counter, i + 1).Value, rng, 3, False)
                G = Application.WorksheetFunction.VLookup(Draw_Start_Cell.Offset(counter, i + 1).Value, rng, 4, False)
                B = Application.WorksheetFunction.VLookup(Draw_Start_Cell.Offset(counter, i + 1).Value, rng, 5, False)
                
                On Error Resume Next
                Set shp = ActiveSheet.Shapes(Draw_Start_Cell.Offset(counter, i).Value)
                On Error GoTo Skip
    
                With shp
                    If .Type = msoConnectorStraight Then .Line.ForeColor.RGB = RGB(R, G, B)
                End With
    this is the unfinished code at the moment, the code is contained within a loop. The if .Type line changes the the boarder on squares, triangles etc which I don't want to happen, just the fill. It works as I want it to on a straight line. Any suggestions?

    Cheers,
    Marcus

  2. #2
    Forum Contributor
    Join Date
    06-29-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2016 (Office 365)
    Posts
    137

    Re: identifying shape.type to change colour of line or fill

    Just to clarify what I am after I need something like this:

    if .Type = "something with a fill" then

    .fill.forecolor.RGB = .......

    else if .Type = "shape with line only" then

    .line.forecolor.RGB = .......

    end if

    It is the "...." item that I need code for that I have so far been unable to figure out.

    Cheers,
    Marcus

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: identifying shape.type to change colour of line or fill

    Hi demarc,

    The following code (tested in Excel 2003 and 32 bit Excel 2010) may help you:
    Option Explicit
    
    Sub ProcessShapes()
    
      'Shape Type enumerations reference: https://msdn.microsoft.com/en-us/library/office/ff860759.aspx
    
      Dim Sh As Shape
      Dim iShapeType As Long
      
      Dim R As Long
      Dim G As Long
      Dim B As Long
      
      
      R = 255
      G = 0
      B = 255
      
      For Each Sh In ActiveSheet.Shapes
      
        iShapeType = Sh.Type
        
        If iShapeType = msoAutoShape Then
        
          Sh.Line.ForeColor.RGB = RGB(R, G, B)
          Sh.Fill.ForeColor.RGB = RGB(R, G, B)
          
        ElseIf iShapeType = msoLine Then
        
          Sh.Line.ForeColor.RGB = RGB(R, G, B)
          
        End If
    
      Next Sh
    
    End Sub

    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Option Explicit
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Lewis

  4. #4
    Forum Contributor
    Join Date
    06-29-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2016 (Office 365)
    Posts
    137

    Re: identifying shape.type to change colour of line or fill

    Hi Lewis,

    I ended up solving my problem with this:

                R = Application.WorksheetFunction.VLookup(Draw_Start_Cell.Offset(counter, i + 1).Value, rng, 3, False)
                G = Application.WorksheetFunction.VLookup(Draw_Start_Cell.Offset(counter, i + 1).Value, rng, 4, False)
                B = Application.WorksheetFunction.VLookup(Draw_Start_Cell.Offset(counter, i + 1).Value, rng, 5, False)
                
                On Error Resume Next
                Set shp = ActiveSheet.Shapes(Draw_Start_Cell.Offset(counter, i).Value)
                On Error GoTo Skip
                
                With shp
                    If InStr(1, shp.Name, "Sq", 1) Then
                        .Fill.ForeColor.RGB = RGB(R, G, B)
                    ElseIf InStr(1, shp.Name, "Tri", 1) Then
                        .Fill.ForeColor.RGB = RGB(R, G, B)
                        .Line.ForeColor.RGB = RGB(R, G, B)
                    ElseIf InStr(1, shp.Name, "Cir", 1) Then
                        .Fill.ForeColor.RGB = RGB(R, G, B)
                    Else
                        .Line.ForeColor.RGB = RGB(R, G, B)
                    End If
                End With
    I make sure each shape for a square has a "Sq" in it, circle names with a "Cir" in it etc. This way it will know which requires a fill colour change and which requires a line colour change, or in the case of triangles both.

    Not sure if it is the most efficient way but works fine for my purposes.

    Thanks for your code and sorry I didn't mark this as solved sooner!

    Marcus

+ 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] Change Shape Fill colour based on cells values.
    By pipoliveira in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-26-2020, 09:21 AM
  2. Change shape colour dependent on cell value
    By BRISBANEBOB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-26-2014, 07:44 AM
  3. [SOLVED] to change shape colour by conditional formating/ VBA
    By asmi in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 11-08-2013, 03:57 AM
  4. [SOLVED] Change fill colour of shape with a click
    By schlepel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2012, 09:34 AM
  5. Need to change the colour of a shape according to its value
    By hawkinsr86 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-08-2012, 02:42 AM
  6. Alternating a shape's fill colour for a fixed period of time
    By deadlyduck in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-06-2010, 05:16 AM
  7. Macro to change colour and and text in shape and then back again
    By jamer02 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-16-2010, 07:17 AM
  8. Changing the Colour of an Auto Shape (Line)
    By d.i.barr in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-23-2005, 05:48 AM

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