+ Reply to Thread
Results 1 to 9 of 9

Open workbook on todays date

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2012
    Location
    Derby, England
    MS-Off Ver
    Excel 2010
    Posts
    257

    Open workbook on todays date

    HI,

    I have a spreadsheet that I'm working on and I want it to select the cell that corresponds to today's date when you open the workbook

    I already have the following that does a few other things when you open the workbook and need to incorporate it into this code:

    The sheet "Rota" is the sheet with the list of dates on: I have attached an example for ease as once I've got the code I can figure out how to incorporate it.
    dates test.xlsx
    Private Sub Workbook_Open()
    
    Application.ScreenUpdating = False
    
    Sheets("Current Week").Cells(3, 4) = Sheets("Current Week").Cells(5, 5)
    With Worksheets("Current Week")
            .Activate
            .Range("D3").Select
        End With
        Sheets("Rota").Activate
        
        Application.ScreenUpdating = True
        
    End Sub

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Open workbook on todays date

    Hi, jshaw82,

    due to the file format itīs impossible to integrate macro code into an *.xlsx, you must either save as *.xlsm or *.xlsb and then copy the code into ThisWorkbook.

    Your attachment shows an empty workbook - what shall be integrated where?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    08-14-2012
    Location
    Derby, England
    MS-Off Ver
    Excel 2010
    Posts
    257

    Re: Open workbook on todays date

    Sorry - see the attached - i hadn't saved it as a XLS

    dates test.xls

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Open workbook on todays date

    Hi, jshaw82,

    sheet shows last year so maybe use this code in Workbook_Open:
    Private Sub Workbook_Open()
    Dim lngRow As Long
    
    Application.ScreenUpdating = False
    
    With Worksheets("Current Week")
        .Cells(3, 4) = .Cells(5, 5)
    '    .Range("D3").Select
    End With
    With Worksheets("Rota")
        lngRow = WorksheetFunction.Match(CDbl(DateSerial(Year(Date) - 1, Month(Date), Day(Date))), .Columns(2), 0)
        Application.Goto .Cells(lngRow, 1), True
    End With
    
    Application.ScreenUpdating = True
        
    End Sub
    Ciao,
    Holger

  5. #5
    Forum Contributor
    Join Date
    08-14-2012
    Location
    Derby, England
    MS-Off Ver
    Excel 2010
    Posts
    257

    Re: Open workbook on todays date

    Hi,

    I cannot get the code to work - i think its because i have the cells that the date is in merged? This is on purpose - I have uploaded the test file with the code in so you can see. I have also formatted the sheet how it is in the actual file i'm working with.

    dates test2.xls


    Thanks

    John

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Open workbook on todays date

    Hi, John,

    the cells that the date is in merged
    No, itīs simply that no match can be found if you search for a date in January 2012 and the dates in the sheet start with June 2012.

    Private Sub Workbook_Open()
    Dim varRes As Variant
    
    Application.ScreenUpdating = False
    
    With Worksheets("Current Week")
        .Cells(3, 4) = .Cells(5, 5)
    '    .Range("D3").Select
    End With
    With Worksheets("Rota")
        On Error Resume Next
        varRes = WorksheetFunction.Match(CDbl(DateSerial(Year(Date) - 1, Month(Date), Day(Date))), .Columns(2), 0)
        On Error GoTo 0
        If (Not IsError(varRes) And Len(varRes) > 0) Then Application.Goto .Cells(varRes, 1), True
    End With
    
    Application.ScreenUpdating = True
        
    End Sub
    Ciao,
    Holger

  7. #7
    Forum Contributor
    Join Date
    08-14-2012
    Location
    Derby, England
    MS-Off Ver
    Excel 2010
    Posts
    257

    Re: Open workbook on todays date

    Hi, I've added the code and today's date (28/01/2012) is in cell B396. Its not doing anything. Can you have a look at the attached file and see what I'm doing wrong?

    dates test3.xls

    Many thanks,
    John

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Open workbook on todays date

    Hi, John,

    because the other workbooks showed last year dates I used this line to reflect that:
        varRes = WorksheetFunction.Match(CDbl(DateSerial(Year(Date) - 1, Month(Date), Day(Date))), .Columns(2), 0)
    If the dates are from 2013 the code may be simplified to
        varRes = WorksheetFunction.Match(CDbl(Date), .Columns(2), 0)
    and row 396 is on top of the window (at least for today).

    HTH,
    Holger

  9. #9
    Forum Contributor
    Join Date
    08-14-2012
    Location
    Derby, England
    MS-Off Ver
    Excel 2010
    Posts
    257

    Re: Open workbook on todays date

    Thanks for you support on this - much appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1