PC Review


Reply
Thread Tools Rate Thread

Can't added two columns from one listbox to two columns in another

 
 
Webtechie
Guest
Posts: n/a
 
      20th Aug 2009
Hello,

I have one multi-select two column listbox on a userform. I have a button
that will take the selection from the mult-select listbox that has two
columns to another listbox that also has two columns.

Here is my code:

Dim itemIndex As Integer
Dim myVar As Variant
With frmEmployeeMaintenance

.lstClubsAssigned.RowSource = "" 'second listbox

If .lstClubs.ListIndex = -1 Then 'first listbox
Exit Sub
End If

For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
If .lstClubs.Selected(itemIndex) Then
.lstClubsAssigned.AddItem .lstClubs.Column(0, itemIndex) &
";" & .lstClubs.Column(1, itemIndex)
End If
Next itemIndex
End With


I found this code on a help forum, but it is not working for me. It only
adds a concatenated string to the first column. How do I add values to both
columns?

Thanks,

Tony

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      20th Aug 2009
Try...

For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
If .lstClubs.Selected(itemIndex) Then
.lstClubsAssigned.AddItem .lstClubs.list(itemindex, 0)
.lstclubsassigned(.lstclubassigned.listcount - 1, 1) _
= .lstclubs.list(itemindex, 1)
End If
Next itemIndex
End With

(Untested, uncompiled. Watch for typos.)

ps.

If this code is inside the userform module, I'd change this:
With frmEmployeeMaintenance
to
With Me

Me is the object that owns the code--in this case that userform.

Webtechie wrote:
>
> Hello,
>
> I have one multi-select two column listbox on a userform. I have a button
> that will take the selection from the mult-select listbox that has two
> columns to another listbox that also has two columns.
>
> Here is my code:
>
> Dim itemIndex As Integer
> Dim myVar As Variant
> With frmEmployeeMaintenance
>
> .lstClubsAssigned.RowSource = "" 'second listbox
>
> If .lstClubs.ListIndex = -1 Then 'first listbox
> Exit Sub
> End If
>
> For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
> If .lstClubs.Selected(itemIndex) Then
> .lstClubsAssigned.AddItem .lstClubs.Column(0, itemIndex) &
> ";" & .lstClubs.Column(1, itemIndex)
> End If
> Next itemIndex
> End With
>
> I found this code on a help forum, but it is not working for me. It only
> adds a concatenated string to the first column. How do I add values to both
> columns?
>
> Thanks,
>
> Tony


--

Dave Peterson
 
Reply With Quote
 
Webtechie
Guest
Posts: n/a
 
      20th Aug 2009
Dave,

This line:

.lstclubsassigned(.lstclubassigned.listcount - 1, 1) _
= .lstclubs.list(itemindex, 1)

is throwing an error 13 - Type mismatch.

What is that about? It doesn't like the

.lstclubsassigned(.lstclubassigned.listcount - 1, 1).

I've added

..lstClubsAssigned.ColumnCount = 2

just to be sure it knows there are two columns. Same error.

Thanks,

Tony

"Dave Peterson" wrote:

> Try...
>
> For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
> If .lstClubs.Selected(itemIndex) Then
> .lstClubsAssigned.AddItem .lstClubs.list(itemindex, 0)
> .lstclubsassigned(.lstclubassigned.listcount - 1, 1) _
> = .lstclubs.list(itemindex, 1)
> End If
> Next itemIndex
> End With
>
> (Untested, uncompiled. Watch for typos.)
>
> ps.
>
> If this code is inside the userform module, I'd change this:
> With frmEmployeeMaintenance
> to
> With Me
>
> Me is the object that owns the code--in this case that userform.
>
> Webtechie wrote:
> >
> > Hello,
> >
> > I have one multi-select two column listbox on a userform. I have a button
> > that will take the selection from the mult-select listbox that has two
> > columns to another listbox that also has two columns.
> >
> > Here is my code:
> >
> > Dim itemIndex As Integer
> > Dim myVar As Variant
> > With frmEmployeeMaintenance
> >
> > .lstClubsAssigned.RowSource = "" 'second listbox
> >
> > If .lstClubs.ListIndex = -1 Then 'first listbox
> > Exit Sub
> > End If
> >
> > For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
> > If .lstClubs.Selected(itemIndex) Then
> > .lstClubsAssigned.AddItem .lstClubs.Column(0, itemIndex) &
> > ";" & .lstClubs.Column(1, itemIndex)
> > End If
> > Next itemIndex
> > End With
> >
> > I found this code on a help forum, but it is not working for me. It only
> > adds a concatenated string to the first column. How do I add values to both
> > columns?
> >
> > Thanks,
> >
> > Tony

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      20th Aug 2009
I don't think it's the code.

I think it may be one of the settings you've made when designing the userform.

I created (and tested!) a small userform with 2 listboxes and a commandbutton.
This was the code behind the userform:

Option Explicit
Private Sub CommandButton1_Click()

Dim ItemIndex As Long

Me.lstClubsAssigned.Clear '???????
With Me.lstClubs
For ItemIndex = .ListCount - 1 To 0 Step -1
If .Selected(ItemIndex) = True Then
Me.lstClubsAssigned.AddItem .List(ItemIndex, 0)
Me.lstClubsAssigned.List(Me.lstClubsAssigned.ListCount - 1, 1) _
= .List(ItemIndex, 1)
End If
Next ItemIndex
End With

End Sub
Private Sub UserForm_Initialize()
Dim myRng As Range
With Worksheets("Sheet1")
Set myRng = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

With Me.lstClubs
.RowSource = ""
.ColumnCount = myRng.Columns.Count
.MultiSelect = fmMultiSelectMulti
.List = myRng.Value
.ColumnWidths = "50;50"
End With

With Me.lstClubsAssigned
.RowSource = ""
.ColumnCount = myRng.Columns.Count
.MultiSelect = fmMultiSelectMulti
.ColumnWidths = "50;50"
End With

End Sub


========
One more thing to check.

Make sure you don't have the .rowsource assigned to the second listbox--either
in code or in the properties window. That doesn't play well with .additem.

But I'm not sure that would cause the error you're seeing.



Webtechie wrote:
>
> Dave,
>
> This line:
>
> .lstclubsassigned(.lstclubassigned.listcount - 1, 1) _
> = .lstclubs.list(itemindex, 1)
>
> is throwing an error 13 - Type mismatch.
>
> What is that about? It doesn't like the
>
> .lstclubsassigned(.lstclubassigned.listcount - 1, 1).
>
> I've added
>
> .lstClubsAssigned.ColumnCount = 2
>
> just to be sure it knows there are two columns. Same error.
>
> Thanks,
>
> Tony
>
> "Dave Peterson" wrote:
>
> > Try...
> >
> > For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
> > If .lstClubs.Selected(itemIndex) Then
> > .lstClubsAssigned.AddItem .lstClubs.list(itemindex, 0)
> > .lstclubsassigned(.lstclubassigned.listcount - 1, 1) _
> > = .lstclubs.list(itemindex, 1)
> > End If
> > Next itemIndex
> > End With
> >
> > (Untested, uncompiled. Watch for typos.)
> >
> > ps.
> >
> > If this code is inside the userform module, I'd change this:
> > With frmEmployeeMaintenance
> > to
> > With Me
> >
> > Me is the object that owns the code--in this case that userform.
> >
> > Webtechie wrote:
> > >
> > > Hello,
> > >
> > > I have one multi-select two column listbox on a userform. I have a button
> > > that will take the selection from the mult-select listbox that has two
> > > columns to another listbox that also has two columns.
> > >
> > > Here is my code:
> > >
> > > Dim itemIndex As Integer
> > > Dim myVar As Variant
> > > With frmEmployeeMaintenance
> > >
> > > .lstClubsAssigned.RowSource = "" 'second listbox
> > >
> > > If .lstClubs.ListIndex = -1 Then 'first listbox
> > > Exit Sub
> > > End If
> > >
> > > For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
> > > If .lstClubs.Selected(itemIndex) Then
> > > .lstClubsAssigned.AddItem .lstClubs.Column(0, itemIndex) &
> > > ";" & .lstClubs.Column(1, itemIndex)
> > > End If
> > > Next itemIndex
> > > End With
> > >
> > > I found this code on a help forum, but it is not working for me. It only
> > > adds a concatenated string to the first column. How do I add values to both
> > > columns?
> > >
> > > Thanks,
> > >
> > > Tony

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
Webtechie
Guest
Posts: n/a
 
      21st Aug 2009
Dave,

You were right. I had another procedure that was changing the properties of
the listbox. I had forgotten about that code.

I made some adjustments and everything is working fine now.

Thanks for pointing me in the right direction!

Tony

"Dave Peterson" wrote:

> I don't think it's the code.
>
> I think it may be one of the settings you've made when designing the userform.
>
> I created (and tested!) a small userform with 2 listboxes and a commandbutton.
> This was the code behind the userform:
>
> Option Explicit
> Private Sub CommandButton1_Click()
>
> Dim ItemIndex As Long
>
> Me.lstClubsAssigned.Clear '???????
> With Me.lstClubs
> For ItemIndex = .ListCount - 1 To 0 Step -1
> If .Selected(ItemIndex) = True Then
> Me.lstClubsAssigned.AddItem .List(ItemIndex, 0)
> Me.lstClubsAssigned.List(Me.lstClubsAssigned.ListCount - 1, 1) _
> = .List(ItemIndex, 1)
> End If
> Next ItemIndex
> End With
>
> End Sub
> Private Sub UserForm_Initialize()
> Dim myRng As Range
> With Worksheets("Sheet1")
> Set myRng = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)
> End With
>
> With Me.lstClubs
> .RowSource = ""
> .ColumnCount = myRng.Columns.Count
> .MultiSelect = fmMultiSelectMulti
> .List = myRng.Value
> .ColumnWidths = "50;50"
> End With
>
> With Me.lstClubsAssigned
> .RowSource = ""
> .ColumnCount = myRng.Columns.Count
> .MultiSelect = fmMultiSelectMulti
> .ColumnWidths = "50;50"
> End With
>
> End Sub
>
>
> ========
> One more thing to check.
>
> Make sure you don't have the .rowsource assigned to the second listbox--either
> in code or in the properties window. That doesn't play well with .additem.
>
> But I'm not sure that would cause the error you're seeing.
>
>
>
> Webtechie wrote:
> >
> > Dave,
> >
> > This line:
> >
> > .lstclubsassigned(.lstclubassigned.listcount - 1, 1) _
> > = .lstclubs.list(itemindex, 1)
> >
> > is throwing an error 13 - Type mismatch.
> >
> > What is that about? It doesn't like the
> >
> > .lstclubsassigned(.lstclubassigned.listcount - 1, 1).
> >
> > I've added
> >
> > .lstClubsAssigned.ColumnCount = 2
> >
> > just to be sure it knows there are two columns. Same error.
> >
> > Thanks,
> >
> > Tony
> >
> > "Dave Peterson" wrote:
> >
> > > Try...
> > >
> > > For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
> > > If .lstClubs.Selected(itemIndex) Then
> > > .lstClubsAssigned.AddItem .lstClubs.list(itemindex, 0)
> > > .lstclubsassigned(.lstclubassigned.listcount - 1, 1) _
> > > = .lstclubs.list(itemindex, 1)
> > > End If
> > > Next itemIndex
> > > End With
> > >
> > > (Untested, uncompiled. Watch for typos.)
> > >
> > > ps.
> > >
> > > If this code is inside the userform module, I'd change this:
> > > With frmEmployeeMaintenance
> > > to
> > > With Me
> > >
> > > Me is the object that owns the code--in this case that userform.
> > >
> > > Webtechie wrote:
> > > >
> > > > Hello,
> > > >
> > > > I have one multi-select two column listbox on a userform. I have a button
> > > > that will take the selection from the mult-select listbox that has two
> > > > columns to another listbox that also has two columns.
> > > >
> > > > Here is my code:
> > > >
> > > > Dim itemIndex As Integer
> > > > Dim myVar As Variant
> > > > With frmEmployeeMaintenance
> > > >
> > > > .lstClubsAssigned.RowSource = "" 'second listbox
> > > >
> > > > If .lstClubs.ListIndex = -1 Then 'first listbox
> > > > Exit Sub
> > > > End If
> > > >
> > > > For itemIndex = .lstClubs.ListCount - 1 To 0 Step -1
> > > > If .lstClubs.Selected(itemIndex) Then
> > > > .lstClubsAssigned.AddItem .lstClubs.Column(0, itemIndex) &
> > > > ";" & .lstClubs.Column(1, itemIndex)
> > > > End If
> > > > Next itemIndex
> > > > End With
> > > >
> > > > I found this code on a help forum, but it is not working for me. It only
> > > > adds a concatenated string to the first column. How do I add values to both
> > > > columns?
> > > >
> > > > Thanks,
> > > >
> > > > Tony
> > >
> > > --
> > >
> > > Dave Peterson
> > >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox w/many columns, need update all columns to table Paul Microsoft Access Forms 1 9th Feb 2010 07:47 PM
VBA macro to hide certain columns even when new columns have been added vow.jackofhearts@gmail.com Microsoft Excel Discussion 7 1st Mar 2007 05:51 PM
Data is added in 1 column instaed of 4 columns (in listbox) =?Utf-8?B?QXJqYW4=?= Microsoft Excel Programming 1 19th Oct 2006 01:22 PM
Parent Columns and Child Columns don't have type-matching columns microsoft news Microsoft ADO .NET 1 21st Sep 2004 10:08 AM
Xcel or Access sort columns of names so that I remove the intersect between 2 columns from one of the columns? William.R.Reisen Microsoft Access External Data 2 20th Dec 2003 02:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:33 PM.