尝试解决Range(xx),Interior.ColorIndex中无效的NULL使用问题

时间:2016-03-11 21:28:43

标签: excel null

我正在尝试在Excel中创建一个宏,它将找到一系列单元格来确定它们的颜色。

以下是我收到无效使用Null错误的代码:

Dim Result As Long
Result = Range("D8:N10").Interior.ColorIndex

电子表格:

image

单元格包含多位数字代码或可以为空白。 我试图确定整个范围是否是特定颜色(绿色),我将在另一个单元格中设置一个值。 单元格中还有其他多行,我可以通过它来运行,这是我遇到的第一个产生错误的工作表。

1 个答案:

答案 0 :(得分:0)

这是我的回答

Sub findCellColors()
    Dim i
    Dim d
    Dim Sets As Range 'the range where to set the colors to find
    Dim myData As Range 'the range of the data

    Set Sets = Range("Sets") 'the Sets and MyData are range names
    Set myData = Range("MyData")

    For Each i In Sets 'for each cell in the range("Sets")...
        For Each d In myData 'for each cell in the range("MyData")... Do this ==>
            If d.Interior.Color = i.Interior.Color Or _
               d.Value = 1 Then

               '#######################################
               ' You will compare every cell inside the data, if
               ' is the same color of the sets range
               ' that why d.Interior.Color = i.Interior.Color [OR]
               ' if the value of the "d" cell (d is the var where you store every cell
               ' of the data range) value is 1, because the conditional formatting is sets
               ' that way,(see the pictue 1)
               '#######################################

                i.Offset(0, 1).Value = i.Offset(0, 1).Value & " " & d.Address
                'here I just store the address of the colored cells, but you can do whatever you want...
            End If
        Next d
    Next i
End Sub

好吧,在代码中你会看到评论。

我假设有两件事:

1)你或其他人,或一个宏,为细胞着色,现在你想找到那些颜色(现在就像颜色代码一样了解)

2)这些颜色是由条件格式设置的,然后在我的代码中,我在OR之后设置条件,如您所见。如果我的猜测是真的(条件格式化思考)那么你需要在其他单元格中设置相同的条件,而不是检查颜色,因为单元格没有真正着色,你看到的颜色,但没有设置为属性细胞,只是格式化,让你看到。

看到这张图片说明:

enter image description here

以下是此代码的数据:

enter image description here

如果您只有条件格式,那么您可以使用:

Sub findCellColors2()
    Dim d
    Dim myData As Range 'the range of the data

    Set myData = Range("MyData")

        For Each d In myData 'for each cell in the range("MyData")... Do this ==>
            'Here we are not validating anything of the range Sets,
            'we are using it as a reference, to store the data...
            Select Case d.Value 'Mr. VBA please check the value of d
                Case 1 'then in case the value of d were 1
                    Range("O2").Value = Range("O2").Value & " " & d.Address
                Case 4
                    Range("O3").Value = Range("O3").Value & " " & d.Address
                Case 7
                    Range("O4").Value = Range("O4").Value & " " & d.Address
                Case 8
                    Range("O5").Value = Range("O5").Value & " " & d.Address
                Case Else
                    'do nothing or whatever you need.
            End Select 'this is equal to the conditional formating
        Next d
End Sub

enter image description here

Selece Case检查是否有单元格查看是否具有您需要验证的值,就像使用条件格式一样。

在这里你可以看到结果:

enter image description here

今天一切都好......如果你需要一些改进,请告诉我,我会尝试放大信息。