I have watched a video on that topic at this link
https://www.youtube.com/watch?v=ODh_PXOljPU" onclick="window.open(this.href);return false;
and I have written the code and changed a little so as to make it faster for the sub called "FillFilters" but it still takes some time
In standard module
Code: Select all
Sub FillFilters()
Dim arr As Variant
Dim oRange As Range
Dim bDone As Boolean
Dim sFilter As String
Dim sTemp As String
Dim i As Long
Dim r As Long
Dim c As Long
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("A1") = "Filters" Then GoTo Skipper
Sheet1.Range("A2").EntireColumn.Insert
Set oRange = Sheet1.Range("A2").CurrentRegion
With oRange
.Cells.EntireColumn.Hidden = False
.Cells(1, 1) = "Filters"
For r = 2 To .Rows.Count
For c = 3 To Columns.Count
If WorksheetFunction.CountIf(Range(Cells(r, 3), Cells(r, c)), Cells(r, c)) = 1 Then
sFilter = sFilter & "," & Cells(r, c)
End If
Next c
arr = Split(sFilter, ",")
Do
bDone = True
For i = 1 To UBound(arr) - 1
If arr(i) > arr(i + 1) Then
bDone = False: sTemp = arr(i): arr(i) = arr(i + 1): arr(i + 1) = sTemp
End If
Next i
Loop While bDone = False
sFilter = Join(arr, ",")
With .Cells(r, 1).Validation
.Delete
.Add Type:=xlValidateList, Formula1:="-" & sFilter & ",Blanks"
.InCellDropdown = True
End With
sFilter = ""
Next r
.Cells.EntireColumn.Hidden = False
End With
Skipper:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Sub DeleteFilters()
Application.ScreenUpdating = False
If Range("A1") = "Filters" Then
Cells.EntireColumn.Hidden = False
Range("A1").EntireColumn.Delete
Range("A1").Select
End If
Application.ScreenUpdating = True
End Sub
Sub Filtering(oFilter As Range)
Dim oRange As Range
Dim sCell As String
Dim r As Long
Dim c As Long
Dim iRow As Long
Dim iCount As Long
Application.ScreenUpdating = False
Set oRange = Sheet1.Range("A2").CurrentRegion
sCell = oFilter.Value
If sCell = "Filters" Then oRange.Cells.EntireColumn.Hidden = False: Exit Sub
If sCell = "" Then oRange.Cells.EntireColumn.Hidden = False: Exit Sub
If sCell = "-" Then oRange.Cells.EntireColumn.Hidden = False: Exit Sub
If sCell = "Blanks" Then oRange.Cells.EntireColumn.Hidden = False: sCell = ""
oRange.Cells.EntireColumn.Hidden = False
iRow = oFilter.Row
With oRange
For c = 3 To .Columns.Count
If .Cells(iRow, c) <> sCell Then
.Cells(iRow, c).EntireColumn.Hidden = True
Else
iCount = iCount + 1
End If
Next c
End With
Application.ScreenUpdating = True
MsgBox iCount & " Columns For Row " & iRow
End Sub
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") < "Filters" Then Exit Sub
If Target Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If Target.Column = 1 Then Filtering Target
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("A1") < "Filters" Then Exit Sub
If Target Is Nothing Then Exit Sub
If Target.Column <> 1 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
Filtering Target
End Sub