+ Reply to Thread
Results 1 to 7 of 7

Find yesterdays date in range and return row number to be used in offset

  1. #1
    Registered User
    Join Date
    11-14-2013
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2016
    Posts
    49

    Find yesterdays date in range and return row number to be used in offset

    Hi,

    I'm writing a macro that will import data from one workbook to another, based on yesterdays date. I have the import functionality working, and I've been playing all afternoon, but I can't find a way to find yesterdays date in a range and use that row number as the row offset value in this line:
    Please Login or Register  to view this content.
    Rather than having a set row offset value (in this case, 14) I would like to use the find function to lookup yesterdays date in range "B50:B80" in the worksheet I've designated as "sh1" and return the row number of the cell that has yesterdays date and use this value as the row offset value, replacing the hard coded 14 that is in there currently. The date values in the range are formatted as per Date Format.jpg.

    My code is below:

    Please Login or Register  to view this content.
    Many thanks,
    Dan

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Find yesterdays date in range and return row number to be used in offset

    The find is looking at
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-14-2013
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2016
    Posts
    49

    Re: Find yesterdays date in range and return row number to be used in offset

    Quote Originally Posted by AB33 View Post
    The find is looking at
    Please Login or Register  to view this content.
    Hi,

    Sorry, no the find you have quoted is looking up other values that I need to match (which works as i require). My code, as it stands, imports the values i require and into the right section, but I require the column offset to be dynamic based on yesterday's date not the static 14 value that is currently in my code. So essentially i require some extra lines of code that will do as I've outlined in my original post.

  4. #4
    Registered User
    Join Date
    11-14-2013
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2016
    Posts
    49

    Re: Find yesterdays date in range and return row number to be used in offset

    So I've been playing with a separate macro to attempt to find yesterday's date in the range and return a MsgBox with the row number to ensure it's finding the right cell. From my understanding I've written this new code correctly but for some reason it's not finding the date in the range so is returning the MsgBox saying "Incorrect!". I thought it might be something to do with date formatting, but I tried changing that also, to no avail. Can anyone see a reason why it's not finding the cell I require from Date_Range.jpg?

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    02-24-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    317

    Re: Find yesterdays date in range and return row number to be used in offset

    Your code works for me, and found the row where yesterdays date was.

    You may have to add a line of code that tells VBA your format is different.
    Please Login or Register  to view this content.
    As you step through your code, place the cursor over 'MyDate' to see its value. Hopefully that might help you troubleshoot the code.
    If I helped in any way, please click the star

  6. #6
    Registered User
    Join Date
    11-14-2013
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2016
    Posts
    49

    Re: Find yesterdays date in range and return row number to be used in offset

    Quote Originally Posted by Jim885 View Post
    Your code works for me, and found the row where yesterdays date was.

    You may have to add a line of code that tells VBA your format is different.
    Please Login or Register  to view this content.
    As you step through your code, place the cursor over 'MyDate' to see its value. Hopefully that might help you troubleshoot the code.
    Thanks for the reply Jim. I have tried adding the format date line, again to no avail. Could the potential problem be that the cells with the dates in are linked to formulas in other cells?

  7. #7
    Registered User
    Join Date
    11-14-2013
    Location
    Perth, Western Australia
    MS-Off Ver
    Excel 2016
    Posts
    49

    Re: Find yesterdays date in range and return row number to be used in offset

    Ok, so turns out there was a format I hadn't tried. Set the format of dates in the range to the "*14/03/2001" format and it finds them no worries. Makes sense I guess, given the date function in VBA is using regional date formats and so is the cell now. Thanks for the replies guys.

    Working code is:
    Please Login or Register  to view this content.

+ 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. [SOLVED] How2 find number in range and return that number or if doesn't exist then the next highest
    By Crawfinator1 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 06-06-2014, 08:52 AM
  2. [SOLVED] Find a date within a date range and return the header
    By huy_le in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-03-2014, 06:09 PM
  3. [SOLVED] Find first date of the month in range and return column number
    By jben86 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-06-2013, 08:30 PM
  4. Find Todays Date and return a Integer so I can use it for Offset
    By mgurren in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-23-2013, 01:50 AM
  5. [SOLVED] Find max value in range and return corresponding value in offset range
    By burnsy180 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-30-2012, 05:58 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