Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Hiding Columns Not within a Date Range.
by Allen Wyatt
(last updated July 13, 2018)
Jason has a worksheet that contains several columns, each of which represent a fiscal week for his company. These fiscal weeks begin with column G, with column H being the second fiscal week, column I being the third week, and so forth. Jason would like to create a macro that, when run, would look at today's date, calculate the fiscal week based on that date, and then hide any of the fiscal-week columns that are not within a specified range.
For the purposes of providing an answer, I'm going to assume that the range you want displayed will be equal to the 13 weeks (one quarter) immediately preceding the current fiscal week. With this in mind, there are a couple of things that must be done by the macro. First, it must determine what fiscal week it currently is. Then, it must hide all weeks not in the 13 weeks prior to this current fiscal week and unhide all those that are.
This is all relatively easy to do, with the exception of figuring out which fiscal week it currently is. The method of determining fiscal weeks can vary wildly from company to company. For simplicity's sake, however, I'm going to assume that the determination is fairly straightforward: divide the day of the year by seven and see what we have.
The following macro implements the approach discussed so far.
Sub HideWeeks() Dim BeginYear As Date 'start of fiscal year date Dim FirstWeekCol As Integer 'first fiscal week column Dim FirstShowWkCol As Integer 'first column to show Dim CurrWkCol As Integer 'current week column Dim J As Integer BeginYear = Cells(1, 1).Value FirstWeekCol = 7 'fiscal weeks begin with Col 7 (G) 'Calculate Column of the current fiscal week CurrWkCol = ((Date - BeginYear) \ 7) + FirstWeekCol - 1 'Calculate column of the first week to show FirstShowWkCol = CurrWkCol - 14 If FirstShowWkCol < FirstWeekCol Then FirstShowWkCol = FirstWeekCol End If Application.ScreenUpdating = False 'Unhide all columns Columns("G:IV").Hidden = False 'Unhide all week Columns 'Hide week column before the rolling quarter For J = FirstWeekCol To FirstShowWkCol Columns(J).Hidden = True Next J 'Hide week column after current week For J = CurrWkCol + 1 To 256 Columns(J).Hidden = True Next intCol Application.ScreenUpdating = True End Sub
Note that there is one value that must be grabbed from the worksheet in this macro: the last day of the prior year. It is assumed that this is in cell A1, and it is grabbed and placed in the BeginYear variable. This value is used to determine the day of the current year.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9550) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Hiding Columns Not within a Date Range.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
One way you can widen the columns in a worksheet to fit whatever is in the column is by double-clicking the right edge of ...Discover More
Want to hide a column so it doesn't appear in the worksheet? It's easy to do using the formatting capabilities of Excel.Discover More
It is easy to adjust the width of columns in Excel. It is much harder to adjust the width of a range of columns ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.