PC Review


Reply
Thread Tools Rate Thread

Change when function is called

 
 
Darren
Guest
Posts: n/a
 
      2nd Jun 2009
Hi,
I have the following function that is called everytime a worksheet is
changed, I would like to only allow the function to run on certain
worksheets because I have code on other sheets and having this
function called makes it really slow, so is there a way to specify
which sheets can call the function ?

Thanks in advance

D

**FUNCTION**
Option Base 1
Function UniqueItems(ArrayIn, Optional Count As Variant) As Variant
Dim Unique() As Variant
Dim u() As Variant
Dim Element As Variant
Dim i As Integer
Dim FoundMatch As Boolean
Dim r As Range
Set r = Application.Caller
If IsMissing(Count) Then Count = True
NumUnique = 0
For Each Element In ArrayIn
FoundMatch = False
For i = 1 To NumUnique
If Element = Unique(i) Or Element = 0 Then
FoundMatch = True
GoTo AddItem
End If
Next i
AddItem:
If Not FoundMatch Then
NumUnique = NumUnique + 1
ReDim Preserve Unique(NumUnique)
Unique(NumUnique) = Element
End If
Next Element
If Count Then
UniqueItems = NumUnique
Else
If NumUnique > r.Count Then
ReDim Preserve Unique(1 To r.Count)
Unique(UBound(Unique)) = (NumUnique - r.Count) + 1 & " more"
UniqueItems = Application.Transpose(Unique)
ElseIf NumUnique < r.Count Then
ReDim Preserve Unique(1 To r.Count)
For i = NumUnique + 1 To r.Count
Unique(i) = ""
Next
UniqueItems = Application.Transpose(Unique)
Else
UniqueItems = Application.Transpose(Unique)
End If
End If
End Function
 
Reply With Quote
 
 
 
 
Patrick Molloy
Guest
Posts: n/a
 
      2nd Jun 2009
you could re-write your function. Using a Dictionary from the Microsoft
Scripting Runtime dll would save lots of work as there is a .EXISTS() method
that would enable you to check if a key already exists...

by populating the dictionary you also get to count the number of items and
get the unique list.



"Darren" <(E-Mail Removed)> wrote in message
news:88e217b9-38f6-4c23-9b6c-(E-Mail Removed)...
> Hi,
> I have the following function that is called everytime a worksheet is
> changed, I would like to only allow the function to run on certain
> worksheets because I have code on other sheets and having this
> function called makes it really slow, so is there a way to specify
> which sheets can call the function ?
>
> Thanks in advance
>
> D
>
> **FUNCTION**
> Option Base 1
> Function UniqueItems(ArrayIn, Optional Count As Variant) As Variant
> Dim Unique() As Variant
> Dim u() As Variant
> Dim Element As Variant
> Dim i As Integer
> Dim FoundMatch As Boolean
> Dim r As Range
> Set r = Application.Caller
> If IsMissing(Count) Then Count = True
> NumUnique = 0
> For Each Element In ArrayIn
> FoundMatch = False
> For i = 1 To NumUnique
> If Element = Unique(i) Or Element = 0 Then
> FoundMatch = True
> GoTo AddItem
> End If
> Next i
> AddItem:
> If Not FoundMatch Then
> NumUnique = NumUnique + 1
> ReDim Preserve Unique(NumUnique)
> Unique(NumUnique) = Element
> End If
> Next Element
> If Count Then
> UniqueItems = NumUnique
> Else
> If NumUnique > r.Count Then
> ReDim Preserve Unique(1 To r.Count)
> Unique(UBound(Unique)) = (NumUnique - r.Count) + 1 & " more"
> UniqueItems = Application.Transpose(Unique)
> ElseIf NumUnique < r.Count Then
> ReDim Preserve Unique(1 To r.Count)
> For i = NumUnique + 1 To r.Count
> Unique(i) = ""
> Next
> UniqueItems = Application.Transpose(Unique)
> Else
> UniqueItems = Application.Transpose(Unique)
> End If
> End If
> End Function


 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      2nd Jun 2009
Darren

Just after the declaration part add the below code. You can mention the
valid sheets in the String variable...Function will exit if the sheet is not
found in the list.

Dim strSheets as String
strSheets = "Sheet1,Sheet2,Sheet3"
If InStr(1, "," & strSheets & ",", "," & ActiveSheet.Name & _
",", vbTextCompare) = 0 Then Exit Function

If this post helps click Yes
---------------
Jacob Skaria


"Darren" wrote:

> Hi,
> I have the following function that is called everytime a worksheet is
> changed, I would like to only allow the function to run on certain
> worksheets because I have code on other sheets and having this
> function called makes it really slow, so is there a way to specify
> which sheets can call the function ?
>
> Thanks in advance
>
> D
>
> **FUNCTION**
> Option Base 1
> Function UniqueItems(ArrayIn, Optional Count As Variant) As Variant
> Dim Unique() As Variant
> Dim u() As Variant
> Dim Element As Variant
> Dim i As Integer
> Dim FoundMatch As Boolean
> Dim r As Range
> Set r = Application.Caller
> If IsMissing(Count) Then Count = True
> NumUnique = 0
> For Each Element In ArrayIn
> FoundMatch = False
> For i = 1 To NumUnique
> If Element = Unique(i) Or Element = 0 Then
> FoundMatch = True
> GoTo AddItem
> End If
> Next i
> AddItem:
> If Not FoundMatch Then
> NumUnique = NumUnique + 1
> ReDim Preserve Unique(NumUnique)
> Unique(NumUnique) = Element
> End If
> Next Element
> If Count Then
> UniqueItems = NumUnique
> Else
> If NumUnique > r.Count Then
> ReDim Preserve Unique(1 To r.Count)
> Unique(UBound(Unique)) = (NumUnique - r.Count) + 1 & " more"
> UniqueItems = Application.Transpose(Unique)
> ElseIf NumUnique < r.Count Then
> ReDim Preserve Unique(1 To r.Count)
> For i = NumUnique + 1 To r.Count
> Unique(i) = ""
> Next
> UniqueItems = Application.Transpose(Unique)
> Else
> UniqueItems = Application.Transpose(Unique)
> End If
> End If
> End Function
>

 
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
Re: Add-in function sometimes gets called twice Charles Williams Microsoft Excel Discussion 0 8th Jan 2010 12:39 AM
What is this function called?? jPaulB Microsoft Excel Misc 3 4th Aug 2006 08:33 PM
XLA function called twice Nigel Microsoft Excel Programming 0 16th Nov 2004 10:46 AM
Re: Possible to know who has called a function ? Robin Hammond Microsoft Excel Programming 0 5th Mar 2004 12:33 AM
How could the IRR function be called in C# Burton Wilkins Microsoft Dot NET Framework 0 25th Sep 2003 11:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:38 PM.