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
Bookmarks