Excel VBA数据验证确实会在数字放置时更改颜色

时间:2017-09-13 12:52:45

标签: excel vba validation

如果我有除数字等之外的单词,以下代码可以正常工作......

  

optionList(0)=" cat"

     

optionList(1)=" dog"

     

optionList(2)=" cow"

如果我使用数字,列表会按预期生成,但不会改变颜色。

我没有收到任何错误消息。我在这里缺少什么?

Public Function addDataValidation(row As Long, column As Long)

Dim optionList(3) As String

optionList(0) = "1"
optionList(1) = "2"
optionList(2) = "3"

    With mySheet.Cells(row, column).Validation

        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, Formula1:=Join(optionList, ",")

    End With

   With mySheet.Cells(row, column).FormatConditions.Add(xlCellValue, xlEqual, "=""1""")
        .Font.Bold = True
        .Interior.ColorIndex = 4
        .StopIfTrue = False
    End With

    With mySheet.Cells(row, column).FormatConditions.Add(xlCellValue, xlEqual, "=""2""")
        .Font.Bold = True
        .Interior.ColorIndex = 6
        .StopIfTrue = False
    End With

    With mySheet.Cells(row, column).FormatConditions.Add(xlCellValue, xlEqual, "=""3""")
        .Font.Bold = True
        .Interior.ColorIndex = 3
        .StopIfTrue = False
    End With

mySheet.Cells(row, column).HorizontalAlignment = xlCenter

End Function

0 个答案:

没有答案