VBA Workbook_Open指定日期(当前日期)

时间:2019-01-25 23:46:52

标签: excel vba

我创建了一个工作表,其中sheet1命名为1,sheet2命名为2,并继续到3(作为月份)。我在B2单元格的每个工作表上都有一个日期,我的日期格式是2019年1月1日,但其他工作表我使用='1'!+1公式来表示日期的延续。 我在“ ThisWorkbook”中使用了此VBA代码:

 Private Sub Workbook_Open()
    Worksheets("26").Activate
    Range("B1:B3").Find(Date).Select
End Sub

我想要的就是根据当前日期打开特定的工作表。

谢谢你们!!感激.......

1 个答案:

答案 0 :(得分:0)

'https://stackoverflow.com/questions/54374272/vba-workbook-open-for-specific-date-current-date
'VBA Workbook_Open for Specific Date (Current Date)

Sub CreateWorkbookWith31Sheets()
Dim wb As Workbook
Dim ws As Worksheets
Dim iCt As Integer
Set wb = Workbooks.Add
With wb.Worksheets
For iCt = 1 To .Count
    wb.Worksheets(iCt).Name = iCt
    wb.Worksheets(iCt).Range("B2").Value = DateSerial(2019, 1, iCt)
    wb.Worksheets(iCt).Range("B2").NumberFormat = "m/d/yyyy"
Next iCt
For iCt = iCt To 31
    .Add After:=wb.Worksheets(.Count)
    wb.Worksheets(iCt).Name = iCt
    wb.Worksheets(iCt).Range("B2").Value = DateSerial(2019, 1, iCt)
    wb.Worksheets(iCt).Range("B2").NumberFormat = "m/d/yyyy"
Next iCt
End With
ActiveWorkbook.SaveAs Filename:="C:\mySpecialFolder\my31days.xlsb", _  
    FileFormat:=xlExcel12
End Sub

'Copy this Subroutine to "ThisWorkbook" of the newly created workbook
Private Sub Workbook_Open()
Dim actDay As Integer
    actDay = Format(Date, "d")
    Worksheets(actDay).Activate
End Sub

=>此工作簿将打开并激活
。表格“ 26”(如果今天是每月26号!)