ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Comparing Dates (https://www.excelbanter.com/excel-programming/401365-comparing-dates.html)

JT

Comparing Dates
 
Sorry for the previous post with no info.

I'm comparing data on 2 different sheets in a workbook. To do this, I
create a reference field based on fields appearing on both sheets.

Originally the reference fields contained a cost center, month, day, and
amount. The items from each sheet had to match exactly.

On one sheet the date appears as mm/dd/yyyy. On the other sheet, the data
appears as mm/dd.

Originally, I stripped out the month and day from both sheets and made a
string from these fields after ensuring the length of both were 2 characters.
This worked well.

Now however, I need to change it so the user can enter a number of tolerance
days. If the date on sheet1 is equal to or greater than the date on sheet2
by the number of tolerance days, it is a match.

I want to set a variable for the date (month and day) on sheet2 and compare
it to a variable for the date on sheet1.

I'm having trouble getting started since the date on sheet 2 is only month
and day. I'm not sure how to handle it when the date on sheet2 is the end of
one month and the date on sheet1 is one of the first few days in the next
month.

Is there some way to compare the month and day and calculate the difference
between another month and day?

Thanks. Any help getting started would be greatly appreciated.........






--
JT

Gleam

Comparing Dates
 
Excel uses numbers for dates and times from 1/1/1900. Therefore one canadd
and subtract. (past the decimal point for times) Please try the following
code:

Sub Test()
If Cells(2, 2).Value - Cells(2, 1).Value <= 7 Then
Cells(2, 3).Value = "Within tolerance"
Else
Cells(2, 3).Value = "Different"
End If
End Sub


"JT" wrote:

Sorry for the previous post with no info.

I'm comparing data on 2 different sheets in a workbook. To do this, I
create a reference field based on fields appearing on both sheets.

Originally the reference fields contained a cost center, month, day, and
amount. The items from each sheet had to match exactly.

On one sheet the date appears as mm/dd/yyyy. On the other sheet, the data
appears as mm/dd.

Originally, I stripped out the month and day from both sheets and made a
string from these fields after ensuring the length of both were 2 characters.
This worked well.

Now however, I need to change it so the user can enter a number of tolerance
days. If the date on sheet1 is equal to or greater than the date on sheet2
by the number of tolerance days, it is a match.

I want to set a variable for the date (month and day) on sheet2 and compare
it to a variable for the date on sheet1.

I'm having trouble getting started since the date on sheet 2 is only month
and day. I'm not sure how to handle it when the date on sheet2 is the end of
one month and the date on sheet1 is one of the first few days in the next
month.

Is there some way to compare the month and day and calculate the difference
between another month and day?

Thanks. Any help getting started would be greatly appreciated.........






--
JT


Gleam

Comparing Dates
 
I realised in the middle of the night that the order of dates mattered in my
first lot of code. Here is a version where the order does not matter:
Sub Test()
If Abs(Cells(2, 2).Value - Cells(2, 1).Value) <= 7 Then
Cells(2, 3).Value = "Within tolerance"
Else
Cells(2, 3).Value = "Different"
End If
End Sub

"JT" wrote:

Sorry for the previous post with no info.

I'm comparing data on 2 different sheets in a workbook. To do this, I
create a reference field based on fields appearing on both sheets.

Originally the reference fields contained a cost center, month, day, and
amount. The items from each sheet had to match exactly.

On one sheet the date appears as mm/dd/yyyy. On the other sheet, the data
appears as mm/dd.

Originally, I stripped out the month and day from both sheets and made a
string from these fields after ensuring the length of both were 2 characters.
This worked well.

Now however, I need to change it so the user can enter a number of tolerance
days. If the date on sheet1 is equal to or greater than the date on sheet2
by the number of tolerance days, it is a match.

I want to set a variable for the date (month and day) on sheet2 and compare
it to a variable for the date on sheet1.

I'm having trouble getting started since the date on sheet 2 is only month
and day. I'm not sure how to handle it when the date on sheet2 is the end of
one month and the date on sheet1 is one of the first few days in the next
month.

Is there some way to compare the month and day and calculate the difference
between another month and day?

Thanks. Any help getting started would be greatly appreciated.........






--
JT



All times are GMT +1. The time now is 06:58 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com