PC Review


Reply
Thread Tools Rate Thread

Can you improve the performance on my user defined function?

 
 
Law
Guest
Posts: n/a
 
      2nd May 2007
Hi All

I have 5 sheets. 4 of the sheets contain forecast data and 1 of them
contains Actual data.

The forecast sheets have the following formula under the required
columns at the bottom of the data range for calculating the
corresponding actual total when the forecast data is autofiltered:

'=SUM((SpecialLookUp(SubRangeSingleCol(QT_SAM_FCT4_1, 3), ACT!
QT_SAM_ACT_1, 3, 6)))

Essentially what I am doing here is retrieving the filtered row IDs in
the Forecast sheet and then looking up these rowIds in the Actual data
sheet and then summing these req. values of the Actual data in the
Forecast sheet.

The problem occurs when I filter the Actual data it takes ages for
Excel to re-calculate these lookup formulas in the Forecast sheets.

Any suggestion as to how to make the calculation quicker would be more
than welcome.




Function SubRangeSingleCol(QueryRange As Range, Col As Integer) As
Range

For Each cl In QueryRange.Columns(Col).Cells

If (cl.EntireRow.Hidden = False) Then
If SubRangeSingleCol Is Nothing Then
Set SubRangeSingleCol = cl
Else
Set SubRangeSingleCol = Union(SubRangeSingleCol, cl)
End If
End If

Next cl


End Function

Function SpecialLookUp(IDInputRange As Range, LookUpRange As Range,
IDCol As Integer, ResultCol As Integer) As Range

For Each cl In IDInputRange.Cells
For Each cl1 In LookUpRange.Columns(IDCol).Cells
If SpecialLookUp Is Nothing Then
If cl1.Value = cl.Value Then
Set SpecialLookUp = cl1.Offset(0, ResultCol)
End If
Else
If cl1.Value = cl.Value Then
Set SpecialLookUp = Union(SpecialLookUp,
cl1.Offset(0, ResultCol - IDCol))
End If
End If
Next cl1

Next cl

End Function


Regards

Lars

 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      2nd May 2007
Hi Lars,

It's difficult to follow quite what you are doing, following comments based
merely on a glance of your functions.

The most obvious possibility that would make your functions slow is multiple
Union's, these become exponentially slower with many non contiguous areas,
if that's what your filtered range gives. Your first function, I think,
could be done in a single line -

On error resume next
Set SubRangeSingleCol = _
QueryRange.Columns(Col).SpecialCells(xlCellTypeVisible)

Not sure what's going overall with your second function and cell formula.
But it might be quicker to do the Sum in the function rather than returning
a range to the cell, to avoid the union.

It's worth declaring your variables, eg Dim cl as Range. Also use a
temporary range variable, when done assign to the function, eg -
Dim rngOutput as
' build rngOutput
Set SpecialLookUp = rng

If you care to explain with references (names, address's etc) and simple
data samples that can easily be recreated without seeing what you have,
others may well be able to suggest more ideas and perhaps a different
approach.

Regards,
Peter T


"Law" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All
>
> I have 5 sheets. 4 of the sheets contain forecast data and 1 of them
> contains Actual data.
>
> The forecast sheets have the following formula under the required
> columns at the bottom of the data range for calculating the
> corresponding actual total when the forecast data is autofiltered:
>
> '=SUM((SpecialLookUp(SubRangeSingleCol(QT_SAM_FCT4_1, 3), ACT!
> QT_SAM_ACT_1, 3, 6)))
>
> Essentially what I am doing here is retrieving the filtered row IDs in
> the Forecast sheet and then looking up these rowIds in the Actual data
> sheet and then summing these req. values of the Actual data in the
> Forecast sheet.
>
> The problem occurs when I filter the Actual data it takes ages for
> Excel to re-calculate these lookup formulas in the Forecast sheets.
>
> Any suggestion as to how to make the calculation quicker would be more
> than welcome.
>
>
>
>
> Function SubRangeSingleCol(QueryRange As Range, Col As Integer) As
> Range
>
> For Each cl In QueryRange.Columns(Col).Cells
>
> If (cl.EntireRow.Hidden = False) Then
> If SubRangeSingleCol Is Nothing Then
> Set SubRangeSingleCol = cl
> Else
> Set SubRangeSingleCol = Union(SubRangeSingleCol, cl)
> End If
> End If
>
> Next cl
>
>
> End Function
>
> Function SpecialLookUp(IDInputRange As Range, LookUpRange As Range,
> IDCol As Integer, ResultCol As Integer) As Range
>
> For Each cl In IDInputRange.Cells
> For Each cl1 In LookUpRange.Columns(IDCol).Cells
> If SpecialLookUp Is Nothing Then
> If cl1.Value = cl.Value Then
> Set SpecialLookUp = cl1.Offset(0, ResultCol)
> End If
> Else
> If cl1.Value = cl.Value Then
> Set SpecialLookUp = Union(SpecialLookUp,
> cl1.Offset(0, ResultCol - IDCol))
> End If
> End If
> Next cl1
>
> Next cl
>
> End Function
>
>
> Regards
>
> Lars
>



 
Reply With Quote
 
Law
Guest
Posts: n/a
 
      3rd May 2007
On May 2, 12:04 pm, "Peter T" <peter_t@discussions> wrote:
> Hi Lars,
>
> It's difficult to follow quite what you are doing, following comments based
> merely on a glance of your functions.
>
> The most obvious possibility that would make your functions slow is multiple
> Union's, these become exponentially slower with many non contiguous areas,
> if that's what your filtered range gives. Your first function, I think,
> could be done in a single line -
>
> On error resume next
> Set SubRangeSingleCol = _
> QueryRange.Columns(Col).SpecialCells(xlCellTypeVisible)
>
> Not sure what's going overall with your second function and cell formula.
> But it might be quicker to do the Sum in the function rather than returning
> a range to the cell, to avoid the union.
>
> It's worth declaring your variables, eg Dim cl as Range. Also use a
> temporary range variable, when done assign to the function, eg -
> Dim rngOutput as
> ' build rngOutput
> Set SpecialLookUp = rng
>
> If you care to explain with references (names, address's etc) and simple
> data samples that can easily be recreated without seeing what you have,
> others may well be able to suggest more ideas and perhaps a different
> approach.
>
> Regards,
> Peter T
>
> "Law" <Lawrence.Colo...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > Hi All

>
> > I have 5 sheets. 4 of the sheets contain forecast data and 1 of them
> > contains Actual data.

>
> > The forecast sheets have the following formula under the required
> > columns at the bottom of the data range for calculating the
> > corresponding actual total when the forecast data is autofiltered:

>
> > '=SUM((SpecialLookUp(SubRangeSingleCol(QT_SAM_FCT4_1, 3), ACT!
> > QT_SAM_ACT_1, 3, 6)))

>
> > Essentially what I am doing here is retrieving the filtered row IDs in
> > the Forecast sheet and then looking up these rowIds in the Actual data
> > sheet and then summing these req. values of the Actual data in the
> > Forecast sheet.

>
> > The problem occurs when I filter the Actual data it takes ages for
> > Excel to re-calculate these lookup formulas in the Forecast sheets.

>
> > Any suggestion as to how to make the calculation quicker would be more
> > than welcome.

>
> > Function SubRangeSingleCol(QueryRange As Range, Col As Integer) As
> > Range

>
> > For Each cl In QueryRange.Columns(Col).Cells

>
> > If (cl.EntireRow.Hidden = False) Then
> > If SubRangeSingleCol Is Nothing Then
> > Set SubRangeSingleCol = cl
> > Else
> > Set SubRangeSingleCol = Union(SubRangeSingleCol, cl)
> > End If
> > End If

>
> > Next cl

>
> > End Function

>
> > Function SpecialLookUp(IDInputRange As Range, LookUpRange As Range,
> > IDCol As Integer, ResultCol As Integer) As Range

>
> > For Each cl In IDInputRange.Cells
> > For Each cl1 In LookUpRange.Columns(IDCol).Cells
> > If SpecialLookUp Is Nothing Then
> > If cl1.Value = cl.Value Then
> > Set SpecialLookUp = cl1.Offset(0, ResultCol)
> > End If
> > Else
> > If cl1.Value = cl.Value Then
> > Set SpecialLookUp = Union(SpecialLookUp,
> > cl1.Offset(0, ResultCol - IDCol))
> > End If
> > End If
> > Next cl1

>
> > Next cl

>
> > End Function

>
> > Regards

>
> > Lars- Hide quoted text -

>
> - Show quoted text -


Thanks Peter

Using a temp variable in the function made things a lot quicker.

Cheers

L

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using dcount function within user-defined worksheet function =?Utf-8?B?cG9uZ3RoYWk=?= Microsoft Excel Programming 3 15th Jan 2007 09:55 AM
Excel - User Defined Function Error: This function takes no argume =?Utf-8?B?QnJ1Y2VJbkNhbGdhcnk=?= Microsoft Excel Programming 3 23rd Aug 2006 08:53 PM
Need to open the Function Arguments window from VBA for a user defined function. korrin.anderson@gmail.com Microsoft Excel Programming 0 20th Jun 2006 03:53 PM
User Defined Function does not appear on Function listing =?Utf-8?B?SC5QLkhvZWllLCBOb3J3YXk=?= Microsoft Excel Programming 1 21st Mar 2006 10:27 AM
User-Defined Function pre-empting Built-in Function? How to undo???? MarWun Microsoft Excel Programming 1 6th Aug 2003 09:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:54 PM.