Hide Columns outside of date range

JonReyno

Board Regular
Joined
Jun 8, 2009
Messages
130
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm after some help if you will.

I have a single worksheet that has every day of the financial year (as from 17th June) on it starting in cell C5 through to Cell KD5 (being 31st March 2014).

I am looking to only show data from today back 2 weeks so it will be a rolling 2 week period. Within the data I have columns A and B which need to be visable at all times.

Does anyone have any ideas either with VBA/Macros or another solution how I would be able to do this. I had it in my mind that when you open the workbook the code automatically runs to show the day you open the file back 2 weeks.

I'm really hoping someone can help as I have been racking my brain trying to work out how I can do it.

I am using Excel 2010.

Many Thanks
Jon
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi Jon,
This bit of code should do exactly what you want.
To Enter the VBA Editor press Alt+F11.
Put the following in the ThisWorkbook mS Excel Objects of the workbook you have the report in.
Code:
Private Sub Workbook_Open()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1") 'change this to the sheetname of your report
ws.Activate
Call HideColumns
End Sub
After adjusting the Sheetname you can Insert a new module to the Project (Insert à module) and enter the following code in there:
Code:
Public Sub HideColumns()
Dim wb As Workbook
Dim ws As Worksheet
Dim rngDates As Range
Dim datToday As Date
 
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet 'run this macro from the sheet you want the hidden columns
Set rngDates = ws.Range("C5:KD5") 'this range should match the range of dates
 
Application.ScreenUpdating = False
 
'make sure you unhide all columns before hiding them based on dates
rngDates.Select
Selection.EntireColumn.Hidden = False
 
datToday = Date
 
For Each cell In rngDates.Cells
    If cell.Value <= (datToday - 14) Or cell.Value > datToday Then
        cell.Select
        Selection.EntireColumn.Hidden = True
    End If
Next
 
ws.Range("A1").Select
Application.ScreenUpdating = True
 
MsgBox Done!, vbOKOnly
 
End Sub

Hope it works.
 
Upvote 0
That has worked prefectly, well, so far, the test will be tomorrow to see if it calulates correctly, but I have every faith it will do. I removed the message box as it was just coming up wiht a '0' for some reason, but thats not an issue as I have set the format as a button.

Thank you for your help, you have saved me a lot of time!!

Jon :)
 
Upvote 0
Hi Jon,
This bit of code should do exactly what you want.
To Enter the VBA Editor press Alt+F11.
Put the following in the ThisWorkbook mS Excel Objects of the workbook you have the report in.
Code:
Private Sub Workbook_Open()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1") 'change this to the sheetname of your report
ws.Activate
Call HideColumns
End Sub
After adjusting the Sheetname you can Insert a new module to the Project (Insert à module) and enter the following code in there:
Code:
Public Sub HideColumns()
Dim wb As Workbook
Dim ws As Worksheet
Dim rngDates As Range
Dim datToday As Date
 
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet 'run this macro from the sheet you want the hidden columns
Set rngDates = ws.Range("C5:KD5") 'this range should match the range of dates
 
Application.ScreenUpdating = False
 
'make sure you unhide all columns before hiding them based on dates
rngDates.Select
Selection.EntireColumn.Hidden = False
 
datToday = Date
 
For Each cell In rngDates.Cells
    If cell.Value <= (datToday - 14) Or cell.Value > datToday Then
        cell.Select
        Selection.EntireColumn.Hidden = True
    End If
Next
 
ws.Range("A1").Select
Application.ScreenUpdating = True
 
MsgBox Done!, vbOKOnly
 
End Sub

Hope it works.

I knew it would be too good to be true!!

When my colleagues open this file it comes up with a Run Time error which is 'Run-time error '91': Object variable or With block variable not set'.

Prior to the error appearing you have to enable the document for editing, not sure if this has an impact on why the error is appearing.

Is there any way around it? The macro that is trying to run is:


Private Sub Workbook_Open()Dim wb As WorkbookDim ws As WorksheetSet wb = ActiveWorkbookSet ws = wb.Worksheets("Sheet1") 'change this to the sheetname of your reportws.ActivateCall HideColumnsEnd SubI hope you can help me resolve this issue.ThanksJon</PRE>
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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