【问题标题】:Macro to check range of cells for certain value to hide rows if any cells in the range contain that value?如果范围内的任何单元格包含该值,宏来检查单元格范围的某个值以隐藏行?
【发布时间】:2015-02-26 06:14:41
【问题描述】:

以下是我目前的代码:

Sub Compare()

Sheets("SCR SYSTEM SPECS").Select
Sheets("SCR SYSTEM SPECS").Copy
Dim WS As Excel.Worksheet
Dim ColumnCount As Long
Dim I As Long
Dim Cell As Excel.Range

Set WS = ActiveSheet    'adjust as necessary
ColumnCount = 12    'adjust as necessary
With WS
For I = ColumnCount To 1 Step -1
    Set Cell = .Cells(3, I)
    If Cell.Value = False Then
        Cell.EntireColumn.Delete
    End If
Next I
End With

ActiveSheet.Shapes.Range(Array("Button 1", "Check Box 1", "Check Box 2", _
    "Check Box 3", "Check Box 4", "Check Box 5", "Check Box 6", "Check Box 7", _
    "Check Box 8", "Check Box 9", "Check Box 10", "Check Box 11")).Select
Selection.Delete

End Sub

我需要一个宏来遍历所有这些以循环遍历范围 B4:L4 并检查每个单元格以查看它是否以 X 结尾。此行将包含以下数字/文本的任意组合:1300、2000、2000X、2500、2500X、3000、3000X、4500、6000、7000、9000。我需要说明这些单元格是否都以 X 结尾,然后隐藏或删除某些行。我一直在尝试使用 If Not Like "*X" 失败。以下是我迄今为止尝试过的代码,但没有成功。非常感谢任何帮助。

Dim MyCell, Rng As Range
Set Rng = Sheets("SCR SYSTEM SPECS").Range("B4:L4")
For Each MyCell In Rng
    If Not MyCell Like "*X" Then '''''will only do something if the cell is not blank
    Rows("4:18").Select
    Selection.EntireRow.Hidden = True
    'Else '''''if cell is equal to blank
    'Rows("4:18").Select
    'Selection.EntireRow.Hidden = False
    End If
Next

这是我试图用来隐藏包含所有 0 的行的更改代码,但它隐藏了这些行是否包含所有 0,或者它们是否包含 2 列和 0,然后是 4 列。请告知。

Dim MyCell2, Rng2 As Range
Set Rng2 = Sheets("SCR SYSTEM SPECS").Range("B36:L36")
For Each MyCell2 In Rng2
    If Right(Trim(MyCell2.Value), 1) = "0" Then
    Range("36:36").Select
    Selection.EntireRow.Hidden = True
    End If
Next

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    LIKE 函数很棘手,而且可能不是您想要的,因为它很模糊,无论如何。

    使用RIGHT函数:

    Sub TestThis()
    Dim MyCell, Rng As Range
    Set Rng = Sheets("SCR SYSTEM SPECS").Range("B4:L4")
    For Each MyCell In Rng
        If Right(Trim(MyCell.Value),1) = "X" Then '''''will only do something if the cell is not blank
            Rows("4:18").EntireRow.Hidden = True
            'Else '''''if cell is equal to blank
            'Rows("4:18").EntireRow.Hidden = False
        End If
    Next
    End Sub
    

    我还修改了上面的代码以避免Selection,这在大约 99% 的情况下是不必要的。

    因为您正在寻找“如果这些单元格中没有一个包含...”,我建议在满足条件后对Exit 循环进行以下附加修订。

    Sub TestThis()
    Dim MyCell, Rng As Range
    Set Rng = Sheets("SCR SYSTEM SPECS").Range("B4:L4")
    For Each MyCell In Rng
        If Right(Trim(MyCell.Value),1) = "X" Then '''''will only do something if the cell is not blank
            Rows("4:18").EntireRow.Hidden = True
            Exit For '## Exit the loop once the condition is met ##'
            'Else '''''if cell is equal to blank
            'Rows("4:18").EntireRow.Hidden = False
        End If
    Next
    End Sub
    

    【讨论】:

    • 感谢大卫的回复。但是,当我输入代码时,它会给我一条错误消息。它在 1 之前突出显示 , 并给出一个编译错误:Expected: Then or GoTo
    • 确定再试一次。有一个额外的括号。
    • If Right... 更改为If Not Right... OR= 运算符更改为<> ORTrue 更改为False在下一行 (Rows("4:18").EntireRow.Hidden = True)。任何一个都应该工作。
    • 只需使用 sum 函数,而不是循环遍历行中的所有单元格:If Application.WorksheetFunction.Sum(rng2) = 0 Then ...
    • 好的,然后尝试Application.WorksheetFunction.CountA(rng2),如果返回零,那么所有单元格都是空白的。
    猜你喜欢
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多