Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 234
Default Collection Function

The Function below fills a Collection with my Products, but when I call the function a second time, I don't want it to have to run. I think I've had a line like this working befo If Not colProducts Is Nothing Then Exit Function
....but when I step through, my collection actually IS nothing when I enter it the second time. Any ideas why?

Thank
Ro

sub testi
msgbox colproducts(1
msgbox colproducts(2
end su

Public Function colProducts() As Collectio
'Don't bother populating if it's already ful
If Not colProducts Is Nothing Then Exit Functio

'Populate the collectio
For i = 1 To 3
colProducts.Add Cells(i, 1).Valu
Nex
End Functio

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Collection Function

ColProducts is not the object, it is just a function that populates a
collection object.

What you need is to trap the caller, something like

Dim colProd As Collection

If colProd Is Nothing Then
Set colProd = New Collection
colProd = ColProducts
End If


Public Function colProducts() As Collection
Dim i
Dim xx As New Collection
'Populate the collection
For i = 1 To 30
xx.Add Cells(i, 1).Value, CStr(Cells(i, 1).Value)
Next
Set colProducts = xx
End Function

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rob" wrote in message
...
The Function below fills a Collection with my Products, but when I call

the function a second time, I don't want it to have to run. I think I've
had a line like this working befo If Not colProducts Is Nothing Then
Exit Function
...but when I step through, my collection actually IS nothing when I enter

it the second time. Any ideas why??

Thanks
Rob

sub testit
msgbox colproducts(1)
msgbox colproducts(2)
end sub

Public Function colProducts() As Collection
'Don't bother populating if it's already full
If Not colProducts Is Nothing Then Exit Function

'Populate the collection
For i = 1 To 30
colProducts.Add Cells(i, 1).Value
Next
End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Collection Function

Rob,

Try something like

Public Function colProducts() As Collection
Dim i As Long
Static C As Collection
If C Is Nothing Then
Set C = New Collection
End If
If C.Count = 0 Then
For i = 1 To 30
C.Add Cells(i, 1).Value
Next
End If
Set colProducts = C
End Function

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Rob" wrote in message
...
The Function below fills a Collection with my Products, but

when I call the function a second time, I don't want it to have
to run. I think I've had a line like this working befo If
Not colProducts Is Nothing Then Exit Function
...but when I step through, my collection actually IS nothing

when I enter it the second time. Any ideas why??

Thanks
Rob

sub testit
msgbox colproducts(1)
msgbox colproducts(2)
end sub

Public Function colProducts() As Collection
'Don't bother populating if it's already full
If Not colProducts Is Nothing Then Exit Function

'Populate the collection
For i = 1 To 30
colProducts.Add Cells(i, 1).Value
Next
End Function



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
Reset New Collection Tony Di Stasi[_2_] Excel Programming 2 February 19th 04 03:21 PM
Function to create a collection Derek Gadd Excel Programming 4 November 1st 03 03:30 PM
Is a Collection the best option? Stuart[_5_] Excel Programming 5 August 31st 03 09:51 PM
Is a Collection the best option? Bradley Dawson Excel Programming 1 August 31st 03 08:14 PM
Sum textboxes in a collection Mark[_18_] Excel Programming 1 August 28th 03 04:17 AM


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