LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default efficiency qn: search collection or use on error?

Function elementExists(myCollection As Collection, myName As String) As
Boolean
Dim vArr as Variant
On Error Resume Next
vArr = TypeName(myCollection(myName))
elementExists = (VarType(vArr) < vbEmpty)
End Function

works with both and doesn't use the undocumented VarPtr.

--
Regards,
Tom Ogilvy



"keepITcool" wrote in message
ft.com...
use the VarPtr method in my example.
afaik that works in all cases.





--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Tom Ogilvy wrote :

OK, here is a better example:

Sub ABCD()
Dim myCollection As New Collection
Dim vArr(1 To 10) As String
Dim sStr As String, sSTr1 As String
For i = 1 To 10
sStr = ""
sSTr1 = ""
For j = 1 To 4
sStr = sStr & Chr(Int(Rnd() * 26 + 65))
sSTr1 = sSTr1 & Chr(Int(Rnd() * 26 + 65))
Next
myCollection.Add sStr, sStr
If Rnd() < 0.5 Then
vArr(i) = sStr
Else
vArr(i) = sSTr1
End If
Next
For i = 1 To 10
Debug.Print i, vArr(i), elementExists(myCollection, vArr(i))
Next
End Sub

Function elementExists(myCollection As Collection, myName As String)
As Boolean
On Error Resume Next
elementExists = (myName = myCollection(myName))
End Function

of course this doesn't do objects, but string seemed to be the type
anyway.



 
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
Equation Efficiency? Ithaca Excel Worksheet Functions 1 April 29th 09 01:20 AM
Range efficiency Steven Excel Worksheet Functions 0 December 9th 05 02:15 PM
Runtime error with the sheet collection, which was not there in 20 Laks Excel Programming 1 June 18th 04 11:55 AM
VB Efficiency: Inserting a Row Tippy[_3_] Excel Programming 8 May 26th 04 08:34 PM
.select efficiency Lawlera Excel Programming 4 February 4th 04 02:38 PM


All times are GMT +1. The time now is 04:51 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"