+ Reply to Thread
Results 1 to 13 of 13

on startup scroll across columns to the current date

Hybrid View

  1. #1
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    Office365
    Posts
    58

    on startup scroll across columns to the current date

    I have a range of dates by formula on row 2 across columns starting from G to DE. Dates are formatted as dd/mmm. Need a macro that will automatically scroll to the current date on opening the workbook. Row2 is protected, but row 3 onwards is not. The cursor should stop at Row 3, at the column with the current date. Any help appreciated. Thanks
    Rob.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: on startup scroll across columns to the current date

    Paste into ThisWorkbook module:


    Option Explicit
    
    Private Sub Workbook_Open()
    Dim x As Variant
            Worksheets("Sheet1").Select  '<-- change sheet name as required
            x = Day(Date)
           On Error Resume Next
           Worksheets("Sheet1").Rows(3).Find(What:=x, LookIn:=xlValues).Activate  '<-- change row # as required
           Application.Goto Selection, True
     End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    08-09-2015
    Location
    CzR
    MS-Off Ver
    MS Office 2013
    Posts
    41

    Re: on startup scroll across columns to the current date

    x = Day(Date)
    fails - you need to find dd/mmm string.

    My recommended code:

    Private Sub Workbook_Open()
    On Error Resume Next
    Application.Goto Sheet1.Rows(2).Find(Format$(Date,"dd/mmm"), , xlValues, xlWhole).Item(2, 1), True
    End Sub
    Change 'Sheet1' with the codename of your sheet; if the workbook has only one sheet, write simply
    Application.GoTo Rows(2)...
    Last edited by JBeaucaire; 03-01-2017 at 04:53 PM. Reason: Removed unnecessary commentary

  4. #4
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    Office365
    Posts
    58

    Re: on startup scroll across columns to the current date

    Hi friends,
    Both codes worked fine in the sample spreadsheet provided, but did not in my application. Disabling the error protection, it came up with RunTime Error 91: Object or With Block variable not set
    Rob
    Attached Files Attached Files
    Last edited by Robn02; 02-27-2017 at 04:21 PM.

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: on startup scroll across columns to the current date

    Please post your workbook for review.

  6. #6
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    Office365
    Posts
    58

    Re: on startup scroll across columns to the current date

    Posted on previous message. Appreciate your help.
    Thanks
    Rob

  7. #7
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: on startup scroll across columns to the current date

    This will check ALL sheets in the workbook for the current date. So, if you will have more than sheet VAC containing date,
    we'll need to change it.

    Private Sub Workbook_Open()
    'UpdatebyExtendoffice20161221
        Dim daterng As Range
        Dim DateCell As Range
        Dim WorkSht As Worksheet
        Dim dateStr As String
        Application.ScreenUpdating = False
        For Each WorkSht In Worksheets
            WorkSht.Select
            'Set daterng = Range("A:A")
            Set daterng = WorkSht.UsedRange
            'daterng.Select
            For Each DateCell In daterng
                DateCell.Activate
                ActiveCell.Select
                On Error Resume Next
                dateStr = DateCell.Value
                If dateStr = Date Then
                Application.ScreenUpdating = True
                    DateCell.Select
                    Exit Sub
                End If
            Next
        Next WorkSht
        
        'Worksheets(1).Select
    End Sub
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    Office365
    Posts
    58

    Re: on startup scroll across columns to the current date

    Thank you for the help. I will try this out when I get home tonight.
    Rob

  9. #9
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    Office365
    Posts
    58

    Re: on startup scroll across columns to the current date

    Thanks for everyone's help.
    I finally settled for this code in the Worksheet Module. Works as intended.


    Private Sub Worksheet_Activate()
    Dim DateRng As Range, DateCell As Range
    
    Set DateRng = Range("2:2")
    For Each DateCell In DateRng
    If DateCell.Value = Date Then DateCell.Select
    Next
    
    End Sub
    Thanks for the education. Valuable stuff.
    Rob

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: on startup scroll across columns to the current date

    Try this for fun.

    With Sheet17
        Application.Goto .Cells(2, Application.Match(CDbl(Date), .Rows(2), 0)), True
    End With
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  11. #11
    Registered User
    Join Date
    08-09-2015
    Location
    CzR
    MS-Off Ver
    MS Office 2013
    Posts
    41

    Re: on startup scroll across columns to the current date

    I've written this short code for you:

    Private Sub Workbook_Open()
    On Error Resume Next
    Application.Goto Sheet1.Rows(2).Find(Format$(Date,"dd/mmm"), , xlValues, xlWhole).Item(2, 1), True
    End Sub
    If (without the line 'On Error Resume Next') it fails, try only this:

    1. Write in the immediate window

    ? Sheet1.Name
    (don't forget the leading '?'). If it fails, you wrote incorrect sheet codename; use the correct one. If the codename 'Sheet1' is correct, then

    2. write in the immediate window (use copy/paste of course)

    ? Sheet1.Rows(2).Find(Format$(Date,"dd/mmm"), , xlValues, xlWhole) Is Nothing

    If it returns 'True', the date wasn't found, it's missing in your sheet; fix it. That's why the subroutine begins with 'On Error Resume Next'. If you want to say something to user in this case, change the code into

    Private Sub Workbook_Open()
    On Error Resume Next
    Application.Goto Sheet1.Rows(2).Find(Format$(Date,"dd/mmm"), , xlValues, xlWhole).Item(2, 1), True
    if Err Then Application.Speech.Speak "Sorry buddy, the today's date is missing in the sheet", True
    End Sub
    Last edited by JBeaucaire; 03-01-2017 at 04:51 PM.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: on startup scroll across columns to the current date

    Jan Mach, your recommendations overall are excellent. Thanks for the alternate ideas.


    As for using the Sheet1 syntax the downfall of this method comes when a sheet is deleted and recreated with the same name, not an uncommon thing. By using the sheet names in your code, the vba survives the codename changes. Noted here for clarity of why it so often the preferred approach.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: on startup scroll across columns to the current date

    @ Jan

    It's said by many programmers that the use of On Error Resume Next is just a way to avoid writing extensive Error Handling and it's to be used with caution.

    You provide some debugging tips which is a good thing but the tip needed to get your code to work isn't included since it throws an Error 91 when deleting the On Error ....

    Did you test your code in the example file provided in Post#4 because the code I provided in the post just before yours works just fine in that file ?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Get excel to automatically scroll to the current date
    By Nannakl in forum Excel Programming / VBA / Macros
    Replies: 37
    Last Post: 07-06-2022, 06:35 AM
  2. [SOLVED] Scroll Bar and Current Value. Min and Max Values
    By alive555 in forum Excel General
    Replies: 42
    Last Post: 08-16-2015, 10:34 AM
  3. Replies: 2
    Last Post: 02-23-2014, 10:28 AM
  4. [SOLVED] copy and paste in current active cell, and need current date then scroll down 140 lines
    By vengatvj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-04-2013, 03:40 AM
  5. Replies: 8
    Last Post: 10-04-2013, 02:03 PM
  6. [SOLVED] Need average of days for date values from date to current day that updates current date
    By FinGhost in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-12-2013, 04:41 PM
  7. [SOLVED] Double click to add current date in multiple columns
    By MAI2010 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-17-2013, 05:39 PM

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