Dim Date Help.

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
Hi everyone,

I don't understand this logic. When I dimension a date such as the following satement:
(1)
Dim StartDate As Date
Set StartDate = Thisworkbook.Worksheets("Report").Range("A1").Value

I got a error message that says "Compile error, Object required" What does this mean? However, when a date dimensioned as Variant and without SET statement then it runs OK. Also, if I set StartDate = Thisworkbook.Worksheets("Report").Range.Value without the Dim Statement. This works as well.
(2) and (3) work , Why does (1) not work?:confused:

Thanks for your help in advance.



(2)
Dim StartDate As Variant
(3)
StartDate = Thisworkbook.Worksheets("Report").Range("A1").Value
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try
Code:
Sub Test()
Dim StartDate As Date
StartDate = ThisWorkbook.Worksheets("Report").Range("A1").Value
End Sub

Biz
 
Upvote 0
Hi everyone,

I don't understand this logic. When I dimension a date such as the following satement:
(1)
Dim StartDate As Date
Set StartDate = Thisworkbook.Worksheets("Report").Range("A1").Value

I got a error message that says "Compile error, Object required" What does this mean?

It means that you need an Object to Set a reference to. A Date is not an Object, rather, its just a Value. An example of an Object would be the cell itself (that is, a Range Object). Does that make sense?

Mark
 
Upvote 0
Biz,

Thanks for your help. It your code works.
But why is it when I use the "SET" with Dim statement the code doesn't work?

Sub Test ()
Dim StartDate As Date
StartDate = Thisworkbook.Worksheets("Report").Range("A1").Value
End Sub()

I am confused as to why sometimes a Dim statement must follow by a SET statement. Yet, in this example when I incorporate the SET statement the code doesn't work.

Sub Test ()
Dim StartDate As Date
Set StartDate = Thisworkbook.Worksheets("Report").Range("A1").Value
End Sub()
 
Upvote 0
GTO, Thanks for the explanation. I think I get that a Date cannot be an object, but I try dimensioning it as Variant and it doesn't work and it says " Type mismatch ". Do you know why?

Sub Test()
Dim StartDate As Variant
Set StartDate = Thisworkbook.worksheet("Report").Value
End Sub()
 
Last edited:
Upvote 0
GTO, Thanks for the explanation. I think I get that a Date cannot be an object, but I try dimensioning it as Variant and it doesn't work and it says " Type mismatch ". Do you know why?

Sub Test()
Dim StartDate As Variant
Set StartDate = Thisworkbook.worksheet("Report").Value
End Sub()

That is the point. A Date cannot be an Object, thus you cannot use the Set Statement and Set a reference to it. You can assign the value of the Date to a variable, as Biz showed.
 
Upvote 0
PS. - Dimensioning or Declaring the variable as a Variant is simply hiding the faulty code until the Set statement attempts to execute, as a Variant could be referring to an Object.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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