• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Problem identifying dates|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Problem identifying dates|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosProblem identifying dates
sp_PrintTopic sp_TopicIcon
Problem identifying dates
Avatar
Barry Pegram
Gloucestershire UK
Member
Members
Level 0
Forum Posts: 14
Member Since:
March 21, 2022
sp_UserOfflineSmall Offline
1
April 5, 2022 - 11:11 pm
sp_Permalink sp_Print
Hello All
I have an extensive Gardening Almanac/Diary workbook with 12+ sheets of assorted data and a second workbook with various sheets laid out as 'forms' in order to display existing, and add new, data.
On one of the data sheets I have a list of just about all gardening tasks that I perform in the garden - from preparing the ground,thro' to harvesting. These tasks are listed in chronilogical order. Not all dates exist and of those that do, most are repeated several times  (The search could be looking for a non existent date). See attached sample worksheet.
 
MY PROBLEM IS:
...I am trying to get the row number of the first entry of the searcg date . This works if  dates in the searched list are in the form:   13/4/2018   (as long as that exact date exists in the search field)
However, because of what I had previously read (life before VBA)  -- that to reliably calculate with dates they need to be entered into the worksheet in the form   =DATE(2022,04,13)  -- and that is how I have entered most dates. Some are in a more extended formula, as in: -
=DATE(YEAR(CN_DT_Last_Frost),MONTH(CN_DT_Last_Frost)-1,DAY(CN_DT_Last_Frost)-5) 
where I want a date entry relative to the last expected frost in my area (Entered this way so that it will work for northern Scotland or southern England (N.Dakota - Florida) just change the referenced Frost_Date).
 Neither of these date forms seem to get identified in the line of code:  Set rng = Sheet1.Columns("B:B").Find...     and I get an error window "Object variable or With block variable not set". 
 
So in essence the question is:
Can dates in this form be searched? And also how do I search for a nearest (larger or smaller than) to the looked for date?
 
I have created a worksheet with sample data taken directly from my origional. Hope that helps explain things.
As always thank you in anticipation.
Kindest regards
 
Beepee
sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
April 10, 2022 - 11:02 pm
sp_Permalink sp_Print

Hi Barry,

2 things you need to change:

Set rng = Sheet1.Columns("B:B").Find(What:=dDate, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

Replace xlFormulas with xlValues, you will never find a date in a formula string that looks like: =DATE(2018,3,7)

Format B8:B38 as Short Date

Next, you can handle errors in a different way, just check if Rng is nothing, this means there was no result found for that date, you don't need to disable errors with On Error GoTo.

Set rng = Sheet1.Columns("B:B").Find(What:=dDate, _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
Sheet1.Cells(iCount, 11).Value = "Date not found: " & Format(dDate, "dd/mm/yyyy")
Else
rownumber = rng.Row
Sheet1.Cells(iCount, 11).Value = Sheet1.Cells(rownumber, 4).Value
End If
Set rng = Nothing
Next iCount

Avatar
Barry Pegram
Gloucestershire UK
Member
Members
Level 0
Forum Posts: 14
Member Since:
March 21, 2022
sp_UserOfflineSmall Offline
3
April 11, 2022 - 9:50 pm
sp_Permalink sp_Print

Hi Catalin.

Thank you for responding to my query. I had tried "xlValues" but I had not used any other date format.

However, having made the changes you suggest (I did actually copy/paste to avoid errors) I still get "Date not found: 30/12/1899"; i.e. the error rather than the matching 'ITEM' as the result in column K.

Just to confirm -- I am using Excel 2016.

Where am I going wrong?

Thanks again in anticipation

sp_AnswersTopicAnswer
Answers Post
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
April 11, 2022 - 10:31 pm
sp_Permalink sp_Print sp_EditHistory

That date is not a valid date. Excel date system starts from 1/1/1900, this is Day 1. Today's date (april 11 2022) is represented by a number: 44662, meaning that 44662 days passed since day 1. The subject is complex, there is another system: In the 1904 date system, dates are calculated by using January 1, 1904, as a starting point. 

You cannot use in excel dates before 1900 in numeric/date format, only as text.

Avatar
Barry Pegram
Gloucestershire UK
Member
Members
Level 0
Forum Posts: 14
Member Since:
March 21, 2022
sp_UserOfflineSmall Offline
5
April 11, 2022 - 11:42 pm
sp_Permalink sp_Print

Hi Its Beepee again.

 

Please ignor my previous reply to this post... STUPID HAST on my part.  It all works fine now that I have my head straight

Many thanks and appologies for my error. 🙂

Avatar
Barry Pegram
Gloucestershire UK
Member
Members
Level 0
Forum Posts: 14
Member Since:
March 21, 2022
sp_UserOfflineSmall Offline
6
April 12, 2022 - 2:25 am
sp_Permalink sp_Print

Hi All -- Beepee again.

The first reply to this post works and solved my immediate problem. Many thanks to Catalin.

However I am still struggling to solve the issue of the search date not existing in the data list. Under these circumstances I would like to get the nearest later (or sometimes earlier?) date.

Any help much appreciated. Thank you

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
7
April 12, 2022 - 2:34 am
sp_Permalink sp_Print

I would like to get the nearest later (or sometimes earlier?) date.

Depending on what? You have to be more precise than that, we can't imagine the rules that needs to be applied.

Avatar
Barry Pegram
Gloucestershire UK
Member
Members
Level 0
Forum Posts: 14
Member Since:
March 21, 2022
sp_UserOfflineSmall Offline
8
April 12, 2022 - 5:43 pm
sp_Permalink sp_Print

Hi. Yes...

The gardening tasks  - - are listed in chronilogical order. Not all dates exist on the data sheet and of those that do, most are repeated several times  (The search could be looking for a non existent date). See below sample worksheet. The search date is derived from a fairly simple function that adds nine days to the current date, (changes year to 2017-18) to look for outstanding jobs for the next 'week'.

Using the data below, and assume 'today' is 10 March then I would want to find either the first entry for the 19 March or the nearest later date (20 Mar in list). Hope this is sufficient. Thanks as always.

01/03/2018 F RED CURRANT 
07/03/2018 F CORNFLOWER
07/03/2018 F GAZANIA
07/03/2018 F RUDBECKIA
07/03/2018 H CRESS
07/03/2018 M LIME
07/03/2018 V BROAD BEAN
07/03/2018 V LEEK
07/03/2018 V LETTUCE
07/03/2018 V POTATO
07/03/2018 V TOMATO 
10/03/2018 H MUSTARD
10/03/2018 V CARROT
10/03/2018 V RUNNER BEAN
15/03/2018 F CHRYSANTHEMUM
15/03/2018 F ESCHSCHOLTZIA
15/03/2018 F LOBELIA
15/03/2018 F PETUNIA
15/03/2018 F VERBENA
15/03/2018 V BROCCOLI 
15/03/2018 V BRUSSELS SPROUTS
15/03/2018 V CUCUMBER
18/03/2018 V SHALLOT
18/03/2018 V TOMATO 
20/03/2018 V RUNNER BEAN
21/03/2018 V POTATO
23/03/2018 F MARIGOLD
23/03/2018 F WATER HYSSOP
23/03/2018 H BASIL
23/03/2018 H CHERVIL
23/03/2018 H ROCKET 
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
9
April 12, 2022 - 9:35 pm
sp_Permalink sp_Print sp_EditHistory

Is there a reason why you need vb code for this?
The problems you indicate come from poor data structure, instead of those 12+sheets and forms to add data, you should reorganize into a proper structure.

You can do a search similar to the vb search using a simple formula, this for example returns the row index of the match found:

=MATCH(J2,B:B,0)

If you want to find the next entry in case of an error, just use:

=IFERROR(MATCH(J2,B:B,0),MATCH(J2,B:B,1))

Avatar
Barry Pegram
Gloucestershire UK
Member
Members
Level 0
Forum Posts: 14
Member Since:
March 21, 2022
sp_UserOfflineSmall Offline
10
April 12, 2022 - 11:13 pm
sp_Permalink sp_Print

Hi Catalin, and thank you for the quick response. The 12 data sheets, and therefore the structure of the data, evolved over several years. I started out with a fairly straight forward log of purchases and sowing, planting, harvesting dates. I now have cultivation notes, weather data, seed saving, germination data etc. I am not sure I would know a proper structure -- Excel, macros, and more recently VBA have been pretty much self-taught (with help from Mr Google) and  help from people like yourself which has been very much appreciated. 

Anyway, I have a cell on the output form that displays the '9_day' date that I am searching for; so I am sure I can use your =MATCH... formula with the =IFERROR...; in place of what I have to find the necessary date.

So once again I am most grateful for your help. Thanks

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Brian Pham
Guest(s) 8
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 205
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
stuart burge
Bruce Tang Nian
Scot C
Othman AL MUTAIRI
Misael Gutierrez Sr.
Attif Ihsan
Kieran Fee
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Forum Stats:
Groups: 3
Forums: 24
Topics: 6223
Posts: 27295

 

Member Stats:
Guest Posters: 49
Members: 31920
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.