Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default OFFSET in Excel VBA Function

I am attempting to move an Excel formula from a worksheet and place it
in an Excel VBA function.

Naively, I thought that I could simply take the worksheet equation,
pass it variables, and add "Application." to all worksheet
function. I took the "Application." out of the VBA function to make it
easier to read.

Unfortunately this does not work for the OFFSET function, but I have
not been able to figure out just why.

Excel Worksheet Function:
=SUMPRODUCT(OFFSET(CI9,-MIN(CF9-Year_First,Fac_Depr),0,MIN(CF9-Year_First+1,Fac_Depr+1),1),
OFFSET(FirstInvFacTangDrillDepr,-MIN(CF9-Year_First,Fac_Depr),0,MIN(CF9-Year_First+1,Fac_Depr+1),1))

CF9,CI9 and named cells are single cells as opposed to multiple ones
such as in a range

Attempted Excel VBA Function:
Function FirstYearDepreciation(Current_Year, Year_First, Fac_Depr,
FirstInvFacTangDrillDepr, Eligible_Depr)
Dim Current_Year As Double
Dim Year_First As Double
Dim Fac_Depr As Integer
Dim FirstInvFacTangDrillDepr As Double
Dim Eligible_Depr As Variant

FirstYearDepreciation = SumProduct( _
Offset(Eligible_Depr, -Min(Current_Year - Year_First, Fac_Depr), 0,
Min(Current_Year - Year_First + 1, Fac_Depr + 1), 1), _
Offset(FirstInvFacTangDrillDepr, -Min(Current_Year - Year_First,
Fac_Depr), 0, Min(Current_Year - Year_First + 1, Fac_Depr + 1), 1))

End Function


Can anyone shed some light on this?

Thanks in advance.

Floyd

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default OFFSET in Excel VBA Function

Your code didn't even compile.
Always useful to do Debug, Compile VBA project.
In this case it would have shown you the syntax errors and some more errors.

Try this:

Function FirstYearDepreciation(Current_Year As Double, _
Year_First As Double, _
Fac_Depr As Integer, _
FirstInvFacTangDrillDepr As Double, _
Eligible_Depr As Variant)

FirstYearDepreciation = _
WorksheetFunction.SumProduct(WorksheetFunction.Off set(Eligible_Depr, _
WorksheetFunction.Min(Current_Year -
Year_First, _
Fac_Depr), _
0, _
WorksheetFunction.Min(Current_Year -
Year_First + 1, _
Fac_Depr + 1), 1), _
WorksheetFunction.Offset(FirstInvFacTangDrillDepr,
_
WorksheetFunction.Min(Current_Year
- Year_First, _

Fac_Depr), _
0, _
WorksheetFunction.Min(Current_Year
- Year_First + 1, _

Fac_Depr + 1), 1))

End Function


RBS

"Floyd" wrote in message
ups.com...
I am attempting to move an Excel formula from a worksheet and place it
in an Excel VBA function.

Naively, I thought that I could simply take the worksheet equation,
pass it variables, and add "Application." to all worksheet
function. I took the "Application." out of the VBA function to make it
easier to read.

Unfortunately this does not work for the OFFSET function, but I have
not been able to figure out just why.

Excel Worksheet Function:
=SUMPRODUCT(OFFSET(CI9,-MIN(CF9-Year_First,Fac_Depr),0,MIN(CF9-Year_First+1,Fac_Depr+1),1),
OFFSET(FirstInvFacTangDrillDepr,-MIN(CF9-Year_First,Fac_Depr),0,MIN(CF9-Year_First+1,Fac_Depr+1),1))

CF9,CI9 and named cells are single cells as opposed to multiple ones
such as in a range

Attempted Excel VBA Function:
Function FirstYearDepreciation(Current_Year, Year_First, Fac_Depr,
FirstInvFacTangDrillDepr, Eligible_Depr)
Dim Current_Year As Double
Dim Year_First As Double
Dim Fac_Depr As Integer
Dim FirstInvFacTangDrillDepr As Double
Dim Eligible_Depr As Variant

FirstYearDepreciation = SumProduct( _
Offset(Eligible_Depr, -Min(Current_Year - Year_First, Fac_Depr), 0,
Min(Current_Year - Year_First + 1, Fac_Depr + 1), 1), _
Offset(FirstInvFacTangDrillDepr, -Min(Current_Year - Year_First,
Fac_Depr), 0, Min(Current_Year - Year_First + 1, Fac_Depr + 1), 1))

End Function


Can anyone shed some light on this?

Thanks in advance.

Floyd


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default OFFSET in Excel VBA Function

Thanks so much for the assistance and certainly the tips on how to
Debug.

I pasted the code in and ran it. I am still getting the same error
message, #VALUE!, but at least it is compiling.

I noticed in another post today that the Mr. Ogilvy recommended using
EVALUATE for SUMPRODUCT. I too tried this with no success.

Cheers.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default OFFSET in Excel VBA Function

OK, if you can give us 5 values for the 5 arguments and tell us what result
you expect
we might be able to figure out what the problem is.

RBS


"Floyd" wrote in message
oups.com...
Thanks so much for the assistance and certainly the tips on how to
Debug.

I pasted the code in and ran it. I am still getting the same error
message, #VALUE!, but at least it is compiling.

I noticed in another post today that the Mr. Ogilvy recommended using
EVALUATE for SUMPRODUCT. I too tried this with no success.

Cheers.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default OFFSET in Excel VBA Function

Certainly.

CI9 - 15.00
CF9 - 2005
Fac_Depr - 7.0


Current_Year As Double - 2005
Year_First As Double - 2005
Fac_Depr As Integer - 7
FirstInvFacTangDrillDepr As Double - 14.29%
Eligible_Depr As Variant - 15.00

Expected Value : 2.14 (0.1429*15)

Thank you.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default OFFSET in Excel VBA Function

That is when sumproduct is used in an array formula mode.

--
Regards,
Tom Ogilvy

"Floyd" wrote in message
oups.com...
Thanks so much for the assistance and certainly the tips on how to
Debug.

I pasted the code in and ran it. I am still getting the same error
message, #VALUE!, but at least it is compiling.

I noticed in another post today that the Mr. Ogilvy recommended using
EVALUATE for SUMPRODUCT. I too tried this with no success.

Cheers.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default OFFSET in Excel VBA Function

Although it compiles I think the problem is with Application.Offset
Not sure if this can work. I thought that Offset had to do with the Range
object
and I don't see any ranges in your code.
This bit for example:

MsgBox WorksheetFunction.Offset(14, WorksheetFunction.Min(2005, 7), _
0, _
WorksheetFunction.Min(2005 - 2005 + 1, 7
+ 1), 1)

gives an error:
object doesn't support this property or method.

You will have to pass the ranges to the function and the whole thing needs a
bit of a work-around.


RBS

"Floyd" wrote in message
ps.com...
Certainly.

CI9 - 15.00
CF9 - 2005
Fac_Depr - 7.0


Current_Year As Double - 2005
Year_First As Double - 2005
Fac_Depr As Integer - 7
FirstInvFacTangDrillDepr As Double - 14.29%
Eligible_Depr As Variant - 15.00

Expected Value : 2.14 (0.1429*15)

Thank you.


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default OFFSET in Excel VBA Function

RBS,

Thank you.

There are no ranges to pass, which as you have figured out is causing
problems.

Cheers.

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default OFFSET in Excel VBA Function

I am still lost. Can you provide some explicit suggestions that I
might try. I have lots of energy around this and will spend whatever
time is necessary to solve this.

The function in the Excel worksheet works just fine. Can someone offer
any things that I might try to pass a single cell into the function,
such that the OFFSET function will work.

Thanks for everyone's assistance yesterday.

Cheers.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
large function result as reference for offset function Z Excel Discussion (Misc queries) 1 May 5th 09 12:55 AM
XL2002 - OFFSET function and LARGE function Trevor Williams Excel Worksheet Functions 3 March 3rd 08 01:40 PM
Offset function in Excel 2003 CLarshtnt Excel Worksheet Functions 7 November 27th 07 02:29 PM
Offset function with nested match function not finding host ss. MKunert Excel Worksheet Functions 1 March 21st 06 10:46 PM
help with offset function Mexage Excel Worksheet Functions 0 May 24th 05 05:18 PM


All times are GMT +1. The time now is 06:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"