0

Excel VBA:我的任务是根据文本文件中给出的输入修改几列的背景颜色。我能够使用十六进制颜色来做到这一点。现在我需要支持简单英语的颜色。文本文件现在将包含如下数据

columnname1,red
columnname2,green

所以我必须将这个“红色”映射到 colorConstant 数字 3 以使其工作。如果我需要允许所有支持的颜色 在此处检查,我是否需要为所有颜色编写 IF ELSE。有一个简单的方法吗?

4

1 回答 1

0

您可以使用Collection来存储映射:

Public indexOf As Collection

Sub map(color As String, index As Integer)
    Call indexOf.Add(index, color)
End Sub

Sub init()
    Set indexOf = New Collection

    map "red", 3
    map "green", 4
    map "blue", 5
End Sub

Sub test()
    init

    [A1].Interior.ColorIndex = indexOf("red")
    [A2].Interior.ColorIndex = indexOf("green")
    [A3].Interior.ColorIndex = indexOf("blue")
End Sub
于 2012-12-13T22:10:17.490 回答