Maintain array after function terminates

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. 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.

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
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I may be way off the mark here, but I think what you are describing requires you to create a custom class object to hold your array so that it persists between calls.
 
Upvote 0
=ODIN=

Thank you for the direction. Apologies for the late reply. I will definitely look into that.
 
Upvote 0
Isn't gbl_arr_unHide_col() retaining it's values?

Where have you declared it?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top