+ Reply to Thread
Results 1 to 8 of 8

Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 errors

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 errors

    Hello, new to the forum (and to VBA in general).

    I'm working on building a tool in Excel that dynamically changes the fill color of each state in a US map, based on values in another sheet. It basically works by finding each state name and value, returning the fill color in the cell one column to the right, and applying that fill color to the appropriate state in the map.

    It has been working off and on, but recently broke for no apparent reason. I now receive a 1004 Error (Unable to get the Match property of the WorksheetFunction class).

    I have one piece of code (functioning properly) that changes the fill color depending on the value of the cell (which in this case is a percentile):

    Sub ChangeColor()
    
    Dim LastRow As Long
    Dim FullRange As Variant
    Dim cell As Variant
    
    With ActiveSheet
        LastRow = Range("C" & Rows.Count).End(xlUp).Row
    End With
    
    Set FullRange = Range("C2:C" & LastRow)
    For Each cell In FullRange
    
    If cell.Value < 0.1 Then cell.Interior.Color = RGB(99, 37, 35)
    If cell.Value >= 0.1 And cell.Value < 0.2 Then cell.Interior.Color = RGB(150, 54, 52)
    If cell.Value >= 0.2 And cell.Value < 0.3 Then cell.Interior.Color = RGB(218, 150, 148)
    If cell.Value >= 0.3 And cell.Value < 0.4 Then cell.Interior.Color = RGB(230, 184, 183)
    If cell.Value >= 0.4 And cell.Value < 0.5 Then cell.Interior.Color = RGB(242, 220, 219)
    If cell.Value >= 0.5 And cell.Value < 0.6 Then cell.Interior.Color = RGB(235, 241, 222)
    If cell.Value >= 0.6 And cell.Value < 0.7 Then cell.Interior.Color = RGB(216, 228, 188)
    If cell.Value >= 0.7 And cell.Value < 0.8 Then cell.Interior.Color = RGB(179, 203, 127)
    If cell.Value >= 0.8 And cell.Value < 0.9 Then cell.Interior.Color = RGB(118, 147, 60)
    If cell.Value >= 0.9 Then cell.Interior.Color = RGB(0, 76, 0)
        Next
    End Sub
    The second piece of code, which actually populates the colors in the map, looks like this:

    Sub Update_Map_VISITS()
    
    Application.ScreenUpdating = False
    
        Dim intState As Integer
        Dim strStateName As String
        Dim intStateValue As Variant
        Dim intColorLookup As Variant
        Dim rngStates As Range
        Dim rngColors As Range
        
        Set rngStates = Range(ThisWorkbook.Names("STATES_VISITS").RefersTo)
        Set rngColors = Range(ThisWorkbook.Names("STATE_COLORS_VISITS").RefersTo)
        
            With Worksheets("MainMap")
                For intState = 1 To rngStates.Rows.Count
                    strStateName = rngStates.Cells(intState, 1).Text
                    intStateValue = rngStates.Cells(intState, 2).Value
                    intColorLookup = Application.WorksheetFunction.Match(intStateValue, Range("STATE_COLORS_VISITS"), True)
                        With .Shapes(strStateName)
                            .Fill.Solid
                            .Fill.ForeColor.RGB = rngColors.Cells(intColorLookup, 1).Offset(0, 1).Interior.Color
                        End With
                Next
            End With
        
            Range("E8").Value = Range("C4").Value
    
    Application.ScreenUpdating = True
    
    End Sub
    Where STATES_VISITS, and STATE_COLORS_VISITS are named ranges in Excel. I'm getting the error in the following line:

    intColorLookup = Application.WorksheetFunction.Match(intStateValue, Range("STATE_COLORS_VISITS"), True)
    I've also attached the full excel file in case that helps. HUGE thank you to anyone who can help shed some light on this!
    -Chris
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,638

    Re: Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 erro

    Works for me. Well, it doesn't crash. I don't know if anything changes. I click on the button, it flickers, it comes back.

    I can put a break on the module and step through the code and it goes through so it IS doing something and it's not breaking.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-22-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 erro

    Hmmm, did you try dropping in a new set of numbers in column B, running the ChangeColor script, then try updating the map?

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,638

    Re: Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 erro

    Hmmm, did you try dropping in a new set of numbers in column B, running the ChangeColor script, then try updating the map?
    How would I know I should do that? What is the process you go through? How do you reproduce the fault? What is a valid set of numbers? Is there an invalid set of numbers? Am I supposed to make it up? Do I run the macros in the VBE?

  5. #5
    Registered User
    Join Date
    01-22-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 erro

    Strange, I'm still getting that 1004 error every time I try updating the map...

    Ideally I'd like to be able to drop in new sets of values into column B, but it looks like that's what's causing the error. As a test, would you mind trying some random values in Column B (i.e. Randbetween (1,1000)), running the ChangeColor script so that column C updates, and then seeing if the map will update accordingly on your end?

    Really appreciate the help!

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,638

    Re: Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 erro

    OK, done that, got the crash.

            With Worksheets("MainMap")
                For intState = 1 To rngStates.Rows.Count
                    strStateName = rngStates.Cells(intState, 1).Text
                    intStateValue = rngStates.Cells(intState, 2).Value
                        intColorLookup = Application.WorksheetFunction.Match(intStateValue, Range("STATE_COLORS_VISITS"), True)
                        With .Shapes(strStateName)
                            .Fill.Solid
                            .Fill.ForeColor.RGB = rngColors.Cells(intColorLookup, 1).Offset(0, 1).Interior.Color
                        End With
                Next
            End With
    intState = 22
    strStateName = MICHIGAN
    intStateValue = 55

    ?rngstates.Address
    $A$2:$B$51

    rngStates.Rows.Count = 50


    I'm guessing the problem is here:

    Sub ChangeColor()
    
    Dim LastRow As Long
    Dim FullRange As Variant
    Dim cell As Variant
    
    With ActiveSheet
        LastRow = Range("E" & Rows.Count).End(xlUp).Row
    End With
    
    Set FullRange = Range("E2:E" & LastRow)
    For Each cell In FullRange

    There's nothing in column E so that range becomes E1:E2 as Lastrow will be 1.

    Have you tidies up the worksheet and not changed the code to match?


    Regards, TMS

  7. #7
    Registered User
    Join Date
    01-22-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 erro

    Update: SOLVED

    Turns out that if you use an approximate match in the MATCH function ("true"/"1"), the reference array needs to be in ascending/descending order. I was able to update my code to exact match and it works perfectly:

    intColorLookup = Application.WorksheetFunction.Match(intStateValue, Range("STATE_COLORS_VISITS"), 0)
    Thanks again for looking into this!
    -Chris

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,638

    Re: Dynamic US Map was working fine, but now I'm getting WorksheetFunction.Match 1004 erro

    You're welcome.

    Still a bit concerned about the reference to a column with no data for the loop ... not sure that's doing what you think it is.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ 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. Replies: 2
    Last Post: 05-24-2013, 09:32 AM
  2. [SOLVED] WorksheetFunction.match run time error '1004' with numbers but not text
    By Hussar13 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2013, 05:31 PM
  3. Run-time error 1004: Unable to get the Match property of the WorksheetFunction class
    By maffmommie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2010, 09:32 AM
  4. worksheetfunction.match giving run time error '1004'
    By devo2511 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-15-2010, 04:47 PM
  5. run-time error '1004 Unable to get Match property of WorksheetFunction class
    By exl044 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-08-2010, 02:23 AM

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