Code Check

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Hi, what is wrong with this code i get application defined object errror


With Sheets("SecuritiesReport").Range("A5")
.AutoFilter Field:=4, Criteria1:="=DOMCORP", Operator:=xlOr, Criteria2:="=TEMP", _
Operator:=xlOr, Criteria3:="=UNDADMIN", Operator:=xlOr, Criteria4:="=RIGHTS", _
Operator:=xlOr, Criteria5:="=SUBMVMNT"
End With
 
One more improvement for the seldom case then Rng is the single cell:
Rich (BB code):

Option Explicit
Option Compare Text

' ZVI:2011-07-06 http://www.mrexcel.com/forum/showthread.php?t=561925
' Filter Rng-range by Criteria
' Arguments:
'   Rng      - filtered range without header
'   Criteria - (optional) filter criteria(s), range_without_header)/value/array_of_values
' Note:
'   Missing of Criteria means ShowAllData in Rng-range
' VBA calls:
'   MyFilter Range("D11:D1000"), Range("D2:D5")
'   MyFilter Range("D11:D1000"), Range("DynamicRange")  NOTE: Don't include Header to DynamicRange
'   MyFilter Range("D11:D1000"), "DOMCORP,TEMP,UNDADMIN,RIGHTS"
'   MyFilter Range("D11:D1000"), Array("DOMCORP","TEMP","UNDADMIN","RIGHTS")
 Sub MyFilter(ByVal Rng As Range, Optional Criteria)
  Dim a, b, i As Long, r As Long, s As String, Sh As Worksheet, v, x
  On Error GoTo exit_
  Set Sh = Rng.Parent
  Set Rng = Intersect(Sh.UsedRange, Rng)
  If IsMissing(Criteria) Then Rng.EntireRow.Hidden = False: Exit Sub
  a = Rng.Columns(1).Value
  If Not IsArray(a) Then
    ReDim a(1 To 1, 1 To 1)
    a(1, 1) = Rng.Value
  End If
  b = Criteria: If Not IsArray(b) Then b = Split(b, ",")
  r = Rng.Row
  Application.ScreenUpdating = False
  Rng.EntireRow.Hidden = True
  With Sh
    For i = 1 To UBound(a)
      v = a(i, 1)
      If VarType(v) = vbString Then v = Trim(v)
      For Each x In b
        If x = v Then
          s = s & r & ":" & r
          If Len(s) >= 240 Then
            .Range(s).EntireRow.Hidden = False
            s = ""
          Else
            s = s & ","
          End If
          Exit For
        End If
      Next
      r = r + 1
    Next
    If Len(s) > 1 Then .Range(Left(s, Len(s) - 1)).EntireRow.Hidden = False
  End With
exit_:
  Application.ScreenUpdating = True
  If Err Then MsgBox Err.Description
End Sub

Sub SetMyFilter1()
  MyFilter Range("D11:D1000"), Range("D2:D5")
End Sub

Sub SetMyFilter2()
  MyFilter Range("D11:D1000"), "DOMCORP,TEMP,UNDADMIN,RIGHTS"
End Sub

Sub SetMyFilter3()
  MyFilter Range("D11:D1000"), Array("DOMCORP", "TEMP", "UNDADMIN", "RIGHTS")
End Sub

Sub SetMyFilter4()
  MyFilter Range("D11:D1000"), "DOMCORP"
End Sub

Sub SetMyFilter5()
  MyFilter Range("D11:D11"), "TEMP"
End Sub

' Show all data in range
Sub ResetMyFilter()
  MyFilter Range("D11:D1000")
End Sub
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
One more improvement for the seldom case then Rng is the single cell:

Vladimir this is great. Can we Call this filter in Thisworkbook module so that when the user saves the workbook, the code will call this filter and apply the filter. The workbook will be shared.

Sub SetMyFilter3()
MyFilter Range("D11:D1000"), Array("DOMCORP", "TEMP", "UNDADMIN", "RIGHTS")
End Sub
 
Upvote 0
Can we Call this filter in Thisworkbook module so that when the user saves the workbook, the code will call this filter and apply the filter.
Try below code in ThisWorkbook module:
Rich (BB code):

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  MyFilter Sheets(1).Range("D11:D1000"), "DOMCORP,TEMP,UNDADMIN,RIGHTS"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top