Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default All selected listbox values to a cell?

I am afraid I don't know much about VBA or programming. What I have tried to
do is to find pieces of VBA code in the net and then try to see if it works
for me. What I'd need the multiselect ActiveX listbox to do is that all the
values selected are saved in the active cell.

What I've managed to do so far with this piece of code is that I get a
listbox activated when I double clicks in a cell which contains a validation
list. When I click on the CommandButton1, only the first value which is
selected is transfered to the active cell. How could I get also the rest of
the values transfered to the same cell?

Harri

'==========================
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim wsList As Worksheet
Set ws = ActiveSheet
Set wsList = Sheets("ValidationLists")

Cancel = True
Set cboTemp = ws.OLEObjects("ListBox1")
On Error Resume Next
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 45
.Height = Target.Height + 100
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
End If

errHandler:
Application.EnableEvents = True
Exit Sub

End Sub
--------------
Private Sub CommandButton1_Click()
Dim i As Long
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) Then
ActiveCell.Value = Me.ListBox1.List(i)
ActiveCell.Offset(1, 0).Select
End If
Next i
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default All selected listbox values to a cell?

Hi,

Try this:

Private Sub CommandButton1_Click()
Dim i As Long
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) Then
' values delimited by blank .. change as required
ActiveCell.Value = AciveCell & Me.ListBox1.List(i) & " "
End If
Next i
End Sub


"HK" wrote:

I am afraid I don't know much about VBA or programming. What I have tried to
do is to find pieces of VBA code in the net and then try to see if it works
for me. What I'd need the multiselect ActiveX listbox to do is that all the
values selected are saved in the active cell.

What I've managed to do so far with this piece of code is that I get a
listbox activated when I double clicks in a cell which contains a validation
list. When I click on the CommandButton1, only the first value which is
selected is transfered to the active cell. How could I get also the rest of
the values transfered to the same cell?

Harri

'==========================
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim wsList As Worksheet
Set ws = ActiveSheet
Set wsList = Sheets("ValidationLists")

Cancel = True
Set cboTemp = ws.OLEObjects("ListBox1")
On Error Resume Next
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 45
.Height = Target.Height + 100
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
End If

errHandler:
Application.EnableEvents = True
Exit Sub

End Sub
--------------
Private Sub CommandButton1_Click()
Dim i As Long
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) Then
ActiveCell.Value = Me.ListBox1.List(i)
ActiveCell.Offset(1, 0).Select
End If
Next i
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
getting selected listbox values x taol Excel Programming 1 January 27th 06 12:39 AM
How to add selected row into listbox? frankosun Excel Programming 1 December 17th 05 10:01 PM
Fill values into a listbox matching selected values from a combobox Jon[_19_] Excel Programming 4 January 25th 05 04:25 PM
referencing values in adjacent cells to selected cell Darren Haslett Excel Programming 3 February 23rd 04 05:45 PM
Moving cell values from sheet2 to sheet1 using UserForms and ListBox Stein Kristiansen Excel Programming 0 November 20th 03 07:13 AM


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