Auto Date selections

ExcelRoy

Well-known Member
Joined
Oct 2, 2006
Messages
2,456
Office Version
  1. 2007
Platform
  1. Windows
Hi everyone,

I have the following code that adjusts the previous week and next 2 weeks unhidden when the workbook is opened (By the day)
The dates are displayed in Row 3 (In columns from column Q:KV)

is it possible for "complete" weeks to be shown and not, lets say from Wednesday as an example but show from Monday thru to Sunday, before todyas date and from Monday thru Sunday the follwoing 2 weeks


Private Sub Workbook_Open()
Dim i As Long
With Worksheets("Live Service Orders")
.Cells.EntireColumn.Hidden = False
For i = 17 To .Cells(3, Columns.Count).End(xlToLeft).Column
If .Cells(3, i).Value < Date - 7 Or .Cells(3, i) > Date + 14 Then
.Columns(i).Hidden = True
ElseIf Weekday(.Cells(3, i), 2) > 5 Then
.Columns(i).Hidden = True
End If
Next
End With
End Sub

Many thanks for any help
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,091
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Dim i As Long
With Worksheets("Live Service Orders")
    .Cells.EntireColumn.Hidden = False
    'For i = 17 To .Cells(3, Columns.Count).End(xlToLeft).Column
    myToday = Date
    myWeekday = Weekday(myToday, 2)
    For i = 17 To .Cells(3, Columns.Count).End(xlToLeft).Column
        If .Cells(3, i).Value <= myToday - myWeekday Or .Cells(3, i) > myToday + 7 - myWeekday + 14 Then
            .Columns(i).Hidden = True
        End If
    Next
End With
 

ExcelRoy

Well-known Member
Joined
Oct 2, 2006
Messages
2,456
Office Version
  1. 2007
Platform
  1. Windows
Hi mart37,

Throws a compile error?

Maybe something missing at the start?

Thanks for helping though!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,993
Messages
5,639,423
Members
417,089
Latest member
jonstr101

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
Top