我在编码“9”以上时遇到了麻烦。
Sub ColourStates()
Dim intState As Integer
Dim strStateName As String
Dim intStateValue As Integer
Dim intColourLookup As Integer
Dim rngStates As Range
Dim rngColours As Range
Set rngStates = Range(ThisWorkbook.Names("STATES").RefersTo)
Set rngColours = Range(ThisWorkbook.Names("STATE_COLOURS").RefersTo)
With Worksheets("MainMap")
For intState = 1 To rngStates.Rows.Count
strStateName = rngStates.Cells(intState, 1).Text
intStateValue = rngStates.Cells(intState, 2).Value
' single colour
intColourLookup = Application.WorksheetFunction.Match(intStateValue, Range("STATE_COLOURS"), True)
With .Shapes(strStateName)
.Fill.Solid
.Fill.ForeColor.RGB = rngColours.Cells(intColourLookup, 1).Offset(0, 1).Interior.Color
End With
Next
End With
End Sub
以下是文件本身的链接:https://dl.dropboxusercontent.com/u/41007907/MapOfStates.xls
对于低于9的值,它可以正常工作,但我需要它才能工作到20。
答案 0 :(得分:1)
您的数组STATE_COLORS仅包含0到9区间内的值。以下是您需要执行的步骤: 1)打开excel文件 2)去Formulas Tag 3)单击名称管理器 4)选择STATE_COLORS数组 5)将值增加到20
如果您有任何其他问题,请回复我。