Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Check a collection for objects that may or may not exist within it

Hello,

I'm just wondering if it's possible to check to see if something exists
within a collection before proceeding, I can do it with putting various "On
error goto" statements, but I was just wondering if it's be possible to do
something like the following without having to send the process jumping to
labels.

if aCollection.Item("aKey") < null then
'do something special
else
Msgbox "Yar! No Object 'ere!"
end if

I know that doesn't work, but perhaps there is a way like it?

Thanks in advance!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Check a collection for objects that may or may not exist within it


'--------------------------------------------------------------------------
Public Function ExistsInCollection(pColl, ByVal pKey As String) As Boolean
'--------------------------------------------------------------------------
On Error GoTo NoSuchKey
If VarType(pColl.Item(pKey)) = vbObject Then
' force an error condition if key does not exist
End If
ExistsInCollection = True
Exit Function

NoSuchKey:
ExistsInCollection = False
End Function


an example of usage.

Check if a value in a column is equal to one of a number of values in a
column in another sheet, if so, delete it

Sub testExistsInCollection()
Dim iLastRow As Long
Dim i As Long
Dim colWords As Collection

Set colWords = New Collection

With Worksheets("Sheet2")
For i = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
If .Cells(i, "A").Value < "" Then
colWords.Add i, .Cells(i, "A").Value
End If
Next i
End With

With Worksheets("Sheet1")
iLastRow = .Cells(Rows.Count, "L").End(xlUp).Row
For i = iLastRow To 1 Step -1
If ExistsInCollection(colWords, .Cells(i, "L").Value) Then
.Rows(i).Delete
End If
Next i
End With

End Sub


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"NateBuckley" wrote in message
...
Hello,

I'm just wondering if it's possible to check to see if something exists
within a collection before proceeding, I can do it with putting various
"On
error goto" statements, but I was just wondering if it's be possible to do
something like the following without having to send the process jumping to
labels.

if aCollection.Item("aKey") < null then
'do something special
else
Msgbox "Yar! No Object 'ere!"
end if

I know that doesn't work, but perhaps there is a way like it?

Thanks in advance!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default Check a collection for objects that may or may not exist within it

Hi Nate,

In addition to Bob's response, you may
wish to consider replacing your Collection
with a scripting Dictionary.

In many cases, a Dictionary is preferable
to a collection and,with reference to your
specific question, it has an Exists method.

For more information on Dictionaries and
Collections. see Chip Pearson at:

http://www.cpearson.com/excel/Collec...ctionaries.htm


---
Regards.
Norman


"NateBuckley" wrote in message
...
Hello,

I'm just wondering if it's possible to check to see if something exists
within a collection before proceeding, I can do it with putting various
"On
error goto" statements, but I was just wondering if it's be possible to do
something like the following without having to send the process jumping to
labels.

if aCollection.Item("aKey") < null then
'do something special
else
Msgbox "Yar! No Object 'ere!"
end if

I know that doesn't work, but perhaps there is a way like it?

Thanks in advance!


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
Number of objects in a collection CK Excel Programming 4 April 14th 08 02:06 AM
Passing Objects from Excel VBA collection to a VB6 DLL Dan[_50_] Excel Programming 6 February 15th 06 10:34 PM
Range objects in a collection Corey B Excel Programming 2 September 25th 04 06:24 PM
Track Changes Collection - Does it exist? Steven Revell Excel Programming 0 September 30th 03 12:40 PM
Iterate over Collection Objects in Container. Bob Kilmer Excel Programming 1 August 28th 03 02:37 AM


All times are GMT +1. The time now is 02:35 AM.

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"