+ Reply to Thread
Results 1 to 18 of 18

Macro button to find today's date

Hybrid View

  1. #1
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Macro button to find today's date

    Hi all,

    I've got a spreedsheet which has the following:

    B3-B33 = May1-May31
    C3-C33 = B3+51 - C33+51 etc(51 days ahead of May1,etc)

    Then E3-E32 = June1-June30
    F3-F32 = E3+51 - E32+51 etc(51 days ahead of June1,etc)

    And so forth until cell AP32 which = AO32+51 (June30+51)

    The purpose of this spreadsheet is to determine 51 days until a policy lapses.

    Now, for what I need, I require a button that has an assigned Macro which will go to the cell =today(). For example, today's date is 05/07/13. So my spreadsheet has that value in H7 but also has the same date in C17 (which is 15 May + 51 days).

    I can confirm the range required is B3:AP33.

    Can anyone help me out figuring out the Macro needed?

    If you need more info, or a copy of the spreadsheet, please let me know.
    Raef

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro button to find today's date

    If you need more info, or a copy of the spreadsheet, please let me know.
    More info is always appreciable & work well for speedy response.. sample please..
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

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

    Re: Macro button to find today's date

    Hi, Raef ,

    why not use Conditional Formatting for highlighting the date?
    =B3=TODAY()
    And why not use only 3 cells for showing Today, Today - 51 as well as Today + 51? I donīt get the reason for requesting a macro.

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are demonstrated, mock them up manually if needed. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    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

  4. #4
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    Attached is an example of the spreadsheet
    Attached Images Attached Images

  5. #5
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    Is there any way I can actually post the spreadsheet in question?

  6. #6
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    Funny enough Holger, I already have exactly just that, but what I need is a button to allow the user (who will open up the spreadsheet at cell A1) to press it and take them to today's date, which maybe in cell AK13 for example. So that way cell AK13 is now centre screen, or even if it's easier, the month in question. For example, currently ColumnB is May2013, ColumnE is June2013, until Column AO is June2014

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

    Re: Macro button to find today's date

    Hi, Raef,

    can your Excel work with a picture and perform action in it? Mine canīt.

    Sub FindToday_Values()
    Dim rngFound As Range
    Set rngFound = Range("B3:AP33").SpecialCells(xlCellTypeConstants, 23).Find(what:=Date, LookIn:=xlValues)
    If Not rngFound Is Nothing Then
      Application.Goto reference:=Cells(3, rngFound.Column), Scroll:=True
    End If
    End Sub
    Sub FindToday_Formulas()
    Dim rngFound As Range
    Set rngFound = Range("B3:AP33").SpecialCells(xlCellTypeFormulas, 23).Find(what:=Date, LookIn:=xlValues)
    If Not rngFound Is Nothing Then
      Application.Goto reference:=Cells(3, rngFound.Column), Scroll:=True
    End If
    End Sub

  8. #8
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    I'd love to have a button for a macro placed around E35 that would transport view to show the period/cells around the green cells and the latest read cells in the single view, similar to how it shows now. But later on in the beginning of next year, the users will only see red when they first open up the spreadsheet, if they click the macro button near E35, it will transport them to the current details and view like how it is currently viewed.
    I really hope this makes sense.
    Raef

  9. #9
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Macro button to find today's date

    Hi razor,

    after click of button, where do you want to set focus..

    As per your picture..
    17 pixel Down.. and 245 pixel left.. or 2 inch Down form top, 3 inch left from left..

    Hope you have already realised we cant setFocus on Picture.. and we cant attach Button in a picture.. without AdobeCS3..
    Please upload .XLS[A-Z] file..

  10. #10
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    Quote Originally Posted by Debraj Roy View Post
    after click of button, where do you want to set focus..
    Ok rethinking all this, what I need is the macro button to select the TODAY()-51 day, which is in the attached pic as B17, but they should also be able to see all the other red fields in B field, so maybe selecting B1 will be the better option.

    I'm thinking the macro would need to determine the following:

    =today()-51 (this is today's date -51 days)
    from there determine the month
    from there let the first day of that month be selected
    this will ensure the first day of the month will be seen and the entire month will be seen underneath

    I can't have half the month cut off from view, so that if cell b17 get selected its not in the top left hand corner of the screen but still showing the entire month above and below it.

    Once again if you need more info, please let me know.

    Attached is the spreadsheet.

    Raef
    Attached Files Attached Files

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

    Re: Macro button to find today's date

    Hi, Raef,

    Sub GoToDate()
    Dim lngMonths As Long
    
    lngMonths = Month(Date) - Month(Range("B3"))
    If Year(Date) > Year(Range("B3")) Then
      lngMonths = lngMonths + 12
    End If
    
    Application.Goto Reference:=Cells(2, 2 + lngMonths * 3), Scroll:=True
    
    End Sub
    Corrected code:
    Sub GoToDate2()
    Dim lngMonths As Long
    
    lngMonths = Month(Date) - Month(Range("B3"))
    If lngMonths < 2 And Year(Date) > Year(Range("B3")) Then
      lngMonths = lngMonths + 12 * (Year(Date) - Year(Range("B3")))
    End If
    
    Application.Goto Reference:=Cells(2, 2 + lngMonths * 3), Scroll:=True
    
    End Sub
    Ciao,
    Holger
    Last edited by HaHoBe; 07-05-2013 at 04:34 AM. Reason: placed corrected code

  12. #12
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    Hi, I tried the below Corrected Code and it works fine apart from the fact it selected June 18 and not May 18. (for today's date July 8). What should I do to the code in order for it to go one more month back? Attached is the spreadsheet.

    Sub GoToDate()
    Dim lngMonths As Long

    lngMonths = Month(Date) - Month(Range("B3"))
    If Year(Date) > Year(Range("B3")) Then
    lngMonths = lngMonths + 12
    End If

    Application.Goto Reference:=Cells(2, 2 + lngMonths * 3), Scroll:=True

    End Sub
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    I placed in a future date (07/02/14) into cell C35 and hit find and the spreadsheet didn't automatically change it's view from it's current position to the future date, anyway this can be done as this is the main purpose of this macro?

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

    Re: Macro button to find today's date

    Hi, Raef,

    it works fine apart from the fact it selected June 18 and not May 18.
    The code is supposed to find the column with the actual date, not any date in the past. Itīs not taking the date from any cell but from the system date of the computer. If you want it to reflect the date you put into Cell C35 please try
    Sub Lapsedatefind_GivenDate()
    
    Dim lngMonths As Long
    Dim dteSearch As Date
    
    dteSearch = Range("C35")
    
    lngMonths = Month(dteSearch) - Month(Range("B3"))
    If lngMonths < 2 And Year(dteSearch) > Year(Range("B3")) Then
      lngMonths = lngMonths + 12 * (Year(dteSearch) - Year(Range("B3")))
    End If
    
    Application.Goto Reference:=Cells(2, 2 + lngMonths * 3), Scroll:=True
    
    End Sub
    Please mind that you should wrap code with code-tags.

    Ciao,
    Holger

  15. #15
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    OMG i love how that above code worked! It's what I need it to do but I need it to select the 18/05/14-07/02/14 in CellW20-CellX20 as opposed to the CellAC09-CellAD09 cell.

    Attached is the spreadsheet to show what I mean.
    Attached Files Attached Files

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

    Re: Macro button to find today's date

    Hi, Raef,

    if you search for the Lapse date you will have to face 2 months span in one column - maybe reduce it to search in the area which is 51 days ago like

    Sub Lapsedatefind_GivenDate2()
    
    Dim lngMonths As Long
    Dim dteSearch As Date
    
    dteSearch = Range("C35") - 51
    
    If dteSearch <= Range("B3") Then Exit Sub
    
    
    lngMonths = Month(dteSearch) - Month(Range("B3"))
    If lngMonths < 2 And Year(dteSearch) > Year(Range("B3")) Then
      lngMonths = lngMonths + 12 * (Year(dteSearch) - Year(Range("B3")))
    End If
    
    Application.Goto Reference:=Cells(2, 2 + lngMonths * 3), Scroll:=True
    
    End Sub
    Ciao,
    Holger

  17. #17
    Registered User
    Join Date
    07-05-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel Office Professional Plus 2010
    Posts
    32

    Re: Macro button to find today's date

    Thanks for your advice Holger, that last one was almost perfect, I edited it slightly to the below code and now works perfect. Thank you so much for your support *big thumbs up*

    Sub Lapsedatefind_GivenDate2()
    
    Dim lngMonths As Long
    Dim dteSearch As Date
    
    dteSearch = Range("C35") - 51
    
    If dteSearch <= Range("B3") Then Exit Sub
    
    
    lngMonths = Month(dteSearch) - Month(Range("B3"))
    If lngMonths < 2 And Year(dteSearch) > Year(Range("B3")) Then
      lngMonths = lngMonths + 12 * (Year(dteSearch) - Year(Range("B3")))
    End If
    
    Application.Goto Reference:=Cells(1, 1 + lngMonths * 3), Scroll:=True
    
    End Sub

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

    Re: Macro button to find today's date

    Hi, Raef,

    thanks for the feedback. If this is what you have been looking for please mark this thread as Solved.

    To mark your thread solved do the following:
    - Go to the first post
    - Click edit
    - Click Advance
    - Just below the word "Title:" you will see a dropdown with the word No prefix.
    - Change to Solve
    - Click Save
    Ciao,
    Holger

+ 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