简体   繁体   English

如果范围中的任何单元格包含该值,宏检查某些值的单元格区域是否隐藏行?

[英]Macro to check range of cells for certain value to hide rows if any cells in the range contain that value?

Below is the code I have so far: 以下是我到目前为止的代码:

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

What I need is a macro to go below all of this to loop through range B4:L4 and check each cell to see whether it ends with an X or not. 我需要的是一个宏来低于所有这些循环通过范围B4:L4并检查每个单元格以查看它是否以X结尾。 This row will contain any combination of the following numbers/text: 1300, 2000, 2000X, 2500, 2500X, 3000, 3000X, 4500, 6000, 7000, 9000. I need to say if none of these cells end in X then hide or delete certain rows. 此行将包含以下数字/文本的任意组合:1300,2000,2000X,2500,2500X,3000,3000X,4500,6000,7000,9000。我需要说明如果这些单元格中没有一个以X结尾然后隐藏或删除某些行。 I've been trying to use If Not Like "*X" unsuccessfully. 我一直试图使用If Not Like“* X”失败。 Below is the code I have tried so far which has been unsuccessful. 下面是我到目前为止尝试过的代码,但是没有成功。 Any help is much appreciated. 任何帮助深表感谢。

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

This is the altered code I am trying to use to hide rows that contain all 0's but it is hiding the rows whether they contain all 0's or if they contain 2 columns with a 0 then a column with a 4. Please advise. 这是我试图用来隐藏包含所有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

The LIKE function is tricky, and probably not what you want since it's fuzzy, anyways. LIKE函数很棘手,可能不是你想要的,因为它是模糊的,无论如何。

Use the RIGHT function: 使用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

I also revise the above code to avoid Selection , which is unnecessary about 99% of the time. 我还修改了上面的代码以避免Selection ,这在99%的时间内是不必要的。

Because you're looking for an "If NONE of these cells contain..." I would suggest the following additional revision, to Exit the loop once the condition is met. 因为你正在寻找“如果没有这些单元格包含......”我建议进行以下额外修订,一旦条件满足就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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM