PC Review


Reply
Thread Tools Rate Thread

Compare Dates in vba

 
 
=?Utf-8?B?Qnl0aHN4LUFkZGFnaW8=?=
Guest
Posts: n/a
 
      24th May 2007
Dates seem to be giving me trouble.

In my macro process I open an excel file that is created automatically each
month in a batch process. The file has the date for which the report was run
for printed in cell A1. I would like my macro to check that the date
printed in the file equals the last day of the previous month.

Basically this... even though I know it is very wrong.

Date1 = DATE(YEAR(TODAY()),MONTH(TODAY()),0)
Date2 = windows(filename).cells("A1").value

If Date1 = Date2 Then

Hopefully this will help me finally learn how to handle dates.
Thanks.
 
Reply With Quote
 
 
 
 
Keith74
Guest
Posts: n/a
 
      24th May 2007
Maybe something like this?

Public Sub MatchDates()

If Date = Workbooks("book1").Worksheets("sheet1").Cells(1,
1).Value Then
MsgBox "Dates match"
End If

End Sub

hth

Keith

 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      24th May 2007
I don't think you are too far off. You need the dateserrial function and now

DateSerial(year, month, day)

Date1 = DateSerial(Year(Now()), Month(Now()), 0)
Date2 = windows(filename).cells("A1").value
If Date1 = Date2 Then


"Bythsx-Addagio" wrote:

> Dates seem to be giving me trouble.
>
> In my macro process I open an excel file that is created automatically each
> month in a batch process. The file has the date for which the report was run
> for printed in cell A1. I would like my macro to check that the date
> printed in the file equals the last day of the previous month.
>
> Basically this... even though I know it is very wrong.
>
> Date1 = DATE(YEAR(TODAY()),MONTH(TODAY()),0)
> Date2 = windows(filename).cells("A1").value
>
> If Date1 = Date2 Then
>
> Hopefully this will help me finally learn how to handle dates.
> Thanks.

 
Reply With Quote
 
=?Utf-8?B?Qnl0aHN4LUFkZGFnaW8=?=
Guest
Posts: n/a
 
      24th May 2007
Thanks for the first part that seems to work.
But when I try to assign the value of Cells("A1") to Date2 I get Run-Time
error 438.
Object doesn't support this property or method.

Date2 = Windows(fNamestring).Cells("A1").Value

Here is the whole part
********************
Date1 = DateSerial(Year(Now()), Month(Now()), 0)
Date2 = Windows(fNamestring).Cells("A1").Value

If Date1 = Date2 Then
********************

Should I be declaring Date2 as a Date, or String?


Thanks again guys,

"Joel" wrote:

> I don't think you are too far off. You need the dateserrial function and now
>
> DateSerial(year, month, day)
>
> Date1 = DateSerial(Year(Now()), Month(Now()), 0)
> Date2 = windows(filename).cells("A1").value
> If Date1 = Date2 Then
>
>
> "Bythsx-Addagio" wrote:
>
> > Dates seem to be giving me trouble.
> >
> > In my macro process I open an excel file that is created automatically each
> > month in a batch process. The file has the date for which the report was run
> > for printed in cell A1. I would like my macro to check that the date
> > printed in the file equals the last day of the previous month.
> >
> > Basically this... even though I know it is very wrong.
> >
> > Date1 = DATE(YEAR(TODAY()),MONTH(TODAY()),0)
> > Date2 = windows(filename).cells("A1").value
> >
> > If Date1 = Date2 Then
> >
> > Hopefully this will help me finally learn how to handle dates.
> > Thanks.

 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      24th May 2007
Range uses "A1" not cells for cells(1,1)
need Workbook instead of windows
need to add sheets.

Workbooks(fNamestring).Sheets("sheet1").Range("A1").Value

"Bythsx-Addagio" wrote:

> Thanks for the first part that seems to work.
> But when I try to assign the value of Cells("A1") to Date2 I get Run-Time
> error 438.
> Object doesn't support this property or method.
>
> Date2 = Windows(fNamestring).Cells("A1").Value
>
> Here is the whole part
> ********************
> Date1 = DateSerial(Year(Now()), Month(Now()), 0)
> Date2 = Windows(fNamestring).Cells("A1").Value
>
> If Date1 = Date2 Then
> ********************
>
> Should I be declaring Date2 as a Date, or String?
>
>
> Thanks again guys,
>
> "Joel" wrote:
>
> > I don't think you are too far off. You need the dateserrial function and now
> >
> > DateSerial(year, month, day)
> >
> > Date1 = DateSerial(Year(Now()), Month(Now()), 0)
> > Date2 = windows(filename).cells("A1").value
> > If Date1 = Date2 Then
> >
> >
> > "Bythsx-Addagio" wrote:
> >
> > > Dates seem to be giving me trouble.
> > >
> > > In my macro process I open an excel file that is created automatically each
> > > month in a batch process. The file has the date for which the report was run
> > > for printed in cell A1. I would like my macro to check that the date
> > > printed in the file equals the last day of the previous month.
> > >
> > > Basically this... even though I know it is very wrong.
> > >
> > > Date1 = DATE(YEAR(TODAY()),MONTH(TODAY()),0)
> > > Date2 = windows(filename).cells("A1").value
> > >
> > > If Date1 = Date2 Then
> > >
> > > Hopefully this will help me finally learn how to handle dates.
> > > Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare dates Vic Microsoft Excel Misc 2 1st May 2009 03:30 AM
Re: compare dates Fred Smith Microsoft Excel Misc 0 30th Apr 2009 02:08 PM
compare 2 tables of dates to find the preceding dates Babi Microsoft Excel Worksheet Functions 3 28th Oct 2008 05:52 AM
Compare dates Tom Microsoft Access Queries 2 9th Oct 2003 09:44 PM
Re: how to compare two dates Rick Brandt Microsoft Access Getting Started 0 14th Aug 2003 06:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:35 PM.