Difficulty Subtracting Dates in VBA

RunTime91

Active Member
Joined
Aug 30, 2012
Messages
290
Office Version
  1. 365
I can't believe I'm here for this...but I can't get this to work

Code:
Dim Rng As Range
Dim Rng1 As Range

Set Rng = Worksheets("Sheet1").Range("D4")
Set Rng1 = Worksheets("Sheet1").Range("D4") - 2

The value in "D4" is a date - The error I'm getting is an 'Object Required' Error on the Set Rng1... line

Clearly I'm missing something - but I'm not seeing it

Thanks in advance for the humility about to come my way :)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Rng1 is a Range, not a value. So you cannot subtract 2 from a range (and use you the "Set" command for setting objects, such as ranges - values are not objects!).

Try something like:
Code:
Dim Rng As Range
Dim Val1 as Double

Set Rng = Worksheets("Sheet1").Range("D4")
Set Val1 = Rng.Value - 2
 
Last edited:
Upvote 0
...And there it is...

Geez, Sorry to bother you for something so menial, Joe

But I'm glad you were there, because I was running a marathon around that simple solution

Thank You again, Joe...
 
Upvote 0
You are welcome!
No worries, we all sometimes overlook the little things (feel free to blame it Monday!).
 
Upvote 0
eh, doesn't this code give a similar error to the original code.:)
Code:
Dim Rng As Range
Dim Val1 as Double

Set Rng = Worksheets("Sheet1").Range("D4")
Set Val1 = Rng.Value - 2
You only use Set with objects, and Val1 is not an object - try this.
Code:
Dim Rng As Range
Dim Val1 as Double

    Set Rng = Worksheets("Sheet1").Range("D4")
    Val1 = Rng.Value - 2
Or if you want to subtract 2 days from the date in D4 try this.
Code:
Dim Rng As Range

    Set Rng = Worksheets("Sheet1").Range("D4")
    Rng.Value = Rng.Value - 2
 
Upvote 0
eh, doesn't this code give a similar error to the original code.
Ironic! Talk about overlooking the obvious!
I forgot to remove the "Set" command in the value calculation.

Good catch, Norie. I will blame that on Monday!
;)
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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