Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- Windows
I have a function that identifies and then will either hide or unhide hidden columns. However, I'm at a loss how to maintain the array between times it's called. For example, immediately below is an example of the processes I would perform.
How do I maintain the array between the first time and second time the arr_col_unHide function is called??
Code:
AutoFilter table
arr_col_unHide 'Identify/unhide columns
Delete visible rows
arr_col_unHide 'Rehide columns
How do I maintain the array between the first time and second time the arr_col_unHide function is called??
Code:
Public gbl_arr_unHide_col() As Long
Function arr_col_unHide(ths As Worksheet, _
Optional nlbAction As Boolean = False, _
Optional nlbHide As Boolean = False) As Variant
' ~~ Create array of columns that are hidden
' ~~ How to hide the columns in excel sheet based on prior hidden columns
Dim rng As Range, _
rngUsed As Range
Set rngUsed = rng_Used(ths)
Dim cntr As Long
' ~~ Test if array has values in it; if empty, fill array
If IsError(Application.match("*", (gbl_arr_unHide_col), 0)) = True Then
ReDim gbl_arr_unHide_col(1 To rngUsed.Columns.Count)
' ~~ Identify hidden columns and store in array
For Each rng In rngUsed.Columns
If rng.EntireColumn.Hidden = True Then
cntr = cntr + 1
gbl_arr_unHide_col(cntr) = rng.column
Debug.Print rng.column
End If
Next rng
End If
' ~~ Un/Hide columns if needed
If nlbAction = True Then
' ~~ Un/Hide columns
For cntr = LBound(gbl_arr_unHide_col) To UBound(gbl_arr_unHide_col)
If IsEmpty(gbl_arr_unHide_col(cntr)) = False Then
.Columns(gbl_arr_unHide_col(cntr)).EntireColumn.Hidden = nlbHide
Else
Exit Function ' ~~ Terminate function when no more array elements
End If
Next cntr
End If 'nlbAction
End Function