Comparing Dates

G

Guest

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

Guest

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
 
G

Gleam

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top