如果条件在宏中变为false,则计算数组中的下一个值

时间:2016-11-30 12:24:24

标签: arrays excel vba excel-vba

数组中有3个值。其中,我过滤数据的列中没有1个值(" matang")。我想知道如果该关键字没有结果,我应该如何跳过它。这意味着在使用该关键字过滤后,不会显示任何结果。我想跳过该关键字并转到数组的下一个元素。我试过On Error Resume Next。那么还有其他选择吗?

    Dim Ar() As Variant
    Ar() = Array("jumpsuit", "matang", "bikini")
    Dim i As Variant
    For Each i In Ar
    Sheets("tops").Select
    ActiveSheet.Range("B1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$D$1335").AutoFilter Field:=2, Criteria1:="*" & i & "*", Operator:=xlAnd
    ActiveCell.Offset(0, 1).Range("A1").Select
    Sheets("Sheet1").Select
    Cells.Find(What:=i, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Range("A1").Select
    Selection.Copy
    Selection.End(xlUp).Select
    Sheets("tops").Select
    Do
    ActiveCell.Offset(1, 0).Select
    Loop While ActiveCell.EntireRow.Hidden = True
    ActiveSheet.Paste

3 个答案:

答案 0 :(得分:0)

你可以在那里添加一个CountIf位逻辑来测试数组的第i个元素是否存在于范围内?如果它不存在(即= 0),那么跳过可以进行更改的代码部分?在我的例子中,我使用了“skip”这个词。就这样....

Dim Ar() As Variant
Ar() = Array("jumpsuit", "matang", "bikini")
Dim i As Variant
For Each i In Ar
Sheets("tops").Select
ActiveSheet.Range("B1").Select

If Application.WorksheetFunction.CountIf(ActiveSheet.Range("$A$1:$D$1335"), i) = 0 Then

    GoTo Skip

End If

Selection.AutoFilter
ActiveSheet.Range("$A$1:$D$1335").AutoFilter Field:=2, Criteria1:="*" & i & "*", Operator:=xlAnd
ActiveCell.Offset(0, 1).Range("A1").Select
Sheets("Sheet1").Select
Cells.Find(What:=i, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Range("A1").Select
Selection.Copy
Selection.End(xlUp).Select
Sheets("tops").Select
Do
ActiveCell.Offset(1, 0).Select
Loop While ActiveCell.EntireRow.Hidden = True
ActiveSheet.Paste

然后把'skip:'这个术语放在下一个之前的某个地方(我在你的代码中看不到它,是不是因为这只是一个较大子的一部分?)

答案 1 :(得分:0)

创建一个函数,在将它们放入工作表后检查具有所需值的列表。

Function checking(value as string)
Dim x as integer
Dim numWantedvalues as Integer

numWantedvalues = WorksheetFunction.CountA(ThisWorkbook.Sheets("Sheet2").Range("A:A"))

For x = 1 to numWantedvalues
    If ThisWorkbook.Sheets("Sheet2").Range("A" & x) = value Then
        checking = True
    End If
Next x
end function

然后在代码中引入该函数:

If checking(arValue) = True Then
    'Actions that you want to do
End If

答案 2 :(得分:0)

尝试将自动过滤后的所有内容包装在测试中,但不会产生任何结果:

If ActiveSheet.AutoFilter.Range.Columns(2).SpecialCells(xlCellTypeVisible).Count - 1 > 0 Then

即:

Dim Ar() As Variant
Ar() = Array("jumpsuit", "matang", "bikini")
Dim i As Variant
For Each i In Ar
    Sheets("tops").Select
    ActiveSheet.Range("B1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$D$1335").AutoFilter Field:=2, Criteria1:="*" & i & "*", Operator:=xlAnd
   If ActiveSheet.AutoFilter.Range.Columns(2).SpecialCells(xlCellTypeVisible).Count - 1 > 0 Then
        ActiveCell.Offset(0, 1).Range("A1").Select
        Sheets("Sheet1").Select
        Cells.Find(What:=i, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Range("A1").Select
        Selection.Copy
        Selection.End(xlUp).Select
        Sheets("tops").Select
        Do
        ActiveCell.Offset(1, 0).Select
        Loop While ActiveCell.EntireRow.Hidden = True
        ActiveSheet.Paste
    End If
Next i