Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 371
Default Help required with UDF

I have written a UDF as below:

Function Sum_PayYr(YearEnd As Range)
'Sums the numbers from all quarters belonging to a given year

Dim sum_Value As Double
Dim temp_Array As Range
Dim num_Cols As Integer
Dim i As Integer
Dim k As Range

num_Cols = Year(YearEnd.Value) - 1975
sum_Value = 0

For i = 0 To num_Cols
temp_Array = Range(YearEnd.Offset(1 + 4 * i, -i), YearEnd.Offset(4 + 4 *
i, -i))
For Each k In temp_Array
sum_Value = sum_Value + k.Value
Next k
Next i

Sum_PayYr = sum_Value

End Function

The idea is that this will grab 4 cells from the column directly underneath
the starting cell (containing a date value), add their values to the return
value, then offset by -1 columns and 4 rows and repeat the process, looping
through num_Cols times. Everything initialises ok until temp_Array, at which
point execution appears to stop without any exception notice for some reason.
Does anyone have any idea why this is happening?

--
There are 10 types of people in the world - those who understand binary and
those who don't.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 371
Default Help required with UDF

I've managed to get this running now, addressed the omission of the 'Set'
statement and modified the function to use number of columns rather than
number of years. Cheers :)
--
There are 10 types of people in the world - those who understand binary and
those who don't.


"Geoff" wrote:

I have written a UDF as below:

Function Sum_PayYr(YearEnd As Range)
'Sums the numbers from all quarters belonging to a given year

Dim sum_Value As Double
Dim temp_Array As Range
Dim num_Cols As Integer
Dim i As Integer
Dim k As Range

num_Cols = Year(YearEnd.Value) - 1975
sum_Value = 0

For i = 0 To num_Cols
temp_Array = Range(YearEnd.Offset(1 + 4 * i, -i), YearEnd.Offset(4 + 4 *
i, -i))
For Each k In temp_Array
sum_Value = sum_Value + k.Value
Next k
Next i

Sum_PayYr = sum_Value

End Function

The idea is that this will grab 4 cells from the column directly underneath
the starting cell (containing a date value), add their values to the return
value, then offset by -1 columns and 4 rows and repeat the process, looping
through num_Cols times. Everything initialises ok until temp_Array, at which
point execution appears to stop without any exception notice for some reason.
Does anyone have any idea why this is happening?

--
There are 10 types of people in the world - those who understand binary and
those who don't.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Help required with UDF

Since temp_array is a range, you'll need to use "Set"

Set Temp_array = range(....

Geoff wrote:

I have written a UDF as below:

Function Sum_PayYr(YearEnd As Range)
'Sums the numbers from all quarters belonging to a given year

Dim sum_Value As Double
Dim temp_Array As Range
Dim num_Cols As Integer
Dim i As Integer
Dim k As Range

num_Cols = Year(YearEnd.Value) - 1975
sum_Value = 0

For i = 0 To num_Cols
temp_Array = Range(YearEnd.Offset(1 + 4 * i, -i), YearEnd.Offset(4 + 4 *
i, -i))
For Each k In temp_Array
sum_Value = sum_Value + k.Value
Next k
Next i

Sum_PayYr = sum_Value

End Function

The idea is that this will grab 4 cells from the column directly underneath
the starting cell (containing a date value), add their values to the return
value, then offset by -1 columns and 4 rows and repeat the process, looping
through num_Cols times. Everything initialises ok until temp_Array, at which
point execution appears to stop without any exception notice for some reason.
Does anyone have any idea why this is happening?

--
There are 10 types of people in the world - those who understand binary and
those who don't.


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Help required with UDF

Oops. Didn't see you found the answer until after I posted.

Geoff wrote:

I've managed to get this running now, addressed the omission of the 'Set'
statement and modified the function to use number of columns rather than
number of years. Cheers :)
--
There are 10 types of people in the world - those who understand binary and
those who don't.

"Geoff" wrote:

I have written a UDF as below:

Function Sum_PayYr(YearEnd As Range)
'Sums the numbers from all quarters belonging to a given year

Dim sum_Value As Double
Dim temp_Array As Range
Dim num_Cols As Integer
Dim i As Integer
Dim k As Range

num_Cols = Year(YearEnd.Value) - 1975
sum_Value = 0

For i = 0 To num_Cols
temp_Array = Range(YearEnd.Offset(1 + 4 * i, -i), YearEnd.Offset(4 + 4 *
i, -i))
For Each k In temp_Array
sum_Value = sum_Value + k.Value
Next k
Next i

Sum_PayYr = sum_Value

End Function

The idea is that this will grab 4 cells from the column directly underneath
the starting cell (containing a date value), add their values to the return
value, then offset by -1 columns and 4 rows and repeat the process, looping
through num_Cols times. Everything initialises ok until temp_Array, at which
point execution appears to stop without any exception notice for some reason.
Does anyone have any idea why this is happening?

--
There are 10 types of people in the world - those who understand binary and
those who don't.


--

Dave Peterson
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
VB help required adeel via OfficeKB.com Excel Discussion (Misc queries) 2 July 3rd 09 03:04 PM
Help required Hassan Excel Worksheet Functions 2 July 29th 08 05:38 AM
help required please [email protected] Excel Programming 1 May 18th 07 02:00 PM
VBA Help required..........! Thyagaraj Excel Programming 1 July 1st 06 11:06 AM
Help required...... Duncan Excel Discussion (Misc queries) 2 February 17th 05 10:26 PM


All times are GMT +1. The time now is 04:39 AM.

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

About Us

"It's about Microsoft Excel"