Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default listbox concatenate

I need to figure out the code to concatenate multiple selections from
a list box into one cell with a comma as a divider.

This is what I have pieced together from a few sources so far. I get
an error when running this, so I know it doesn't work. Any help would
be appreciated.

Private Sub OK_exp_Click()
Dim CellRange As Range
Dim CellCnt As Integer
Dim r As Integer
Dim x As Range
Dim w As String
Dim y As Range
Dim z As Range
Dim sbuf As String


w = ", "
'z = Range("Sheet2!A1")

CellCnt = 0
For r = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(r) Then
CellCnt = CellCnt + 1
If CellCnt = 1 Then
Set CellRange = ActiveSheet.UsedRange.Cells(r + 1)
Else
Set CellRange = Union(CellRange,
ActiveSheet.UsedRange.Cells(r + 1))
End If
End If
Next r
For Each y In CellRange
If Len(y.Text) 0 Then sbuf = sbuf & y.Text & w
Next
z = Left(sbuf, Len(sbuf) - 1)
ActiveCell.Value = z
Unload Me

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default listbox concatenate

It would help to know which line is throwing the error and what the error is.
However, the following snippet probably would throw and error because you
are trying to form a union with an undefined range. If the else statement
executes, it is because CellRange was not set in the first part of the if
statement and therefore it is still undefined so you might get a type
mismatch or something similar to tell you it can't form the union, or it will
ignore it altogether.

Else
Set CellRange = Union(CellRange,
ActiveSheet.UsedRange.Cells(r + 1))

I also do not see where you have assigned a value to sbuf prior to using is
as part of it's own value. So that will probably throw an error also.

If Len(y.Text) 0 Then sbuf = sbuf & y.Text & w

Your CelCnt counter needs to be moved from the beginning of the For ... Next
Loop to the end as in the following example.

For r = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(r) Then
If CellCnt = 1 Then
Set CellRange = ActiveSheet.UsedRange.Cells(r + 1)
Else
Set CellRange = Union(CellRange,
ActiveSheet.UsedRange.Cells(r + 1))
End If
End If
CellCnt = CellCnt + 1
Next r

Maybe if you could define what you want to do with a little more detail,
someone could offer a better suggestion.


"Shane" wrote:

I need to figure out the code to concatenate multiple selections from
a list box into one cell with a comma as a divider.

This is what I have pieced together from a few sources so far. I get
an error when running this, so I know it doesn't work. Any help would
be appreciated.

Private Sub OK_exp_Click()
Dim CellRange As Range
Dim CellCnt As Integer
Dim r As Integer
Dim x As Range
Dim w As String
Dim y As Range
Dim z As Range
Dim sbuf As String


w = ", "
'z = Range("Sheet2!A1")

CellCnt = 0
For r = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(r) Then
CellCnt = CellCnt + 1
If CellCnt = 1 Then
Set CellRange = ActiveSheet.UsedRange.Cells(r + 1)
Else
Set CellRange = Union(CellRange,
ActiveSheet.UsedRange.Cells(r + 1))
End If
End If
Next r
For Each y In CellRange
If Len(y.Text) 0 Then sbuf = sbuf & y.Text & w
Next
z = Left(sbuf, Len(sbuf) - 1)
ActiveCell.Value = z
Unload Me

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default listbox concatenate

It was throwing me the error on the z= left... line

The message that it was giving me was
Run-time error '5':


Invalid procedure call or argument


What I want to do is take a list put it in a list box, select multiple
selections and then have them paste into one cell with a comma as a
divider.

I have the list box and the checkmarks. The hard part for me is the
code to do what I described above.

Thanks for your help






On Aug 16, 7:03 pm, JLGWhiz wrote:
It would help to know which line is throwing the error and what the error is.
However, the following snippet probably would throw and error because you
are trying to form a union with an undefined range. If the else statement
executes, it is because CellRange was not set in the first part of the if
statement and therefore it is still undefined so you might get a type
mismatch or something similar to tell you it can't form the union, or it will
ignore it altogether.

Else
Set CellRange = Union(CellRange,
ActiveSheet.UsedRange.Cells(r + 1))

I also do not see where you have assigned a value to sbuf prior to using is
as part of it's own value. So that will probably throw an error also.

If Len(y.Text) 0 Then sbuf = sbuf & y.Text & w

Your CelCnt counter needs to be moved from the beginning of the For ... Next
Loop to the end as in the following example.

For r = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(r) Then
If CellCnt = 1 Then
Set CellRange = ActiveSheet.UsedRange.Cells(r + 1)
Else
Set CellRange = Union(CellRange,
ActiveSheet.UsedRange.Cells(r + 1))
End If
End If
CellCnt = CellCnt + 1
Next r

Maybe if you could define what you want to do with a little more detail,
someone could offer a better suggestion.



"Shane" wrote:
I need to figure out the code to concatenate multiple selections from
a list box into one cell with a comma as a divider.


This is what I have pieced together from a few sources so far. I get
an error when running this, so I know it doesn't work. Any help would
be appreciated.


Private Sub OK_exp_Click()
Dim CellRange As Range
Dim CellCnt As Integer
Dim r As Integer
Dim x As Range
Dim w As String
Dim y As Range
Dim z As Range
Dim sbuf As String


w = ", "
'z = Range("Sheet2!A1")


CellCnt = 0
For r = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(r) Then
CellCnt = CellCnt + 1
If CellCnt = 1 Then
Set CellRange = ActiveSheet.UsedRange.Cells(r + 1)
Else
Set CellRange = Union(CellRange,
ActiveSheet.UsedRange.Cells(r + 1))
End If
End If
Next r
For Each y In CellRange
If Len(y.Text) 0 Then sbuf = sbuf & y.Text & w
Next
z = Left(sbuf, Len(sbuf) - 1)
ActiveCell.Value = z
Unload Me


End Sub- Hide quoted text -


- Show quoted text -



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
userform listbox cannot get listbox.value to transfer back to main sub [email protected] Excel Programming 1 May 17th 06 09:44 PM
VBA: Creating listbox similar to the one in Pivot table (Listbox+Checkbox) modjoe23 Excel Programming 3 August 18th 05 02:35 PM
I know how to concatenate ,can one de-concatenate to split date? QUICK BOOKS PROBLEM- New Users to Excel 1 July 26th 05 05:07 PM
listbox.value not equal to listbox.list(listbox.listindex,0) ARB Excel Programming 0 October 22nd 03 12:46 AM
Is refreshing listbox rowsource in listbox click event possible? Jeremy Gollehon[_2_] Excel Programming 4 September 25th 03 06:45 PM


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