对于一定范围的单元格,首先找到某种颜色的单元格,然后对于那些单元格,找出是否有空白单元格

问题描述 投票:0回答:1

我正在尝试在VBA中为Excel编写代码,该代码查看一系列单元格,在此示例中为范围B4:B15,首先确定哪些单元格具有黄色填充颜色(内部颜色)。然后,在颜色为黄色的单元格中,确定这些单元格是否为空白。

[如果任何黄色单元格为空白,则在整个范围内显示一条消息,指出“有黄色单元格为空白”。

我正在使用For each rcell in r循环来确定哪些单元格是黄色的。

我如何只用黄色单元格建立一个新的“子范围”?

Sub Input_Checker_test()
    Dim ws As Worksheet
    Set ws = Sheets("Main")
    Dim r As Range
    Dim rcell As Range
    Dim rmain As Range
    Dim rmaincell As Range
    Set r = Range("B4:B15").Cells   
    For Each rcell In r   
        If rcell.Interior.Color = 65535 Then
            rcell = rmain
        End If
    Next rcell
    For Each rmaincell In rmain
        If WorksheetFunction.CountA(rmain) = 0 Then
            MsgBox ("Cells are empty")
        Else
            MsgBox ("Cells are full")
        End If     
    Next rmaincell           
End Sub
excel vba
1个回答
0
投票

我有些困惑,因为您说的是字体,然后是室内。如果有黄色字体,则必须有一个值,所以我假设您的意思是内部。由于只需要其中一个满足您的条件,就不需要创建子范围。您可以测试看看是否有任何一个单元格都符合两个条件。

Sub Input_Checker_test()

    Dim ws As Worksheet
    Set ws = Sheets("Main")

    Dim r As Range
    Dim rcell As Range
    Dim YellowCount as Integer
    Dim EmptyCount as Integer
    Set r = ws.Range("B4:B15")

    For Each rcell In r
        If rcell.Interior.Color = 65535 Then
            YellowCount = 1
            If IsEmpty(rcell) Then
                EmptyCount = 1
            End If
        End If
    Next rcell

    If YellowCount > 0 Then
        MsgBox "There are yellow cells"
    End If

    If EmptyCount > 0 Then
        MsgBox "There are empty cells"
    End If


End Sub
© www.soinside.com 2019 - 2024. All rights reserved.