Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 205
Default Listbox and Collections (Again!)

Hi there,

Yes I know the newsgroups are littered with this sort of question, but I
just can't find the answer.

I'm trying to fill the RowSource property with a collection ("List"), that
I've passed to another collection ("colNameForm"). The colNameForm
collection appears to be ok as the "Debug.Print v(0), v(1)" part chucks out
the correct two strings. It's only when I then get to the "Sub
LoadListboxes()" that I get a problem.

I'm getting a "Compile error: Arguement not optional"

Any clues?

Thanks

John

PS I want both parts to be in a 2 column listbox.


-----------------------------------------
'Module level
Dim colNameForm As New Collection
-----------------------------------------
Dim List As New Collection
Dim v As Variant
Dim item As Variant

Set arrFunctionForm = List
Set List = Nothing
For Each item In colNameForm
v = item
Debug.Print v(0), v(1)
Next item
-----------------------------------------

-----------------------------------------
Private Sub LoadListboxes()

Dim Ctrl As MSForms.Control

For Each Ctrl In Me.Controls
Select Case Ctrl.Name
Case "lstListBox1"
Ctrl.Clear
Ctrl.ColumnCount = 2
Ctrl.RowSource = colNameForm
Case "lstListBox2"
Case "lstListBox3"
End Select
Next Ctrl
-----------------------------------------

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Listbox and Collections (Again!)

You can only bind a Listbox to a range using rowsource. You will have to
add the items individually

This modification of your code worked for me:

Public colNameForm As Collection
Sub RemoveDuplicates()
Dim Rng As Range, Cell As Range
Dim v As Variant
Dim item As Variant
Dim List As New Collection

' The items are in A1:A10
Set Rng = Worksheets("Sheet3").Range("A1:A10")

On Error Resume Next
For Each Cell In Rng
v = List.item(Cell.Text)
If Err.Number < 0 Then
v = Array(Cell.Text, Cell.Address(0, 0))
List.Add v, CStr(Cell.Value)
Else
v(1) = v(1) & "," & Cell.Address(0, 0)
List.Remove Cell.Text
List.Add v, CStr(Cell.Value)
End If
Err.Clear
Next Cell

' Resume normal error handling
On Error GoTo 0

Set colNameForm = List
' Print out the list is the Immediate window
' For Each item In colNameForm
' v = item
' Debug.Print v(0), v(1)
' Next item
UserForm1.Show

End Sub

in the USERFORM MODULE:

Private Sub UserForm_Initialize()


Dim Ctrl As MSForms.Control
Dim itm As Variant
For Each Ctrl In Me.Controls
Select Case Ctrl.Name
Case "lstListBox1"
Ctrl.Clear
Ctrl.ColumnCount = 2
For Each itm In colNameForm
Ctrl.AddItem itm(0)
Ctrl.List(Ctrl.ListCount - 1, 1) = itm(1)
Next
Case "lstListBox2"
Case "lstListBox3"
End Select
Next Ctrl

End Sub


--
Regards,
Tom Ogilvy


"John" wrote in message
...
Hi there,

Yes I know the newsgroups are littered with this sort of question, but I
just can't find the answer.

I'm trying to fill the RowSource property with a collection ("List"), that
I've passed to another collection ("colNameForm"). The colNameForm
collection appears to be ok as the "Debug.Print v(0), v(1)" part chucks

out
the correct two strings. It's only when I then get to the "Sub
LoadListboxes()" that I get a problem.

I'm getting a "Compile error: Arguement not optional"

Any clues?

Thanks

John

PS I want both parts to be in a 2 column listbox.


-----------------------------------------
'Module level
Dim colNameForm As New Collection
-----------------------------------------
Dim List As New Collection
Dim v As Variant
Dim item As Variant

Set arrFunctionForm = List
Set List = Nothing
For Each item In colNameForm
v = item
Debug.Print v(0), v(1)
Next item
-----------------------------------------

-----------------------------------------
Private Sub LoadListboxes()

Dim Ctrl As MSForms.Control

For Each Ctrl In Me.Controls
Select Case Ctrl.Name
Case "lstListBox1"
Ctrl.Clear
Ctrl.ColumnCount = 2
Ctrl.RowSource = colNameForm
Case "lstListBox2"
Case "lstListBox3"
End Select
Next Ctrl
-----------------------------------------

End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 205
Default Listbox and Collections (Again!)

Thanks for this Tom. I'll go away and study it and let you know how I got
on.

Best regards

John

"Tom Ogilvy" wrote in message
...
You can only bind a Listbox to a range using rowsource. You will have to
add the items individually

This modification of your code worked for me:

Public colNameForm As Collection
Sub RemoveDuplicates()
Dim Rng As Range, Cell As Range
Dim v As Variant
Dim item As Variant
Dim List As New Collection

' The items are in A1:A10
Set Rng = Worksheets("Sheet3").Range("A1:A10")

On Error Resume Next
For Each Cell In Rng
v = List.item(Cell.Text)
If Err.Number < 0 Then
v = Array(Cell.Text, Cell.Address(0, 0))
List.Add v, CStr(Cell.Value)
Else
v(1) = v(1) & "," & Cell.Address(0, 0)
List.Remove Cell.Text
List.Add v, CStr(Cell.Value)
End If
Err.Clear
Next Cell

' Resume normal error handling
On Error GoTo 0

Set colNameForm = List
' Print out the list is the Immediate window
' For Each item In colNameForm
' v = item
' Debug.Print v(0), v(1)
' Next item
UserForm1.Show

End Sub

in the USERFORM MODULE:

Private Sub UserForm_Initialize()


Dim Ctrl As MSForms.Control
Dim itm As Variant
For Each Ctrl In Me.Controls
Select Case Ctrl.Name
Case "lstListBox1"
Ctrl.Clear
Ctrl.ColumnCount = 2
For Each itm In colNameForm
Ctrl.AddItem itm(0)
Ctrl.List(Ctrl.ListCount - 1, 1) = itm(1)
Next
Case "lstListBox2"
Case "lstListBox3"
End Select
Next Ctrl

End Sub


--
Regards,
Tom Ogilvy


"John" wrote in message
...
Hi there,

Yes I know the newsgroups are littered with this sort of question, but I
just can't find the answer.

I'm trying to fill the RowSource property with a collection ("List"),
that
I've passed to another collection ("colNameForm"). The colNameForm
collection appears to be ok as the "Debug.Print v(0), v(1)" part chucks

out
the correct two strings. It's only when I then get to the "Sub
LoadListboxes()" that I get a problem.

I'm getting a "Compile error: Arguement not optional"

Any clues?

Thanks

John

PS I want both parts to be in a 2 column listbox.


-----------------------------------------
'Module level
Dim colNameForm As New Collection
-----------------------------------------
Dim List As New Collection
Dim v As Variant
Dim item As Variant

Set arrFunctionForm = List
Set List = Nothing
For Each item In colNameForm
v = item
Debug.Print v(0), v(1)
Next item
-----------------------------------------

-----------------------------------------
Private Sub LoadListboxes()

Dim Ctrl As MSForms.Control

For Each Ctrl In Me.Controls
Select Case Ctrl.Name
Case "lstListBox1"
Ctrl.Clear
Ctrl.ColumnCount = 2
Ctrl.RowSource = colNameForm
Case "lstListBox2"
Case "lstListBox3"
End Select
Next Ctrl
-----------------------------------------

End Sub






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 205
Default Listbox and Collections (Again!)

Hi Tom,

Just to say thanks again. Worked perfectly. The only thing is that it's
raised another issue about listbox value length, but I'll write a new post
for that.

Thanks

John

"Tom Ogilvy" wrote in message
...
You can only bind a Listbox to a range using rowsource. You will have to
add the items individually

This modification of your code worked for me:

Public colNameForm As Collection
Sub RemoveDuplicates()
Dim Rng As Range, Cell As Range
Dim v As Variant
Dim item As Variant
Dim List As New Collection

' The items are in A1:A10
Set Rng = Worksheets("Sheet3").Range("A1:A10")

On Error Resume Next
For Each Cell In Rng
v = List.item(Cell.Text)
If Err.Number < 0 Then
v = Array(Cell.Text, Cell.Address(0, 0))
List.Add v, CStr(Cell.Value)
Else
v(1) = v(1) & "," & Cell.Address(0, 0)
List.Remove Cell.Text
List.Add v, CStr(Cell.Value)
End If
Err.Clear
Next Cell

' Resume normal error handling
On Error GoTo 0

Set colNameForm = List
' Print out the list is the Immediate window
' For Each item In colNameForm
' v = item
' Debug.Print v(0), v(1)
' Next item
UserForm1.Show

End Sub

in the USERFORM MODULE:

Private Sub UserForm_Initialize()


Dim Ctrl As MSForms.Control
Dim itm As Variant
For Each Ctrl In Me.Controls
Select Case Ctrl.Name
Case "lstListBox1"
Ctrl.Clear
Ctrl.ColumnCount = 2
For Each itm In colNameForm
Ctrl.AddItem itm(0)
Ctrl.List(Ctrl.ListCount - 1, 1) = itm(1)
Next
Case "lstListBox2"
Case "lstListBox3"
End Select
Next Ctrl

End Sub


--
Regards,
Tom Ogilvy


"John" wrote in message
...
Hi there,

Yes I know the newsgroups are littered with this sort of question, but I
just can't find the answer.

I'm trying to fill the RowSource property with a collection ("List"),
that
I've passed to another collection ("colNameForm"). The colNameForm
collection appears to be ok as the "Debug.Print v(0), v(1)" part chucks

out
the correct two strings. It's only when I then get to the "Sub
LoadListboxes()" that I get a problem.

I'm getting a "Compile error: Arguement not optional"

Any clues?

Thanks

John

PS I want both parts to be in a 2 column listbox.


-----------------------------------------
'Module level
Dim colNameForm As New Collection
-----------------------------------------
Dim List As New Collection
Dim v As Variant
Dim item As Variant

Set arrFunctionForm = List
Set List = Nothing
For Each item In colNameForm
v = item
Debug.Print v(0), v(1)
Next item
-----------------------------------------

-----------------------------------------
Private Sub LoadListboxes()

Dim Ctrl As MSForms.Control

For Each Ctrl In Me.Controls
Select Case Ctrl.Name
Case "lstListBox1"
Ctrl.Clear
Ctrl.ColumnCount = 2
Ctrl.RowSource = colNameForm
Case "lstListBox2"
Case "lstListBox3"
End Select
Next Ctrl
-----------------------------------------

End Sub






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
Collections of Collections David Morton Excel Programming 6 November 13th 04 01:10 AM
Help with collections ksnapp[_45_] Excel Programming 1 April 7th 04 12:42 AM
Using Collections Kerry[_4_] Excel Programming 1 January 25th 04 04:08 PM
listbox.value not equal to listbox.list(listbox.listindex,0) ARB Excel Programming 0 October 22nd 03 12:46 AM
Comparing Collections Tom Ogilvy Excel Programming 1 September 17th 03 06:15 PM


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