【问题标题】:Filter function getting compile error过滤函数得到编译错误
【发布时间】:2017-11-08 16:52:15
【问题描述】:

我正在尝试使用过滤功能删除列表中的所有工作表not 但我得到了错误

Compile error: Wrong number of arguments or invalid property assignment

我看不出有什么问题

感谢您对此的任何帮助

Sub DelShtsNotInList()
Dim Arr
Dim Sht As Worksheet

Arr = Array("A", "B", "C")

Application.DisplayAlerts = False
    For Each Sht In Worksheets
        If Not UBound(filter(Arr, "A", True, vbTextCompare)) >= 0 Then 
           Sht.Delete
        End if
    Next Sht
Application.DisplayAlerts = True

End Sub

【问题讨论】:

    标签: vba excel filter


    【解决方案1】:

    我认为编译错误来自其他一些SubFunction,这个应该没问题。但是,请尝试这样:

    Sub DelShtsNotInList()
    
        Dim Arr As Variant
        Dim Sht As Worksheet
    
        Arr = Array("A", "B", "C")
    
        Application.DisplayAlerts = False
        For Each Sht In Worksheets
            If Not UBound(Filter(Arr, Sht.Name, True, vbTextCompare)) >= 0 Then
                If Worksheets.Count = 1 Then
                    MsgBox "Error is coming"
                    Exit Sub
                End If
                Sht.Delete
            End If
        Next Sht
        Application.DisplayAlerts = True
    
    End Sub
    

    如果你尝试删除最后一个工作表,if 会给你一个 msgbox。

    【讨论】:

    • Vityata,无论我做什么,filter 都会突出显示blue,我收到错误消息。知道什么会导致这种情况吗?谢谢。我正在使用 excel 2016
    • @Tim - 不,对不起。如果您只是创建一个新工作簿并运行它会发生什么?
    • 你称它为I think that the compile error comes from some other Sub or Function 我重命名了函数现在一切正常。谢谢
    【解决方案2】:

    这似乎工作得很好:

    Sub DelShtsNotInList()
        Dim Arr
        Dim Sht As Worksheet
    
        Arr = Array("A", "B", "C")
    
        Application.DisplayAlerts = False
        For Each Sht In Worksheets
            If Not UBound(Filter(Arr, Sht.Name, True, vbTextCompare)) >= 0 Then
                Sht.Delete
            End If
        Next Sht
        Application.DisplayAlerts = True
    
    End Sub
    

    注意:请注意,您不能删除最后一个工作表,因此如果所有名称都匹配,则会发生运行时错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-26
      • 2021-06-27
      • 2011-05-29
      • 2014-10-21
      • 1970-01-01
      • 2015-09-07
      • 2014-05-09
      • 2015-05-05
      相关资源
      最近更新 更多