PC Review


Reply
Thread Tools Rate Thread

Date as variable vs in excel worksheet

 
 
don
Guest
Posts: n/a
 
      11th Apr 2007
I have a variable vdate defined as
vdate = "01/01/2007"

When I compare it to a date on a worksheet of the same date, ie,
1/1/2007, Excel says they aren't equal.
If I check both using ISdate() it shows True.

If I change the vdate = 1/1/2007 (no quote marks) it considers it a
division calculation.

If I assign vdate to a cell, ie.
cells(1,1)=vdate

and then compare cells(1,1) to the dates on the spreadsheet it finds
them to be equal.

How do I compare the vdate to the dates on the worksheet without going
thru the process of saving it to a cell.

Thanks
Don
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      11th Apr 2007
One way is to assign vdate as

vdate = #1/1/2007#



--
Hope that helps.

Vergel Adriano


"don" wrote:

> I have a variable vdate defined as
> vdate = "01/01/2007"
>
> When I compare it to a date on a worksheet of the same date, ie,
> 1/1/2007, Excel says they aren't equal.
> If I check both using ISdate() it shows True.
>
> If I change the vdate = 1/1/2007 (no quote marks) it considers it a
> division calculation.
>
> If I assign vdate to a cell, ie.
> cells(1,1)=vdate
>
> and then compare cells(1,1) to the dates on the spreadsheet it finds
> them to be equal.
>
> How do I compare the vdate to the dates on the worksheet without going
> thru the process of saving it to a cell.
>
> Thanks
> Don
>

 
Reply With Quote
 
=?Utf-8?B?Sk1C?=
Guest
Posts: n/a
 
      11th Apr 2007
Isdate means "1/1/07" can be converted to a date, not that it actually is a
date.

Check VBA help for CDate function to convert it, or define your variable as
a date (date literals are enclosed w/#).

Const vdate As Date = #1/1/2007#


"don" wrote:

> I have a variable vdate defined as
> vdate = "01/01/2007"
>
> When I compare it to a date on a worksheet of the same date, ie,
> 1/1/2007, Excel says they aren't equal.
> If I check both using ISdate() it shows True.
>
> If I change the vdate = 1/1/2007 (no quote marks) it considers it a
> division calculation.
>
> If I assign vdate to a cell, ie.
> cells(1,1)=vdate
>
> and then compare cells(1,1) to the dates on the spreadsheet it finds
> them to be equal.
>
> How do I compare the vdate to the dates on the worksheet without going
> thru the process of saving it to a cell.
>
> Thanks
> Don
>

 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      11th Apr 2007
Don,
Others have pointed out ways of dealing with this, but now you see the
benefit of setting the correct data type for your variable and not relying
on Variants, which can hold any type.
If you had:
Dim vdate as Date
depending on what/how you were doing, you would either get an error upon
assignment or correct comparison.

Also note that dates are basically Doubles. Hence you can:
Dim vdate As Date

vdate = #1/1/2007#
'Or
'vdate = "1/1/2007"

MsgBox vdate + 1

But this will error with a Type Mismatch
Dim vdate 'As Variant

vdate = "1/1/2007"
'vdate now holds a string
'This will error
MsgBox vdate + 1

How VBA deals with days v. months in a date like 1/1/2007 is another matter.
I always try to be as explicit as possible. DateSerial is useful for this.

NickHK

"don" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have a variable vdate defined as
> vdate = "01/01/2007"
>
> When I compare it to a date on a worksheet of the same date, ie,
> 1/1/2007, Excel says they aren't equal.
> If I check both using ISdate() it shows True.
>
> If I change the vdate = 1/1/2007 (no quote marks) it considers it a
> division calculation.
>
> If I assign vdate to a cell, ie.
> cells(1,1)=vdate
>
> and then compare cells(1,1) to the dates on the spreadsheet it finds
> them to be equal.
>
> How do I compare the vdate to the dates on the worksheet without going
> thru the process of saving it to a cell.
>
> Thanks
> Don



 
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
How to hide rows in an Excel worksheet based on a variable Dave Osborn Microsoft Excel Misc 2 21st Jul 2009 10:11 PM
Excel Named Formula Weakly Interacts with a Variable Range on the Worksheet - Re-Visit tkt_tang@hotmail.com Microsoft Excel Misc 5 6th Sep 2007 06:42 PM
Macro to name a worksheet with a date variable? =?Utf-8?B?S2V2aW5QaW5EQw==?= Microsoft Excel Programming 3 15th Aug 2007 12:39 AM
Storing variable values in Excel worksheet Henry Stockbridge Microsoft Excel Programming 3 25th Apr 2006 02:38 PM
Excel Named Formula Weakly Interacts with a Variable Range on the Worksheet tkt_tang@hotmail.com Microsoft Excel Discussion 6 6th Dec 2005 04:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:48 AM.