Excel VBA复制单元格颜色错误

时间:2014-04-18 10:00:38

标签: excel vba excel-vba

我有一个简单的功能来复制具有不同范围内相似内容的单元格的背景颜色(一个范围是failRange,另一个范围是toColor)

在分配Interior.Color的行中失败,而excel调试器根本没有给我任何信息,它只是停止。我已经分离出变量,因此我可以使用调试器轻松查看所有值,并且它们都设置得很好。

有没有人看到问题???

        Function ColorRange(failRange As Range, toColor As Range)
        Dim targetCell As Range
        Dim failCell As Range
        Dim targetValue As String
        Dim failValue As String
        Dim colorValue As Long
        Dim compareResult As Integer
        Dim counter As Integer

        For Each targetCell In toColor
            targetValue = Left(targetCell.Text, 7)

            For Each failCell In failRange
                failValue = failCell.Text
                compareResult = InStr(failValue, targetValue)
                If compareResult > 0 Then
                     colorValue = failCell.Interior.ColorIndex
                     rem next line causes failure
                     targetCell.Interior.ColorIndex = colorValue
                     counter = counter + 1
                     Exit For
                End If
            Next failCell

        Next targetCell
        ColorRange= counter
        End Function

1 个答案:

答案 0 :(得分:0)

从工作表单元格调用的 UDF 只能向该单元格返回值。它不会影响任何单元格格式。

如果您想更改格式,请使用