首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无视空白细胞

无视空白细胞
EN

Stack Overflow用户
提问于 2017-11-30 19:11:25
回答 2查看 48关注 0票数 0

我已经编写了一个代码,它完全符合我的要求,但它也改变了匹配空白的颜色。我想知道我能在代码中添加什么,这样空白单元格就不会被标记。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-30 19:14:30

在更改ColorIndex对象的Interior属性之前,必须检查是否为空内容。

代码语言:javascript
复制
'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必须在执行条件之前进行匹配,所以可以使用以下内容来简化事情:

代码语言:javascript
复制
'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
票数 1
EN

Stack Overflow用户

发布于 2017-11-30 19:17:02

你是说像这样?

代码语言:javascript
复制
match = IIf((CellA = CellB) And (CellA <> ""), "yes", "no")

而不是

代码语言:javascript
复制
match = IIf(CellA = CellB, "yes", "no")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47580945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档