无视空白单元格

时间:2017-11-30 19:11:25

标签: excel vba excel-vba

我编写的代码完全符合我的要求,但它也会改变匹配空白的颜色。我想知道我可以添加到代码中,所以空白单元格没有标记。

beginrow = 2
First = 0
FirstLast = 0
Second = 0
SecondLast = 0

For z = 2 To finalrow
    If Cells(z, 9).Value = Cells(1 + z, 9).Value And Cells(z, 10).Value <> Cells(1 + z, 10).Value And Cells(z, 12).Value = Cells(1 + z, 12).Value Then
        First = z
        FirstLast = First + 1
    End If
    If Cells(z, 9).Value = Cells(1 + z, 9).Value And Cells(z, 12).Value <> Cells(1 + z, 12).Value Then
            Second = z
            SecondLast = Second + 1
            endKnown = True
    End If
    If endKnown = True Then
        For arownumber = beginrow To First 'need to find the rownumbers that we compare with
            For change = 4 To 7
                For smrownumber = FirstLast To Second 'need to find the rownumbers for comparing
                    For across = 4 To 7
                        CellA = Cells(arownumber, change)
                        CellB = Cells(smrownumber, across)
                        match = IIf(CellA = CellB, "yes", "no")
                        If match = "yes" Then
                            Cells(arownumber, change).Interior.ColorIndex = 3
                            Cells(smrownumber, across).Interior.ColorIndex = 3
                        End If
                    Next across
                Next smrownumber
            Next change
        Next arownumber
        endKnown = False
        If SecondLast <> 0 Then
            beginrow = SecondLast
        End If
      End If
 Next z

2 个答案:

答案 0 :(得分:1)

在更改react-intl对象的ColorIndex属性的Interior之前,您必须检查空内容。

Cells

由于'If your cell isn't empty then change background color If(Cells(arownumber, change).Value <> "") Then Cells(arownumber, change).Interior.ColorIndex = 3 End If 'If your cell isn't empty then change background color If(Cells(smrownumber, across).Value <> "") Then Cells(smrownumber, across).Interior.ColorIndex = 3 End If 必须在执行条件之前匹配,因此您可以使用以下内容简化操作:

Cells

答案 1 :(得分:1)

你的意思是这样吗?

match = IIf((CellA = CellB) And (CellA <> ""), "yes", "no")

而不是

match = IIf(CellA = CellB, "yes", "no")