Private Sub CommandButton1_Click()
ActiveSheet.AutoFilterMode = flase
Range("A1:A25").AutoFilter
Range("A1:A25").AutoFilter Field:=1, Criteria1:="Apple"
End Sub
Autofilter Add or Remove
Private Sub CommandButton1_Click()
Range("A1").AutoFilter
ActiveSheet.AutoFilterMode = False
Range("A1").AutoFilter
Range("A1").AutoFilter
End Sub
Set Autofilter to a Range
Private Sub CommandButton1_Click()
ActiveSheet.AutoFilterMode = False
Dim filterrange As Range
Set filterrange = Range(("A1"), Cells(Rows.Count, 1).End(xlUp))
filterrange.AutoFilter field:=1, Criteria1:="Apple"
End Sub
Add or Remove Autofilter
Private Sub CommandButton1_Click()
'add autofilter
Range("A1").AutoFilter
End Sub
Private Sub CommandButton2_Click()
'remove autofilter
'Range("A1").AutoFilter
ActiveSheet.AutoFilterMode = False
End Sub
AutoFilter - Top 10
Private Sub CommandButton1_Click()
Criteria1:="96", _
Operator:=xlTop10Items
End Sub
AutoFilter - Select Multiple Values
Private Sub CommandButton1_Click()
Range("A1").AutoFilter
Range("A1").AutoFilter field:=2, _
Criteria1:=Array("Apple", "Banana"), _
Operator:=xlFilterValues
End Sub
AutoFilter - OR Operator
Private Sub CommandButton1_Click()
Range("A1").AutoFilter
Criteria1:="Apple", _
Operator:=xlOr, _
Criteria2:="Banana"
End Sub
AutoFilter - AND Operator _ Alpha
Private Sub CommandButton1_Click()
Range("A1").AutoFilter
Operator:=xlAnd, Criteria2:="<=C"
End Sub
AutoFilter - AND Operator - Numbers
Private Sub CommandButton1_Click()
Range("A1").AutoFilter
Operator:=xlAnd, Criteria2:="<=100"
End Sub
Autofilter Vs Sort
Retrieve Last Month values
Private Sub CommandButton1_Click()
Range("A1").AutoFilter
Range("A1").AutoFilter Field:=7, _
Criteria1:=xlFilterLastMonth, _
Operator:=xlFilterDynamic
End Sub
Autofilter - Defining Particular Rage
Private Sub CommandButton1_Click()
ActiveSheet.Range("$A$1:$F$100").AutoFilter Field:=2, Criteria1:="Banana"
End Sub
Autofilter - Sort Syntax
XLNo(default)
XLYes
True = For case sensitive
False: No case sensititve
XLSortColumns: Sort by columns
XLSortRows: Sort by Rows
Private Sub CommandButton1_Click()
'To remove the existing sorting
Sheets("Sheet2").Sort.SortFields.Clear
Sort.SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, _
Order:=xlAscending
With ThisWorkbook.Sheets("Sheet2")
Sort.SetRange Range("A1").CurrentRegion
.Sort.Header = xlYes
.Sort.MatchCase = False
.Sort.Orientation = xlSortColumns
.Sort.Apply
End With
End Sub
Private Sub CommandButton2_Click()
'To remove the existing sorting
Sheets("Sheet2").Sort.SortFields.Clear
Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending
With ThisWorkbook.Sheets("Sheet2")
Sort.SetRange Range("A1").CurrentRegion
.Sort.Header = xlYes
.Sort.MatchCase = False
.Sort.Orientation = xlSortColumns
.Sort.Apply
End With
End Sub
Autofilter - Sort Ascending Order - If Condition
Private Sub CommandButton1_Click()
Dim j As Boolean
Q = 2
Do
j = False
For i = 2 To 20
If Cells(i, 5).Value > Cells(i + 1, 5).Value Then
Cells(i, 6).Value = Cells(i, 5).Value
Cells(i, 5).Value = Cells(i + 1, 5).Value
Cells(i + 1, 5).Value = Cells(i, 6).Value
End If
j = True
Next
Q = Q + 1
Loop While j
End Sub
Sorting values for Specified Range
Private Sub CommandButton1_Click()
'To remove the existing sorting
Sheets("Sheet2").Sort.SortFields.Clear
'Sheets("Sheet2").Sort.SortFields.Clear
Sort.SortFields.Add Key:=Range("B25"), SortOn:=xlSortOnValues, _
Order:=xlAscending
With ThisWorkbook.Sheets("Sheet2")
Sort.SetRange Range("A25:F85").Rows
.Sort.Header = xlYes
.Sort.MatchCase = False
.Sort.Orientation = xlSortColumns
.Sort.Apply
End With
End Sub
Display Sort Dialogue Box
Private Sub CommandButton1_Click()
Application.Dialogs(xlDialogSort).Show
End Sub
Sort based on custom list
Private Sub CommandButton1_Click()
Application.AddCustomList listarray:=(Range("I2:I4"))
Range("A5:G20").Sort key1:=Range("B5"), Order1:=xlDescending, Header:=xlYes
Application.DeleteCustomList Application.CustomListCount
End Sub
Sort Every 5 Rows
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 2 To 26 Step 5
Range(Cells(i, 1), Cells(i + 4, 7)).Sort _
key1:=Cells(i + 4, 3), _
order1:=xlAscending, _
Header:=xlNo
Range(Cells(i, 1), Cells(i + 4, 7)).Select
Next
End Sub
Advance Filter - Multiple Conditions
XlFilterInPlace: to place in same location
XlFilterCopy: to place in some other location
Advance Filter - Example
Private Sub CommandButton1_Click()
Range("A1").CurrentRegion.AdvancedFilter _
Action:=xlFilterCopy, _
criteriarange:=Range("I1:J3"), _
copytorange:=Sheets("Sheet3").Range("A1"), _
Unique:=False
End Sub
Advance Filter - Remove Duplicates
Private Sub CommandButton1_Click()
Dim r As Range
Set r = Range("A1:A150")
r.AdvancedFilter Action:=xlFilterCopy, copytorange:=Range("D1"), unique:=True
End Sub