+ Reply to Thread
Results 1 to 6 of 6

Macro to find today’s date in list

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2016
    Location
    Warrington, England
    MS-Off Ver
    MS 365
    Posts
    174

    Macro to find today’s date in list

    Hi all, I have a list of dates in Column C for every day of the year. They are listed as:

    C5: Monday, 1 January
    C6: Tuesday, 2 January

    Etc.

    I am trying to get a macro that finds today’s date using the Today() function so I can always skip to the date at the time rather than scrolling down through the list.

    Any ideas? Thanks in advance!

  2. #2
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: Macro to find today’s date in list

    This code will do what you're asking, but I have one concern with your data and that is how it's entered in each cell.

    
    Sub PFDave()
    
    Dim c As Range, ws As Worksheet
    Set ws = Sheets("Sheet1")
    
    For Each c In ws.Range(ws.Cells(1, 3), ws.Cells(ws.UsedRange.Rows.Count, 3))
        If c = Date Then
        c.Select
        Exit Sub
        End If
    Next
    
    End Sub
    Please do add reputation where you see fit, it's nice to be nice and we all enjoy a pat on the back

    Please also mark your thread as solved once it has been.

  3. #3
    Forum Contributor
    Join Date
    08-14-2016
    Location
    Warrington, England
    MS-Off Ver
    MS 365
    Posts
    174

    Re: Macro to find today’s date in list

    Hi PFDave, that worked a treat! Thanks! Sorry to be a pain but would it be possible to explain how that macro is working? Just so I can understand it for the future and if I need to adapt it I can learn for myself a bit better!
    Excel Beginner

  4. #4
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: Macro to find today’s date in list

    Of course I can

    declare variable names and their type:
     Dim c As Range, ws As Worksheet
    Set variable for ws
    Set ws = Sheets("Sheet1")
    This creates the the loop which we are using For Each in a certain range. the ws.Range part is setting the range which we could do prior by declaring and setting another variable. N.B - We're using UsedRange to find the last row we want to use
     For Each c In ws.Range(ws.Cells(1, 3), ws.Cells(ws.UsedRange.Rows.Count, 3))
    To declare and set another variable for the range we would
     Dim rng as range
    Set rng = ws.Range(ws.Cells(1, 3), ws.Cells(ws.UsedRange.Rows.Count, 3))
    This would then change the For Loop to
    For Each c In rng
    Next we're checking if the cell (c) is = to todays date and if it is we want to select it and exit this routine because we've found what we're looking for. If it's not equal to todays date it will move onto the next cell in the range
    If c = Date Then
        c.Select
        Exit Sub
        End If

  5. #5
    Forum Contributor
    Join Date
    08-14-2016
    Location
    Warrington, England
    MS-Off Ver
    MS 365
    Posts
    174

    Re: Macro to find today’s date in list

    Awesome thanks Dave! Will mark as solved when I’m not on my phone as I can’t seem to find the option! Really appreciate your help!

  6. #6
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: Macro to find today’s date in list

    Glad it's sorted mate, any feedback greatly received

+ 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. VBA to loop through range to find date closest to today's date
    By esbencito in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2017, 02:08 AM
  2. Replies: 2
    Last Post: 10-22-2015, 03:36 PM
  3. Find the row where today's date is using macro or VBA
    By Chypp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2015, 02:03 PM
  4. Macro button to find today's date
    By razor_raef in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 07-08-2013, 11:23 PM
  5. Find today's date and then add 1
    By brown3218 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-17-2013, 01:21 AM
  6. Find today's date
    By Lynneth in forum Excel General
    Replies: 1
    Last Post: 01-05-2012, 08:25 AM
  7. Find Today's Date in a List-- How?
    By GaryCam in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-11-2006, 09:00 AM

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