PC Review


Reply
Thread Tools Rate Thread

comman button on form to transfer items in listbox to a sheet

 
 
gbpg
Guest
Posts: n/a
 
      21st Mar 2008
I have form that has a number of list boxes that use command buttons to
transfer items from one list box to another, I want to save the items to a
sheet with a command button. Can do this with a text box but not with a list
box. Can some one give an example?


 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      21st Mar 2008
Maybe you could use something like:

Option Explicit
Private Sub CommandButton1_Click()
Dim DestCell As Range
Dim iCtr As Long

With Worksheets("sheet1")
Set DestCell = .Range("A1")
End With

With Me.ListBox1
DestCell.Resize(.ListCount, 1).ClearContents

For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
DestCell.Value = .List(iCtr)
Set DestCell = DestCell.Offset(1, 0)
End If
Next iCtr
End With
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
.AddItem "a"
.AddItem "b"
.AddItem "c"
.AddItem "d"
.AddItem "e"
End With
End Sub


gbpg wrote:
>
> I have form that has a number of list boxes that use command buttons to
> transfer items from one list box to another, I want to save the items to a
> sheet with a command button. Can do this with a text box but not with a list
> box. Can some one give an example?


--

Dave Peterson
 
Reply With Quote
 
gbpg
Guest
Posts: n/a
 
      22nd Mar 2008
Can the Add item be a range? In one list box I have 1200 employees that can
be possibly selected.

"Dave Peterson" wrote:

> Maybe you could use something like:
>
> Option Explicit
> Private Sub CommandButton1_Click()
> Dim DestCell As Range
> Dim iCtr As Long
>
> With Worksheets("sheet1")
> Set DestCell = .Range("A1")
> End With
>
> With Me.ListBox1
> DestCell.Resize(.ListCount, 1).ClearContents
>
> For iCtr = 0 To .ListCount - 1
> If .Selected(iCtr) Then
> DestCell.Value = .List(iCtr)
> Set DestCell = DestCell.Offset(1, 0)
> End If
> Next iCtr
> End With
> End Sub
> Private Sub UserForm_Initialize()
> With Me.ListBox1
> .MultiSelect = fmMultiSelectMulti
> .AddItem "a"
> .AddItem "b"
> .AddItem "c"
> .AddItem "d"
> .AddItem "e"
> End With
> End Sub
>
>
> gbpg wrote:
> >
> > I have form that has a number of list boxes that use command buttons to
> > transfer items from one list box to another, I want to save the items to a
> > sheet with a command button. Can do this with a text box but not with a list
> > box. Can some one give an example?

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      22nd Mar 2008
You can use .additem to add the value from cells in a worksheet.

But you have more choices, too. You could use the .rowsource property and just
tell the listbox where to get the info:

Option Explicit
Private Sub UserForm_Initialize()
Dim myRng As Range

With Worksheets("sheet1")
Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With

With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
.RowSource = myRng.Address(external:=True)
.ColumnCount = myRng.Columns.Count
.ColumnHeads = True
End With
End Sub



gbpg wrote:
>
> Can the Add item be a range? In one list box I have 1200 employees that can
> be possibly selected.
>
> "Dave Peterson" wrote:
>
> > Maybe you could use something like:
> >
> > Option Explicit
> > Private Sub CommandButton1_Click()
> > Dim DestCell As Range
> > Dim iCtr As Long
> >
> > With Worksheets("sheet1")
> > Set DestCell = .Range("A1")
> > End With
> >
> > With Me.ListBox1
> > DestCell.Resize(.ListCount, 1).ClearContents
> >
> > For iCtr = 0 To .ListCount - 1
> > If .Selected(iCtr) Then
> > DestCell.Value = .List(iCtr)
> > Set DestCell = DestCell.Offset(1, 0)
> > End If
> > Next iCtr
> > End With
> > End Sub
> > Private Sub UserForm_Initialize()
> > With Me.ListBox1
> > .MultiSelect = fmMultiSelectMulti
> > .AddItem "a"
> > .AddItem "b"
> > .AddItem "c"
> > .AddItem "d"
> > .AddItem "e"
> > End With
> > End Sub
> >
> >
> > gbpg wrote:
> > >
> > > I have form that has a number of list boxes that use command buttons to
> > > transfer items from one list box to another, I want to save the items to a
> > > sheet with a command button. Can do this with a text box but not with a list
> > > box. Can some one give an example?

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


--

Dave Peterson
 
Reply With Quote
 
gbpg
Guest
Posts: n/a
 
      22nd Mar 2008
I am sorry I actually meant the other way around - My list list does get the
names from a sheet. I want to save the info that goes from the listbox one to
listbox 2 (which works) to cells on a sheet.

Listbox one--->Listbox two----->sheet cells by a range (instead of adding
all of the cells indivually). The idea is that the cells with the multiple
selection could be used elsewhere after the bookcloses.



"Dave Peterson" wrote:

> You can use .additem to add the value from cells in a worksheet.
>
> But you have more choices, too. You could use the .rowsource property and just
> tell the listbox where to get the info:
>
> Option Explicit
> Private Sub UserForm_Initialize()
> Dim myRng As Range
>
> With Worksheets("sheet1")
> Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
> End With
>
> With Me.ListBox1
> .MultiSelect = fmMultiSelectMulti
> .RowSource = myRng.Address(external:=True)
> .ColumnCount = myRng.Columns.Count
> .ColumnHeads = True
> End With
> End Sub
>
>
>
> gbpg wrote:
> >
> > Can the Add item be a range? In one list box I have 1200 employees that can
> > be possibly selected.
> >
> > "Dave Peterson" wrote:
> >
> > > Maybe you could use something like:
> > >
> > > Option Explicit
> > > Private Sub CommandButton1_Click()
> > > Dim DestCell As Range
> > > Dim iCtr As Long
> > >
> > > With Worksheets("sheet1")
> > > Set DestCell = .Range("A1")
> > > End With
> > >
> > > With Me.ListBox1
> > > DestCell.Resize(.ListCount, 1).ClearContents
> > >
> > > For iCtr = 0 To .ListCount - 1
> > > If .Selected(iCtr) Then
> > > DestCell.Value = .List(iCtr)
> > > Set DestCell = DestCell.Offset(1, 0)
> > > End If
> > > Next iCtr
> > > End With
> > > End Sub
> > > Private Sub UserForm_Initialize()
> > > With Me.ListBox1
> > > .MultiSelect = fmMultiSelectMulti
> > > .AddItem "a"
> > > .AddItem "b"
> > > .AddItem "c"
> > > .AddItem "d"
> > > .AddItem "e"
> > > End With
> > > End Sub
> > >
> > >
> > > gbpg wrote:
> > > >
> > > > I have form that has a number of list boxes that use command buttons to
> > > > transfer items from one list box to another, I want to save the items to a
> > > > sheet with a command button. Can do this with a text box but not with a list
> > > > box. Can some one give an example?
> > >
> > > --
> > >
> > > Dave Peterson
> > >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      22nd Mar 2008
If you're using a listbox where you allow multiselections, then you're going to
have to loop through the selected items--like in that first suggestion.

If you just wanted to put the list into a worksheet range:

Option Explicit
Private Sub CommandButton1_Click()
Dim DestCell As Range
Dim iCtr As Long

With Worksheets("sheet1")
Set DestCell = .Range("b2")
End With

With Me.ListBox1
DestCell.Resize(.ListCount, .ColumnCount).Value = .List
End With
End Sub

I

gbpg wrote:
>
> I am sorry I actually meant the other way around - My list list does get the
> names from a sheet. I want to save the info that goes from the listbox one to
> listbox 2 (which works) to cells on a sheet.
>
> Listbox one--->Listbox two----->sheet cells by a range (instead of adding
> all of the cells indivually). The idea is that the cells with the multiple
> selection could be used elsewhere after the bookcloses.
>
> "Dave Peterson" wrote:
>
> > You can use .additem to add the value from cells in a worksheet.
> >
> > But you have more choices, too. You could use the .rowsource property and just
> > tell the listbox where to get the info:
> >
> > Option Explicit
> > Private Sub UserForm_Initialize()
> > Dim myRng As Range
> >
> > With Worksheets("sheet1")
> > Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
> > End With
> >
> > With Me.ListBox1
> > .MultiSelect = fmMultiSelectMulti
> > .RowSource = myRng.Address(external:=True)
> > .ColumnCount = myRng.Columns.Count
> > .ColumnHeads = True
> > End With
> > End Sub
> >
> >
> >
> > gbpg wrote:
> > >
> > > Can the Add item be a range? In one list box I have 1200 employees that can
> > > be possibly selected.
> > >
> > > "Dave Peterson" wrote:
> > >
> > > > Maybe you could use something like:
> > > >
> > > > Option Explicit
> > > > Private Sub CommandButton1_Click()
> > > > Dim DestCell As Range
> > > > Dim iCtr As Long
> > > >
> > > > With Worksheets("sheet1")
> > > > Set DestCell = .Range("A1")
> > > > End With
> > > >
> > > > With Me.ListBox1
> > > > DestCell.Resize(.ListCount, 1).ClearContents
> > > >
> > > > For iCtr = 0 To .ListCount - 1
> > > > If .Selected(iCtr) Then
> > > > DestCell.Value = .List(iCtr)
> > > > Set DestCell = DestCell.Offset(1, 0)
> > > > End If
> > > > Next iCtr
> > > > End With
> > > > End Sub
> > > > Private Sub UserForm_Initialize()
> > > > With Me.ListBox1
> > > > .MultiSelect = fmMultiSelectMulti
> > > > .AddItem "a"
> > > > .AddItem "b"
> > > > .AddItem "c"
> > > > .AddItem "d"
> > > > .AddItem "e"
> > > > End With
> > > > End Sub
> > > >
> > > >
> > > > gbpg wrote:
> > > > >
> > > > > I have form that has a number of list boxes that use command buttons to
> > > > > transfer items from one list box to another, I want to save the items to a
> > > > > sheet with a command button. Can do this with a text box but not with a list
> > > > > box. Can some one give an example?
> > > >
> > > > --
> > > >
> > > > Dave Peterson
> > > >

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


--

Dave Peterson
 
Reply With Quote
 
gbpg
Guest
Posts: n/a
 
      22nd Mar 2008
thanks Dave, I will try and see if I can get that to work..
Cheers

"Dave Peterson" wrote:

> If you're using a listbox where you allow multiselections, then you're going to
> have to loop through the selected items--like in that first suggestion.
>
> If you just wanted to put the list into a worksheet range:
>
> Option Explicit
> Private Sub CommandButton1_Click()
> Dim DestCell As Range
> Dim iCtr As Long
>
> With Worksheets("sheet1")
> Set DestCell = .Range("b2")
> End With
>
> With Me.ListBox1
> DestCell.Resize(.ListCount, .ColumnCount).Value = .List
> End With
> End Sub
>
> I
>
> gbpg wrote:
> >
> > I am sorry I actually meant the other way around - My list list does get the
> > names from a sheet. I want to save the info that goes from the listbox one to
> > listbox 2 (which works) to cells on a sheet.
> >
> > Listbox one--->Listbox two----->sheet cells by a range (instead of adding
> > all of the cells indivually). The idea is that the cells with the multiple
> > selection could be used elsewhere after the bookcloses.
> >
> > "Dave Peterson" wrote:
> >
> > > You can use .additem to add the value from cells in a worksheet.
> > >
> > > But you have more choices, too. You could use the .rowsource property and just
> > > tell the listbox where to get the info:
> > >
> > > Option Explicit
> > > Private Sub UserForm_Initialize()
> > > Dim myRng As Range
> > >
> > > With Worksheets("sheet1")
> > > Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
> > > End With
> > >
> > > With Me.ListBox1
> > > .MultiSelect = fmMultiSelectMulti
> > > .RowSource = myRng.Address(external:=True)
> > > .ColumnCount = myRng.Columns.Count
> > > .ColumnHeads = True
> > > End With
> > > End Sub
> > >
> > >
> > >
> > > gbpg wrote:
> > > >
> > > > Can the Add item be a range? In one list box I have 1200 employees that can
> > > > be possibly selected.
> > > >
> > > > "Dave Peterson" wrote:
> > > >
> > > > > Maybe you could use something like:
> > > > >
> > > > > Option Explicit
> > > > > Private Sub CommandButton1_Click()
> > > > > Dim DestCell As Range
> > > > > Dim iCtr As Long
> > > > >
> > > > > With Worksheets("sheet1")
> > > > > Set DestCell = .Range("A1")
> > > > > End With
> > > > >
> > > > > With Me.ListBox1
> > > > > DestCell.Resize(.ListCount, 1).ClearContents
> > > > >
> > > > > For iCtr = 0 To .ListCount - 1
> > > > > If .Selected(iCtr) Then
> > > > > DestCell.Value = .List(iCtr)
> > > > > Set DestCell = DestCell.Offset(1, 0)
> > > > > End If
> > > > > Next iCtr
> > > > > End With
> > > > > End Sub
> > > > > Private Sub UserForm_Initialize()
> > > > > With Me.ListBox1
> > > > > .MultiSelect = fmMultiSelectMulti
> > > > > .AddItem "a"
> > > > > .AddItem "b"
> > > > > .AddItem "c"
> > > > > .AddItem "d"
> > > > > .AddItem "e"
> > > > > End With
> > > > > End Sub
> > > > >
> > > > >
> > > > > gbpg wrote:
> > > > > >
> > > > > > I have form that has a number of list boxes that use command buttons to
> > > > > > transfer items from one list box to another, I want to save the items to a
> > > > > > sheet with a command button. Can do this with a text box but not with a list
> > > > > > box. Can some one give an example?
> > > > >
> > > > > --
> > > > >
> > > > > Dave Peterson
> > > > >
> > >
> > > --
> > >
> > > Dave Peterson
> > >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
gbpg
Guest
Posts: n/a
 
      24th Mar 2008
thanks Dave, I got it to work and I think I can actually use this again,
although a generalist can be as dangerous as helpful to himself. I am on to
more now in my mad scheme....

Happy Easter.

"gbpg" wrote:

> thanks Dave, I will try and see if I can get that to work..
> Cheers
>
> "Dave Peterson" wrote:
>
> > If you're using a listbox where you allow multiselections, then you're going to
> > have to loop through the selected items--like in that first suggestion.
> >
> > If you just wanted to put the list into a worksheet range:
> >
> > Option Explicit
> > Private Sub CommandButton1_Click()
> > Dim DestCell As Range
> > Dim iCtr As Long
> >
> > With Worksheets("sheet1")
> > Set DestCell = .Range("b2")
> > End With
> >
> > With Me.ListBox1
> > DestCell.Resize(.ListCount, .ColumnCount).Value = .List
> > End With
> > End Sub
> >
> > I
> >
> > gbpg wrote:
> > >
> > > I am sorry I actually meant the other way around - My list list does get the
> > > names from a sheet. I want to save the info that goes from the listbox one to
> > > listbox 2 (which works) to cells on a sheet.
> > >
> > > Listbox one--->Listbox two----->sheet cells by a range (instead of adding
> > > all of the cells indivually). The idea is that the cells with the multiple
> > > selection could be used elsewhere after the bookcloses.
> > >
> > > "Dave Peterson" wrote:
> > >
> > > > You can use .additem to add the value from cells in a worksheet.
> > > >
> > > > But you have more choices, too. You could use the .rowsource property and just
> > > > tell the listbox where to get the info:
> > > >
> > > > Option Explicit
> > > > Private Sub UserForm_Initialize()
> > > > Dim myRng As Range
> > > >
> > > > With Worksheets("sheet1")
> > > > Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
> > > > End With
> > > >
> > > > With Me.ListBox1
> > > > .MultiSelect = fmMultiSelectMulti
> > > > .RowSource = myRng.Address(external:=True)
> > > > .ColumnCount = myRng.Columns.Count
> > > > .ColumnHeads = True
> > > > End With
> > > > End Sub
> > > >
> > > >
> > > >
> > > > gbpg wrote:
> > > > >
> > > > > Can the Add item be a range? In one list box I have 1200 employees that can
> > > > > be possibly selected.
> > > > >
> > > > > "Dave Peterson" wrote:
> > > > >
> > > > > > Maybe you could use something like:
> > > > > >
> > > > > > Option Explicit
> > > > > > Private Sub CommandButton1_Click()
> > > > > > Dim DestCell As Range
> > > > > > Dim iCtr As Long
> > > > > >
> > > > > > With Worksheets("sheet1")
> > > > > > Set DestCell = .Range("A1")
> > > > > > End With
> > > > > >
> > > > > > With Me.ListBox1
> > > > > > DestCell.Resize(.ListCount, 1).ClearContents
> > > > > >
> > > > > > For iCtr = 0 To .ListCount - 1
> > > > > > If .Selected(iCtr) Then
> > > > > > DestCell.Value = .List(iCtr)
> > > > > > Set DestCell = DestCell.Offset(1, 0)
> > > > > > End If
> > > > > > Next iCtr
> > > > > > End With
> > > > > > End Sub
> > > > > > Private Sub UserForm_Initialize()
> > > > > > With Me.ListBox1
> > > > > > .MultiSelect = fmMultiSelectMulti
> > > > > > .AddItem "a"
> > > > > > .AddItem "b"
> > > > > > .AddItem "c"
> > > > > > .AddItem "d"
> > > > > > .AddItem "e"
> > > > > > End With
> > > > > > End Sub
> > > > > >
> > > > > >
> > > > > > gbpg wrote:
> > > > > > >
> > > > > > > I have form that has a number of list boxes that use command buttons to
> > > > > > > transfer items from one list box to another, I want to save the items to a
> > > > > > > sheet with a command button. Can do this with a text box but not with a list
> > > > > > > box. Can some one give an example?
> > > > > >
> > > > > > --
> > > > > >
> > > > > > Dave Peterson
> > > > > >
> > > >
> > > > --
> > > >
> > > > 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
Transfer items from one listbox to another Gaetan Microsoft Access 4 21st Dec 2007 07:10 PM
Transfer ListBox items to a range AD108 Microsoft Excel Programming 2 10th Sep 2006 01:49 PM
Need help with coding comman button on form... =?Utf-8?B?TWFyeSBBIFBlcmV6?= Microsoft Access Form Coding 5 10th Nov 2005 08:30 PM
How to open a specific HTML using comman button in a Form Microsoft Access Forms 1 4th Apr 2005 04:34 PM
Transfer multiple columns items form listbox to range Rolo Microsoft Excel Programming 3 15th Nov 2003 06:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:42 PM.