简体   繁体   English

检查范围内的所有单元格是否包含相同的值

[英]check if all cells in a range contain same value

Can you help me with this Please, I'm trying to check if from range ("L2") to the end if result = "-" pop up a msgbox & colorize the range.你能帮我解决这个问题吗?我正在尝试检查是否从范围(“L2”)到结尾,如果结果 =“-”弹出一个 msgbox 并为范围着色。 the conditions is all the cells value in the range horizontally must be = "-"条件是水平范围内的所有单元格值必须为 = "-"

Example of what I mean:我的意思的例子:

数字

I try to the below code but it's colorized all the value ("-") in the range我尝试使用下面的代码,但它对范围内的所有值(“-”)进行了着色

Sheets("Cumulated BOM").Activate
Dim i As Long
Dim c As Long
Dim myRange As Range
Dim myCell As Range


Set myRange = Range("L2", Range("L" & Row.Count).End(xlUp))

For Each myCell In myRange
    c = c + 1
    If (myCell) = "-" Then
        myCell.Interior.Color = RGB(255, 87, 87)
        i = i + 1
    End If
Next myCell

Highlight Rows With All Their Cells Containing the Same Value突出显示所有单元格包含相同值的行

Option Explicit

Sub HighlightInvalidRows()

    ' Prepare.

    ' Reference the workbook ('wb').
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' Reference the worksheet ('ws').
    Dim ws As Worksheet: Set ws = wb.Worksheets("Cumulated BOM")
    
    ' Reference the range ('rg').
    Dim rg As Range
    Set rg = ws.Range("L2", "S" & ws.Cells(ws.Rows.Count, "L").End(xlUp).Row)
    
    ' Write the number of columns of the range to a variable ('CellsCount').
    Dim CellsCount As Long: CellsCount = rg.Columns.Count
    ' Each row of the range has this number of columns (cells).
    
    ' Remove all range colors.
    rg.Interior.Color = xlNone
    
    ' Combine the rows ('rrg') to be highlighted
    ' into the Highlight range ('hrg').
    
    ' Declare variables that appear for the first time in the following loop.
    Dim hrg As Range
    Dim rrg As Range
    Dim MatchCount As Long
    
    ' Loop through the rows of the range.
    For Each rrg In rg.Rows
        ' Write the number of appearances of the value in the current row
        ' to a variable ('MatchCount').
        MatchCount = Application.CountIf(rrg, "-")
        ' Compare the match count with the cells count.
        If MatchCount = CellsCount Then ' the numbers are equal
            ' Combine the current row into the highlight range.
            If hrg Is Nothing Then ' the first match
                Set hrg = rrg
            Else ' all other matches
                Set hrg = Union(hrg, rrg)
            End If
        'Else ' the numbers are not equal; do nothing
        End If
    Next rrg
        
    ' Highlight the rows (in one go) and inform.
    
    If hrg Is Nothing Then ' no matches found
        MsgBox "No invalid rows found.", vbInformation
    Else ' matches found
        hrg.Interior.Color = RGB(255, 87, 87)
        MsgBox "Invalid rows highlighted.", vbExclamation
    End If
    
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将范围定义为所有包含特定值的单元格 - Define a range as all cells that contain a certain value 将值添加到一系列单元格中,这些单元格的相邻单元格包含相同的值 - Add the values in a range of cells, whose adjacent cells contain same value 如何检查所有单元格区域是否都具有值? - How to check if a range of cells all have a value? 如果范围中的任何单元格包含该值,宏检查某些值的单元格区域是否隐藏行? - Macro to check range of cells for certain value to hide rows if any cells in the range contain that value? 如何检查范围内的所有非空白单元格都具有相同的值? - How to check that all non-blank cells in range have same value? 当范围内的所有像元都等于相同值时更改像元的值 - Changing the value of a cell when all cells in a range equal the same value 如果一个范围内的所有单元格都相同,不包括空白单元格 - IF all cells in a range are the same, excluding blank cells 检查单元格一次是否包含值 - Check if Cells contain value exactly one time 突出显示另一个单元格中包含值的所有单元格 - Highlight all cells that contain a value in another cell 如何检查单元格是否包含多个值之一,如果这些列都包含任一值,则如何更改另一个单元格 - How can I check if cells contain one of several values and change another cell if those columns all contain either value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM