如果您的数据有一行日期,您确实可以使用Weekday(Cells(r,c))
它来确定它是否是周末,然后用于Select Case
隐藏/取消隐藏列。
当前可以通过 检索Date()
。
然后您可以Sub Workbook_Open()
在ThisWorkbook
对象中设置代码。因此,当文件打开时,它会运行代码,因此今天和前 9 天不会被隐藏。
编辑:将这 2 个子添加到“ThisWorkbook”对象中,根据您的工作表名称、行和列进行更改。如果您想要前 9 个“工作日”,那么您将不得不更改案例或使用不同的方法来确定是否应该隐藏列。这是我会去的。祝你好运!
Sub Workbook_Open()
ShowTodayPlusPrevious
End Sub
Private Sub ShowTodayPlusPrevious()
' Assuming Row 1 contains the dates, stating from column 2
Const DateRow As Long = 1
Const PrevDays As Long = 9
Dim oWS As Worksheet, lCol As Long, lLastCol As Long, bHide As Boolean
Set oWS = ThisWorkbook.Worksheets("Sheet1") ' Change this to the name of your sheet
lLastCol = oWS.Cells.SpecialCells(xlLastCell).Column
For lCol = 2 To lLastCol
bHide = True
Select Case Date - oWS.Cells(DateRow, lCol).Value
Case 0 To PrevDays
If Weekday(oWS.Cells(DateRow, lCol).Value) <> vbSaturday And _
Weekday(oWS.Cells(DateRow, lCol).Value) <> vbSunday Then
bHide = False
End If
End Select
oWS.Columns(lCol).Hidden = bHide
Next
Set oWS = Nothing
End Sub