不考虑空白单元格
我已经写了一个代码,正是我想要的,除了它也改变了匹配的空白的颜色。 我想知道我可以添加到代码,所以空白单元格不标记。
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
在更改Cells
对象的Interior
属性的ColorIndex
之前,必须检查空的内容。
'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
必须匹配,所以可以用这样的事情来简化:
'If your cell isn't empty then change background color If(Cells(arownumber, change).Value <> "" And Cells(smrownumber, across).Value <> "") Then Cells(arownumber, change).Interior.ColorIndex = 3 Cells(smrownumber, across).Interior.ColorIndex = 3 End If
你是这个意思吗?
match = IIf((CellA = CellB) And (CellA <> ""), "yes", "no")
代替
match = IIf(CellA = CellB, "yes", "no")