Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Function to create a collection

Hi,

I'm trying to create a collection from a given range of cells. The
code seems to run but the collection I try to create (in this case
called "MainCategory") has no data in it. Can anybody help?

Thanks,
Derek


Public MainCategory As New Collection
Dim AllCells As Range

Sub Test()
<code
Set AllCells = Range(Range("top_label").Offset(1, 0),
Rang("top_label").End(xlDown))

' Create a new collection
Set MainCategory = CreateCollection(AllCells)
End Sub

Function CreateCollection(AllCells As Range) As Collection
Dim Cell
'Prevents errors from trying to have duplicate entries
On Error Resume Next
For Each Cell In AllCells
CreateCollection.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Function to create a collection

Derek,
I think the source of your problem is defining the Function as a collection
type. (I beleive, -MVPs please clarify) that this refers to a pre-f=defined
collection in VB, as opposed to a "New", or user-defined, collection).

Secondly, the "On Error Resume Next" statement in your function (which is
rightly there to deal with duplicate entries) masks the fact that the
CreateCollection will not accept any new members to be added to it (check
the locals window).

So, the workaround (with the benefit of a quick test), us to use a
subroutine as follows:

Public MainCategory As New Collection
Dim AllCells As Range

Sub Test()

Set AllCells = Sheets("Sheet1").Range("DataRange") '(My data
source used to test)
' Create a new collection
Call CreateNewCollection(AllCells, MainCategory)

End Sub


Sub CreateNewCollection(AllCells As Range, xcNew As Collection)
Dim Cell
'Prevents errors from trying to have duplicate entries
On Error Resume Next
For Each Cell In AllCells
xcNew.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
End Sub

Alex

By the way, I like to use the prefix "xc" on all my userdefined collections.
I seems to highlight them well in the code.

"Derek Gadd" wrote in message
om...
Hi,

I'm trying to create a collection from a given range of cells. The
code seems to run but the collection I try to create (in this case
called "MainCategory") has no data in it. Can anybody help?

Thanks,
Derek


Public MainCategory As New Collection
Dim AllCells As Range

Sub Test()
<code
Set AllCells = Range(Range("top_label").Offset(1, 0),
Rang("top_label").End(xlDown))

' Create a new collection
Set MainCategory = CreateCollection(AllCells)
End Sub

Function CreateCollection(AllCells As Range) As Collection
Dim Cell
'Prevents errors from trying to have duplicate entries
On Error Resume Next
For Each Cell In AllCells
CreateCollection.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Function to create a collection

Derek..

For all functions is best to set a temp variable, then work on that
inside the function and at the end return the variable.

I've also added the option to return the collection sorted :)

Option Explicit
Option Compare Text

Sub Test()
Dim AllCells As Range
Dim mainCategory(1 To 3) As Collection
Dim i As Integer
Set AllCells = Range(Range("top_label").Offset(1, 0), Range
("top_label").End(xlDown))
' Create a few collections
Set mainCategory(1) = CreateCollection(AllCells)
Set mainCategory(2) = CreateCollection(AllCells, xlAscending)
Set mainCategory(3) = CreateCollection(AllCells, xlDescending)
End Sub

Function CreateCollection(AllCells As Range, Optional Sorted As
XlSortOrder = 0) As Collection
Dim Coll As New Collection
Dim Cell As Range
Dim n As Long
'note: option compare must be set to TEXT

On Error Resume Next
'Add a dummy to populate collection
Coll.Add Choose(Sorted, -1E+99, String(99, "z"))
For Each Cell In AllCells
With Cell
Select Case Sorted
Case xlAscending
For n = 1 To Coll.Count
If .Value < Coll(n) Then Exit For
Next
Case xlDescending
For n = 1 To Coll.Count
If .Value Coll(n) Then Exit For
Next
Case Else
n = Coll.Count + 1
End Select
Coll.Add .Value, CStr(.Value), , n - 1
End With
Next
'Remove dummy
Coll.Remove 1
'Return the result
Set CreateCollection = Coll
End Function



keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


(Derek Gadd) wrote:

Hi,

I'm trying to create a collection from a given range of cells. The
code seems to run but the collection I try to create (in this case
called "MainCategory") has no data in it. Can anybody help?

Thanks,
Derek


Public MainCategory As New Collection
Dim AllCells As Range

Sub Test()
<code
Set AllCells = Range(Range("top_label").Offset(1, 0),
Rang("top_label").End(xlDown))

' Create a new collection
Set MainCategory = CreateCollection(AllCells)
End Sub

Function CreateCollection(AllCells As Range) As Collection
Dim Cell
'Prevents errors from trying to have duplicate entries
On Error Resume Next
For Each Cell In AllCells
CreateCollection.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
End Function


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Function to create a collection

KeepitCool,

VERY nice solution. Thanks for a very useful post.

Alex

"keepitcool" wrote in message
...
Derek..

For all functions is best to set a temp variable, then work on that
inside the function and at the end return the variable.

I've also added the option to return the collection sorted :)

Option Explicit
Option Compare Text

Sub Test()
Dim AllCells As Range
Dim mainCategory(1 To 3) As Collection
Dim i As Integer
Set AllCells = Range(Range("top_label").Offset(1, 0), Range
("top_label").End(xlDown))
' Create a few collections
Set mainCategory(1) = CreateCollection(AllCells)
Set mainCategory(2) = CreateCollection(AllCells, xlAscending)
Set mainCategory(3) = CreateCollection(AllCells, xlDescending)
End Sub

Function CreateCollection(AllCells As Range, Optional Sorted As
XlSortOrder = 0) As Collection
Dim Coll As New Collection
Dim Cell As Range
Dim n As Long
'note: option compare must be set to TEXT

On Error Resume Next
'Add a dummy to populate collection
Coll.Add Choose(Sorted, -1E+99, String(99, "z"))
For Each Cell In AllCells
With Cell
Select Case Sorted
Case xlAscending
For n = 1 To Coll.Count
If .Value < Coll(n) Then Exit For
Next
Case xlDescending
For n = 1 To Coll.Count
If .Value Coll(n) Then Exit For
Next
Case Else
n = Coll.Count + 1
End Select
Coll.Add .Value, CStr(.Value), , n - 1
End With
Next
'Remove dummy
Coll.Remove 1
'Return the result
Set CreateCollection = Coll
End Function



keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


(Derek Gadd) wrote:

Hi,

I'm trying to create a collection from a given range of cells. The
code seems to run but the collection I try to create (in this case
called "MainCategory") has no data in it. Can anybody help?

Thanks,
Derek


Public MainCategory As New Collection
Dim AllCells As Range

Sub Test()
<code
Set AllCells = Range(Range("top_label").Offset(1, 0),
Rang("top_label").End(xlDown))

' Create a new collection
Set MainCategory = CreateCollection(AllCells)
End Sub

Function CreateCollection(AllCells As Range) As Collection
Dim Cell
'Prevents errors from trying to have duplicate entries
On Error Resume Next
For Each Cell In AllCells
CreateCollection.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
End Function




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Function to create a collection

Using your original construct, these modifications worked:

Public MainCategory As Variant

Sub Test()
Dim AllCells As Range
' <code
Set AllCells = Range(Range("top_label"). _
Offset(1, 0), Range("top_label").End(xlDown))

' Create a new collection
Set MainCategory = CreateCollection(AllCells)
For Each itm In MainCategory
Debug.Print itm
Next
End Sub

Function CreateCollection(AllCells As Range) As Variant
Dim Cell As Range
Dim myColl As New Collection
'Prevents errors from trying to have duplicate entries
On Error Resume Next
For Each Cell In AllCells
myColl.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
Set CreateCollection = myColl
End Function


--
Regards,
Tom Ogilvy

Derek Gadd wrote in message
om...
Hi,

I'm trying to create a collection from a given range of cells. The
code seems to run but the collection I try to create (in this case
called "MainCategory") has no data in it. Can anybody help?

Thanks,
Derek


Public MainCategory As New Collection
Dim AllCells As Range

Sub Test()
<code
Set AllCells = Range(Range("top_label").Offset(1, 0),
Rang("top_label").End(xlDown))

' Create a new collection
Set MainCategory = CreateCollection(AllCells)
End Sub

Function CreateCollection(AllCells As Range) As Collection
Dim Cell
'Prevents errors from trying to have duplicate entries
On Error Resume Next
For Each Cell In AllCells
CreateCollection.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
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
template for dvd collection David Excel Discussion (Misc queries) 2 April 26th 10 11:35 PM
data collection driller Excel Discussion (Misc queries) 1 October 13th 09 01:04 AM
how do i create a data collection form in excel? stacy New Users to Excel 1 October 26th 05 08:21 PM
Does anybody have a CD collection template PeterM Excel Discussion (Misc queries) 2 November 27th 04 05:46 PM
sheet collection Tom Ogilvy Excel Programming 0 October 28th 03 03:15 PM


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