Msgbox when value for this year to date greater than this time last year

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

The below sheet 'Training Log' extract contains the following info:

Column A (starting Row 12, currently Row 8748 and added to daily) contains dates for every day since Jan 1 1998.
Column B contains either text either beginning with "REST", "OTHER" or a route description.
Column C contains a number greater than zero (miles run) or is blank.

Fri, 26 Nov 2021REST
Sat, 27 Nov 2021OTHER (IB)
Sun, 28 Nov 2021Hallas Br/Down Bents Ln Harden Lane/Smithy Ln/ Lee Farm/Blackhills/Golf Course/R down Beck Foot Lane/Wagon Lane/Down LLC to Hirst Wood Lock (to post)/Back up to 5-Rise Locks/Down to 3-Rise & over Br/Brown Cow/Main Rd all the way back home12.2
Mon, 29 Nov 2021OTHER (IB)
Tue, 30 Nov 2021Cull. Rd/Viaduct/Stn Rd Haworth Rd/Shay Lane/ Crack Lane/Main Street/ Bents Lane/Hallas Bridge (18/12/2018)5.0
Wed, 1 Dec 2021REST
Thu, 2 Dec 2021Cullingworth Rd/Viaduct/ Stn Road/Old Allen Road Back Lane/Wilsden Rd/ Cottingley Rd/Lee Lane/ Cross Lane/Coplowe Ln/ Crack Lane/Main Street/ Harden Ln/Mill Hill Top/ Wilsden Rd/Mad Mile/ Greenside Lane (21/02/2020)10.6


I'd be very grateful for 2 macros, which are quite similar:

Macro 1
Looks at Columns A and B from Jan 1 in the current year (whichever year that is) to today's date. It then counts the number of cells with a route description (i.e. excludes text strings beginning with "REST" and "OTHER").
It will then do the same for Columns A and B from Jan 1 in the previous year (whichever year that is) and compares the two totals.
If a route description has been entered in the last row of Column B and the total for the current year is greater than the total for the previous year by less than 2 then a msgbox appears with "You have been running more times this year than last, as at [whatever today's date last year is e.g. Dec 3 2020]".

Macro 2
Looks at Columns A and C from Jan 1 in the current year (whichever year that is) to today's date. It then sums the values of those cells and does the same for Columns A and C from Jan 1 in the previous year (whichever year that is) and compares the two.
If a value greater than zero has been entered in the last row of Column C and the value for the current year is greater than the value for the previous year by less than 10 then a msgbox appears with "You have run more miles this year than last, as at [whatever today's date last year is e.g. Dec 3 2020]".

Hope you can help?

Many thanks!
 
Last edited:
All I have to work with is the file you uploaded in post 6 and it is obvious you and I are not working on the same thing.
This file has its last 2020 route on Nov 4, 2020 at row 8355 with a distance of 4.2

This is what I did to check out the macros and what my results were:

1) with Training Log records as uploaded Dec6
ThisYrCount = 56
LastYrCount = 54
no message box because column B is REST

ThisYrDist = 511.8
LastYrDist = 347.4
no message box because column C is blank


2) Changed B8752 from REST to some route
ThisYrCount = 56
LastYrCount = 54
get message box, but really shouldn't because with no distance in column C, it's a route that's not being counted. If it were that would be 57 for this year
and would not get the message box because it exceeds last year by more than 2

ThisYrDist = 511.8
LastYrDist = 347.4
no message box because column C is blank


3) Entered a 1 as the distance in C8752
ThisYrCount = 57
LastYrCount = 54
no message box because this year more than 2 greater than last year

ThisYrDist = 512.8
LastYrDist = 347.4
no message box because this year distance is more than 10 greater than last year distance


4) Changes to bring last years numbers into line for message boxes
rows 8353 and 8354 changed to routes with distance of 2 each
row 8355 distance changed from 4.2 to 165
now
ThisYrCount = 57
LastYrCount = 56
message box displays

ThisYrDist = 512.8
LastYrDist = 512.2
message box displays

I'm now out of here,
Good luck with your project
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top