Hide columns with variable sheet names

nutrastat

Board Regular
Joined
Nov 1, 2008
Messages
57
Hi,

I have tried to put together some VBA code to hide columns on several sheets, using answers based on various responses but have difficulty because my sheet names may vary, as some ships call Accommodation - House Keeping, and Garbage - Incinerator.

To cope with these name requirements, on a sheet called 'Notes', column/row D152 to D156, I have code that extracts and gives me the sheet names. In these cells I have sheet names Accommodation, Stores, Restaurant, Bars & Garbage. Each cell is named after the sheet such as 'Tab_Name_Accommodation, Tab_Name_Stores, Tab_Name_Restaurant, etc. In this way, for headings within a sheet when the tab name is changed it also changes headings on the sheet, as I simply call up to 'Tab_Name_' cell reference, as everyting is based on the tab name.

My requirement: on some months there are 4 weeks and occasionally 5 weeks in a month, and I wish to hide Week 5 columns (AI to AL) on EACH of these sheets when the cell name WK05_TRUE_FALSE = FALSE (i.e. there is no week 5).

Can this be done by using the cell names (D152 to D156) rather than static names as I have seen in answers in the past?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
on a sheet called 'Notes' ... Each cell is named after the sheet such as 'Tab_Name_Accommodation, Tab_Name_Stores, Tab_Name_Restaurant...

... I wish to hide Week 5 columns (AI to AL) on EACH of these sheets when the cell name WK05_TRUE_FALSE = FALSE ...

Can this be done by using the cell names (D152 to D156) rather than static names as I have seen in answers in the past?

So the cell names, D152- are named 'Tab_Name_Accomodation, etc? Correct?

If so, then try this:
Code:
Sub Hide5thWeek()
    'set to false manually for this test
    WK05_TRUE_FALSE = False
    If WK05_TRUE_FALSE = False Then
        strAccommodationSheet = Range("Tab_Name_Accommodation")
        strStoresSheet = Range("Tab_Name_Stores")
        strRestaurant = Range("Tab_Name_Restaurant")
        Worksheets(strAccommodationSheet).Columns("AI:AL").EntireColumn.Hidden = True
        Worksheets(strStoresSheet).Columns("F:I").EntireColumn.Hidden = True
        Worksheets(strRestaurant).Columns("F:I").EntireColumn.Hidden = True
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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