Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA添加自动筛选,如果它没有';不存在_Excel_Excel 2010_Vba - Fatal编程技术网

Excel VBA添加自动筛选,如果它没有';不存在

Excel VBA添加自动筛选,如果它没有';不存在,excel,excel-2010,vba,Excel,Excel 2010,Vba,如何检查某个范围是否已经有自动过滤器,如果没有,如何应用这些过滤器 目前我只是在使用 Range("A1:N1").AutoFilter 但是,如果该范围已启用过滤器,则会将其关闭 我对此进行了搜索,找到了许多清除和重置自动筛选的解决方案,但没有一个是关于实际检查是否实际应用了筛选。我只是在重新应用之前关闭了自动筛选,而不是检查 Sheets(curSheet).AutoFilterMode = False Range("A1:N1").AutoFilter 您当前的解决方案应该可以正常工作

如何检查某个范围是否已经有自动过滤器,如果没有,如何应用这些过滤器

目前我只是在使用

Range("A1:N1").AutoFilter
但是,如果该范围已启用过滤器,则会将其关闭


我对此进行了搜索,找到了许多清除和重置自动筛选的解决方案,但没有一个是关于实际检查是否实际应用了筛选。

我只是在重新应用之前关闭了自动筛选,而不是检查

Sheets(curSheet).AutoFilterMode = False
Range("A1:N1").AutoFilter

您当前的解决方案应该可以正常工作,但您可以使用If语句,如

If Sheets(curSheet).AutoFilterMode = True Then

'Do Nothing

Else

Sheets(curSheet).Range("A1").AutoFilter

End If

这里有一个简短的解决方案,仅当自动过滤器尚未安装到位时才打开它

If Not Sheets(curSheet).AutoFilterMode Then Range("A1:N1").AutoFilter
优点:只有在没有自动过滤器的情况下才会发生

缺点:仅当通过代码设置自动过滤器时才应使用,因为用户可能在不同的范围内设置了过滤器。

或在一行中进行设置:

If Worksheets("Sheet1").AutoFilterMode = False Then Range("a1").AutoFilter
U

sing the  if false method ... leaves the filter and header validations untouched...

DoFixValid "ShellManycl", "g4:r4" 
'puts as headings 'validation drop downs for the Get of the class  


Private Sub CommandButton2_Click()
   Dim Ra As Range
   Application.ScreenUpdating = False
   Set Ra = Range("f5").CurrentRegion
   Ra(2, 2).Resize(Ra.Rows.Count, Ra.Columns.Count).Clear
 ' clear all except filter and validation Headings
   URaAdd = ActiveSheet.UsedRange.Address  ' tidy used range
   [j1] = Timer
   DoRaShell Range("F5")  ' ' get the data   below headings
   [j2] = Timer - [j1]
   Application.ScreenUpdating = True
   Set Ra = Range("f5").CurrentRegion
   If Not AutoFilterMode Then Ra.AutoFilter  n
'not touch filter values of heanings

End Sub
' Maybe some may be interested in adding  validation to a range

'or to get the Public Property Gets ( no param ) from a class module
'or both from a module


 ' needs reference to Microsoft VBE extensibility pack

Option Explicit: Option Compare Text
Public VRa As Range, VFormula$, VTitle$, VMsG$



Sub DoFixValid(ClassName$, RaAdd$)
   FixGetValidation ClassName
   ValidateForRange Range(RaAdd), VFormula

End Sub

Sub FixGetValidation(ComponentName$)

   Dim Li%, PP%, EL%, LineStr$, PosGet&, PA
   VFormula = ""
   With ActiveWorkbook.VBProject.VBComponents(ComponentName).CodeModule

      For Li = .CountOfDeclarationLines To .CountOfLines
         LineStr = .Lines(Li, 1)
         PosGet = InStr(LineStr, "rty Get ")
         If PosGet > 2 Then
            If InStr(LineStr, "Private") = 0 Then
               LineStr = Mid(LineStr, PosGet + 8)
               LineStr = Left(LineStr, InStr(LineStr, "(") - 1)
               If InStr("!@#$%&", Right(LineStr, 1)) Then LineStr = Left(LineStr, Len(LineStr) - 1)
               VFormula = VFormula & "," & LineStr
            End If
         End If
      Next Li
   End With
End Sub

Sub ValidateForRange(Ra As Range, ValidFormula$, _
Optional Title$ = "For List columns ", Optional MsG$ = " Select from drop down list")
   Ra.Select
   With Selection.Validation
      .Delete
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
      xlBetween, Formula1:=ValidFormula
      .IgnoreBlank = True
      .InCellDropdown = True
      .InputTitle = Title
      .ErrorTitle = Title
      .InputMessage = MsG
      '    .ErrorMessage = ValidFormula
      .ShowInput = True
      .ShowError = True
   End With
End Sub