Last active
July 13, 2021 17:53
-
-
Save sancarn/6657b2a09b79459fd47bcdaabc558642 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public HeaderColor as Long | |
Private OptionsSheet as Worksheet | |
Private DataSheet as Worksheet | |
Private Sub Worksheet_Change(ByVal Target As Range) | |
Me.HeaderColor = RGB(217, 217, 217) | |
set OptionsSheet = sheets("Options") | |
set DataSheet = ActiveWorksheet | |
if target.address = "$B$1" then | |
customiseVisibility target.value | |
end if | |
End Sub | |
Sub customiseVisibility(ByVal query As String) | |
Dim cell As Range | |
Set cell = OptionsSheet.Range("1:1").Find(query) | |
Dim offset As Long | |
offset = 1 | |
While Not IsEmpty(cell.offset(offset)) | |
getNthRegion(DataSheet, offset).Hidden = cell.offset(offset).Value = "N" | |
offset = offset + 1 | |
Wend | |
End Sub | |
Private Function getRegion(cell As Range) As Range | |
Dim formatted As Boolean | |
Dim cell_start, cell_end As Range | |
'If cell row is 1 then exit function | |
If cell.Row <= 1 Then Exit Function | |
'If cell row count > 1 then use first cell selected | |
If cell.Rows.Count > 1 Then Set cell = cell.Cells(1, 1) | |
'If selection is outside of used range, do nothing | |
If Application.Intersect(cell, cell.Parent.UsedRange) Is Nothing Then Exit Function | |
'Special condition | |
If cell.Interior.Color = Me.HeaderColor Then | |
'Select row below | |
Set cell = cell.offset(1) | |
End If | |
'Get start cell | |
Set cell_start = cell | |
While Not cell_start.Interior.Color = Me.HeaderColor And Not Application.Intersect(cell_start, cell.Parent.UsedRange) Is Nothing ' Your gray color | |
Set cell_start = cell_start.offset(-1) | |
Wend | |
'Get end cell | |
Set cell_end = cell | |
While Not cell_end.offset(iRowEnd, 0).Interior.Color = Me.HeaderColor And Not Application.Intersect(cell_end, cell.Parent.UsedRange) Is Nothing ' Your gray color | |
Set cell_end = cell_end.offset(1) | |
Wend | |
'Get region | |
Set getRegion = Range(cell_start.offset(1), cell_end.offset(-1)).EntireRow | |
End Function | |
Function getNthRegion(ByVal sheet As Worksheet, ByVal n As Long) As Range | |
Dim i, counter As Long | |
For i = 1 To sheet.UsedRange.Rows.Count | |
If sheet.Cells(i, 1).Interior.Color = HeaderColor Then | |
counter = counter + 1 | |
End If | |
If counter = n Then | |
Set getNthRegion = getRegion(sheet.Cells(i, 1)) | |
Exit Function | |
End If | |
Next | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment