Hide or Unhide the Rows by using Checkboxes
Public ReportSH As Worksheet
Function ActivateReportWorksheet()
Set ReportSH = ThisWorkbook.Sheets("Report")
ReportSH.Activate
End Function
Sub SortMultipleColumns()
'www.Tricks12345.com
On Error Resume Next
Application.DisplayAlerts = False
ThisWorkbook.Sheets("Report").Delete
For N = 1 To ThisWorkbook.Names.Count
'MsgBox ThisWorkbook.Names(N).Name
ThisWorkbook.Names(N).Delete
Next
Dim InputSH As Worksheet
Set InputSH = ThisWorkbook.Sheets("Input")
ThisWorkbook.Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Report"
ActivateReportWorksheet
'Key denotes about first sort field
InputSH.Range("A1").CurrentRegion.Copy ReportSH.Range("B1")
ReportSH.Sort.SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
'ReportSH.Sort.SortFields.Add Key:=Range("C1"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
' Custom Sort
ReportSH.Sort.SortFields.Add Key:=Range("C1"), SortOn:=xlSortOnValues, _
CustomOrder:="Apple, Orange, Grapes, Banana, Pineapple", DataOption:=xlSortNormal
LastRow = ReportSH.Range("B" & Rows.Count).End(xlUp).Row
With ReportSH.Sort
.SetRange Range("B1:D" & LastRow)
.Header = xlYes 'Data consists of Header or not
.MatchCase = False ' True for case sensitive, false for noncase sensitive
.Orientation = xlTopToBottom ' Denotes where sort is in ascending or descending
.SortMethod = xlPinYin ' Excel supports chinese language also
.Apply
End With
ReportSH.UsedRange.Columns.AutoFit
GroupTheRows_With_Checkboxes
End Sub
Sub Checkboxes_Selection()
On Error Resume Next
For N = 1 To ThisWorkbook.Names.Count
CheckBoxName = ThisWorkbook.Names(N).Name
If ReportSH.CheckBoxes(CheckBoxName).Value = xlOn Then
Range(CheckBoxName).CurrentRegion.Rows.Hidden = False
End If
If ReportSH.CheckBoxes(CheckBoxName) = xlOff Then
If CheckBoxName = "East" Then
ReportSH.Range(CheckBoxName).CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1, Selection.Columns.Count).Select
Else:
ReportSH.Range(CheckBoxName).CurrentRegion.Select
End If
Selection.Rows.Hidden = True
End If
Next
End Sub
Sub GroupTheRows_With_Checkboxes()
'ActivateReportWorksheet
r = 2
Dim cb As CheckBox
CheckboxRow = 2
Do Until ReportSH.Range("B" & r + 1).Value = ""
ReportSH.Range("B" & r).Activate
If r = 2 Then
AddCheckboxes (CheckboxRow)
ElseIf ReportSH.Range("B" & r).Value <> ReportSH.Range("B" & r + 1).Value Then
ReportSH.Range("B" & r + 1).EntireRow.Insert
CheckboxRow = r + 2
AddCheckboxes (CheckboxRow)
r = r + 1
End If
r = r + 1
Loop
End Sub
Function AddCheckboxes(CheckboxRow)
With ReportSH.Range("A" & CheckboxRow)
Set cb = ReportSH.CheckBoxes.Add(.Left, .Top, .Width, .Height)
With cb
.Value = xlOn
.Caption = ReportSH.Range("B" & CheckboxRow).Value
.Name = ReportSH.Range("B" & CheckboxRow).Value
.Border.ColorIndex = 3
.OnAction = "Checkboxes_Selection"
End With
End With
ThisWorkbook.Names.Add Name:=ReportSH.Range("B" & CheckboxRow).Value, RefersTo:=ReportSH.Range("B" & CheckboxRow)
End Function