I've recently converted a number of ranges to Named Ranges to try and 'protect' if the user adds any rows or columns to the sheet.
I have the below code basically 5 different times, for 5 separate spreadsheets.
I had thought that by also assigning named ranges to everything that I could then possible combine all the named ranges together into one singe macro. However, those attempts fail with a Method 'Range' of object '_Global' failed.
Essentially, I'm trying to learn if you can combine multiple Named Ranges from separate worksheets into 1 macro, or if you need to Select each sheet separately. Just trying to learn and improve. I thought about also trying to learn and create a 2 dimensional array of the sheet name and named ranges for each, and then loop it that way.
Or assign separate 'rows_rng' and 'col_rng' for each sheet?
The below works fine. Only trying to learn why adding names ranges from other sheets will fail (I did removed the Sheets.Select when trying to add additional named ranges from other sheets)
Sub AR_Chart_Hide()
Sheets("Receivable Charts").Select
Dim rows_rng As Range, col_rng As Range
Dim rows2hide As Range, col2hide As Range, Cl
Set rows_rng = Range("RowHide_ARAgeCompare,RowHide_ARConc,RowHide_ARInvTest,RowHide_ARLead,RowHide_ARPastDue,RowHide_ARRecon,RowHide_ARSalesConc,RowHide_ARStats,RowHide_ARTrends,RowHide_ARVerification")
'Set rows_rng = Range("O15:O27, Y38:Y47, AL55:AL78, BK97:BK118, CA149:CA178, CG191:CG197, CW225:CW232, DL254:DL258, DZ271:DZ275, ER318:ER354")
Set col_rng = Range("ColHide_ARAgeCompare, ColHide_ARLead, ColHide_ARConc,ColHide_ARRecon,ColHide_ARStats")
'Set col_rng = Range("U50:X50, BC120:BJ120, DU277:DZ277, EE292:EH292, EM355:EQ355")
For Each Cl In rows_rng
If Cl.Value = "Yes" Then
If rows2hide Is Nothing Then Set rows2hide = Cl Else Set rows2hide = Union(Cl, rows2hide)
End If
Next
For Each Cl In col_rng
If Cl.Value = "Yes" Then
If col2hide Is Nothing Then Set col2hide = Cl Else Set col2hide = Union(Cl, col2hide)
End If
Next
Application.ScreenUpdating = 0
rows_rng.EntireRow.Hidden = 0
If Not rows2hide Is Nothing Then rows2hide.EntireRow.Hidden = 1
col_rng.EntireColumn.Hidden = 0
If Not col2hide Is Nothing Then col2hide.EntireColumn.Hidden = 1
Application.ScreenUpdating = 1
End Sub
Bookmarks