Problem using named range in Offset

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I thought I understood this from some previous discussions here, but apparently I do not.

This table shows three different ways to calculate the number of days between two dates. Column C has been assigned the name "Date".

The first one (Col D), uses relative cell addresses. The second uses the named range for the first term, but a cell reference in the Offset call. Both of these work.

The third one, which uses the named range in the Offset call, gets an error.

R/CCDEFGHI
5DateDaysFormulaDaysFormulaDaysFormula
6
4/06/20​
5D6: =C6-C75F6: =Date-OFFSET(C6,1,0)
#REF!​
H6: =Date-(OFFSET(Date,1,0))
7
4/01/20​
40D7: =C7-C840F7: =Date-OFFSET(C7,1,0)
#REF!​
H7: =Date-(OFFSET(Date,1,0))
8
2/21/20​
98D8: =C8-C998F8: =Date-OFFSET(C8,1,0)
#REF!​
H8: =Date-(OFFSET(Date,1,0))
9
11/15/19​
n/an/an/an/an/a

I tried several variations that I thought were suggested by various people here, but none of them work.

The expression
VBA Code:
=Date-(OFFSET(+Date,1,0))
, gets an error.

My attempt to use intersect operator,
VBA Code:
=Date-(row(H6)+1 Date)
, gets an error.

Is there a simple expression to subtract the date in the previous row from the one in the current row that uses only the named range (no cell references)?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
See if this works,

=Date-(OFFSET(Date,1,0,1,1))

I Tried copying your sample to excel but I'm getting #VALUE! instead of #REF! which is because the named range refers to more than 1 cell, not sure why I get different a error to you, perhaps it's the way that you have defined the name.
 
Upvote 0
The proverbial penny has just dropped, if you have defined 'Date' as = $C:$C then it will return #REF because offsetting an entire column by 1 row takes you off of the bottom of the sheet.
C:C is the same as C1:C1048576 a 1 row offset would be C2:C1048577. As C1048577 does not exist, it creates the #REF! error.

With this in mind, a revision to the formula that I suggested which I think should do the trick.

=Date-(OFFSET(Date,ROW(),0,1,1))

or

=Date-INDEX(Date,ROW()+1)

Both should yield the same result.
 
Upvote 0
Solution
See if this works,

=Date-(OFFSET(Date,1,0,1,1))
Nope. It returns the value in the Date column in that row. For row 6, I get 43927, which is the internal number for 4/06/20. I tried just =OFFSET(Date,1,0,1,1). It returns Zero,
 
Upvote 0
The proverbial penny has just dropped, if you have defined 'Date' as = $C:$C then it will return #REF because offsetting an entire column by 1 row takes you off of the bottom of the sheet.
C:C is the same as C1:C1048576 a 1 row offset would be C2:C1048577. As C1048577 does not exist, it creates the #REF! error.
OK, if you say so. :confused:

With this in mind, a revision to the formula that I suggested which I think should do the trick.

=Date-(OFFSET(Date,ROW(),0,1,1))

Yep :)

or

=Date-INDEX(Date,ROW()+1)

Both should yield the same result.
Yes!!! :):biggrin:(y)

I like the second one better as I understand it better.

Thank you
 
Upvote 0
@ JenniferMurphy: jasonb75 has already provided a working solution.

I will try to explain what's wrong with you intersect approach:

- row(H6) returns number 6;
- row(H6)+1 is 7;
- (7 Date) is not a valid intersect operation;
- for this approach to work, it should be =Date-INDIRECT(ROW()+1&":"&ROW()+1) Date
 
Upvote 0
@ JenniferMurphy: jasonb75 has already provided a working solution.

I will try to explain what's wrong with you intersect approach:

- row(H6) returns number 6;
- row(H6)+1 is 7;
- (7 Date) is not a valid intersect operation;
- for this approach to work, it should be =Date-INDIRECT(ROW()+1&":"&ROW()+1) Date
For some reason, I cannot get my head around the Intersect operator.

These two expressions work and give the same answer, the contents of C7:
VBA Code:
=C:C 7:7
=7:7 C:C

But this one gets an error:
VBA Code:
=C:C 7
 
Upvote 0
Intersect needs two ranges as operands.

7 is not a range, same as C is not a range.

7:7 is a range, and C:C is a range, so =C:C 7:7 works, whereas =C:C 7 does not.

Hope this helps.
 
Upvote 0
Intersect needs two ranges as operands.

7 is not a range, same as C is not a range.

7:7 is a range, and C:C is a range, so =C:C 7:7 works, whereas =C:C 7 does not.

Hope this helps.
Ok, but then if Date is the name for C:C, why do these work
VBA Code:
=C:C 7:7
=Date 7:7
and
VBA Code:
=ROW()&":"&ROW()
is "7:7", why do these all get errors?
VBA Code:
=date row():row()
=date row()&":"&row()
=date (row()&":"&row())
 
Upvote 0
This is because when you enter 7:7 in a formula, Excel interprets this as a range (row #7).

If you construct "7:7" as =ROW()&":"&ROW(), its a text string, not row #7, and =range string does not work. For it to work, the string should be wrapped in INDIRECT.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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