簡體   English   中英

對於一系列單元格,首先找到具有某種顏色的單元格,然后對於這些單元格,查找是否有空白

[英]For a range of cells, first find the cells that are a certain color, then, for those cells, find if any are blank

我正在嘗試在 VBA 中為 Excel 編寫代碼,該代碼查看一系列單元格,在此示例中為 Range 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

我有點困惑,因為你說的是字體,然后是內部。 如果有黃色字體,那么必須有一個值,所以我假設你的意思是內部。 由於您只需要其中一個來滿足您的條件,因此您無需創建子范圍。 您可以測試以查看是否有任何單元格同時滿足這兩個條件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM