0

以下是我到目前为止的代码:

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
4

1 回答 1

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% 的情况下是不必要的。

因为您正在寻找“如果这些单元格中没有一个包含...”,我建议在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
于 2013-05-15T14:06:32.567 回答