Forum

Problem identifying...
 
Notifications
Clear all

Problem identifying dates

10 Posts
2 Users
0 Reactions
108 Views
(@beepee)
Posts: 14
Eminent Member
Topic starter
 
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
 
Posted : 06/04/2022 9:11 am
(@catalinb)
Posts: 1937
Member Admin
 

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

 
Posted : 11/04/2022 9:02 am
(@beepee)
Posts: 14
Eminent Member
Topic starter
 

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

 
Posted : 12/04/2022 7:50 am
(@catalinb)
Posts: 1937
Member Admin
 

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.

 
Posted : 12/04/2022 8:31 am
(@beepee)
Posts: 14
Eminent Member
Topic starter
 

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. 🙂

 
Posted : 12/04/2022 9:42 am
(@beepee)
Posts: 14
Eminent Member
Topic starter
 

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

 
Posted : 12/04/2022 12:25 pm
(@catalinb)
Posts: 1937
Member Admin
 

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.

 
Posted : 12/04/2022 12:34 pm
(@beepee)
Posts: 14
Eminent Member
Topic starter
 

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 
 
Posted : 13/04/2022 3:43 am
(@catalinb)
Posts: 1937
Member Admin
 

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))

 
Posted : 13/04/2022 7:35 am
(@beepee)
Posts: 14
Eminent Member
Topic starter
 

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

 
Posted : 13/04/2022 9:13 am
Share: