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

时间:2013-05-15 13:43:54

标签: excel excel-vba vba

以下是我目前的代码:

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为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

1 个答案:

答案 0 :(得分:2)

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%的时间内是不必要的。

因为你正在寻找“如果没有这些单元格包含......”,我建议在符合条件后{@ 1}}进行以下额外修订。

Exit
相关问题