+ Reply to Thread
Results 1 to 4 of 4

Help with loop to fill color of shapes (US map)

Hybrid View

  1. #1
    Registered User
    Join Date
    08-25-2010
    Location
    pittsburgh
    MS-Off Ver
    Excel 2003
    Posts
    6

    Help with loop to fill color of shapes (US map)

    Working on a US map that shows media markets. Each media market is a shape and the shape name represents the market name (i.e. Pittsburgh).

    I have 2 values for 210 media markets. I want to compare the 2 values for each media market and then fill the corresponding shape with either red or blue based on which value is larger.

    I need help with a loop for this instead of writing individual code for each one like I am now....

    Private Sub Worksheet_Calculate()
    
        If Range("AI2").Value > Range("AJ2").Value Then
            ActiveSheet.Shapes.Range(Array("ALBUQUERQUE")).Select
            With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(0, 0, 255)
            End With
            
        Else
        
            ActiveSheet.Shapes.Range(Array("ALBUQUERQUE")).Select
            With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(255, 0, 0)
            End With
            
        End If
        
        If Range("AI3").Value > Range("AJ3").Value Then
            ActiveSheet.Shapes.Range(Array("BOSTON")).Select
            With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(0, 0, 255)
            End With
            
        Else
        
            ActiveSheet.Shapes.Range(Array("BOSTON")).Select
            With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(255, 0, 0)
            End With
            
        End If
        
        If Range("AI4").Value > Range("AJ4").Value Then
            ActiveSheet.Shapes.Range(Array("BUFFALO")).Select
            With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(0, 0, 255)
            End With
            
        Else
        
            ActiveSheet.Shapes.Range(Array("BUFFALO")).Select
            With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(255, 0, 0)
            End With
            
        End If
    
    End Sub
    My media market names are in AH2:AH211 and the corresponding values for each market are in AI2:AJ211.

    I'd like the code to look at each media market name, compare the values, and then fill the corresponding shape with either red or blue.

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Help with loop to fill color of shapes (US map)

    Split creates a zero based array of strings.
    That is why there are two commas before ALBUQUERQUE.
    ALBUQUERQUE will be the third element in the array, but with the index of 2.
    After you add the 11th row name, you will not need a comma following.

        Const sList = ",,ALBUQUERQUE,BOSTON,BUFFALO,,,,"
        Dim i As Long
        Dim sArray() As String
        
        sArray = Split(sList, ",")
        
        For i = 2 To 11
            If Range("AI" & i).Value > Range("AJ" & i).Value Then
                ActiveSheet.Shapes.Range(Array(sArray(i))).Select
                With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(0, 0, 255)
                End With
            Else
                ActiveSheet.Shapes.Range(Array(sArray(i))).Select
                With Selection.ShapeRange.Fill
                    .ForeColor.RGB = RGB(255, 0, 0)
                End With
            End If
        Next i
    I haven't tested it, but instead of:

    ActiveSheet.Shapes.Range(Array(sArray(i))).Select
    With Selection.ShapeRange.Fill
        .ForeColor.RGB = RGB(0, 0, 255)
    End With
    Why not?

    ActiveSheet.Shapes.Range(Array(sArray(i))).ShapeRange.Fill.ForeColor.RGB = RGB(0, 0, 255)
    I'm unclear what the Array function is doing?

  3. #3
    Registered User
    Join Date
    08-25-2010
    Location
    pittsburgh
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Help with loop to fill color of shapes (US map)

    Steven - that's working.

    In the list, how would I include a media market name like "Charleston, WV"?

    Also, what do you mean by "After you add the 11th row name, you will not need a comma following"?

    I have a list of 210 media markets. How do I get all of the media market names in that list?

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Help with loop to fill color of shapes (US map)

    My mistake, I read AH2:AH211 as if it was AH2:AH11.

    You can add them all to sList. There does not need to be a comma after the last item in the list.

    And obviously,

    For i = 2 To 11
    Becomes:
    For i = 2 To 211
    In the list, how would I include a media market name like "Charleston, WV"?
    Change the comma (in sList) to ";" or some other character.

    And then:
    sArray = Split(sList, ";")
    The point is that you have a list of items in one string separated by a character, then you use Split to split the string into a string array based on this character. It can be a comma, a semi-comma, "@", an empty space " ", or any character which you can put in a string.
    Last edited by StevenM; 06-28-2012 at 01:54 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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