I have a UserForm with two listboxes. Three buttons to add, remove, and transfer. It works great except for when the selected LB items are too long for the cells. I need to have them split into a new line for each value that exceeds max character length. I found some code that will do that for a specific line of text, but despite my best efforts, I can't find a way to incorporate the two.
Transfer button:
Private Sub JLParaTransfer_Click()
Dim lItem As Long, lRows As Long, lCols As Long
Dim bSelected As Boolean
Dim lColLoop As Long, lTransferRow As Long
JLParaCB2.Value = False
JLParaCB2.Value = True
lTransferRow = 0
lRows = JLParaLB2.ListCount - 1
lCols = JLParaLB2.ColumnCount - 1
For lItem = 0 To lRows
If JLParaLB2.Selected(lItem) = True Then
bSelected = True
Exit For
End If
Next
If bSelected = True Then
With ActiveCell
For lItem = 0 To lRows 'Posts the new data
If JLParaLB2.Selected(lItem) = True Then
lTransferRow = lTransferRow + 1
For lColLoop = 0 To lCols
.Cells(lTransferRow, lColLoop + 1) = JLParaLB2.List(lItem, lColLoop)
Next lColLoop
End If
Next
End With
UnHook_Mouse 'Turns off the mouse
Unload Me
End If
JLParaCB2.Value = False
End Sub
The split code:
Dim s, m, v, o1, o2, j
m = 17 'max characters per line
s = "Amazingly few discotheques provide jukeboxes" 'the text
v = Split(s, " ")
For j = 0 To UBound(v)
If o2 = "" Then
If Len(v(j)) > m Then
o1 = o1 & v(j) & vbCrLf
j = j + 1
If j > UBound(v) Then Exit For
End If
End If
If Len(o2 & v(j)) > m Then
o1 = o1 & Trim(o2) & vbCrLf
o2 = ""
j = j - 1
Else
o2 = o2 & v(j) & " "
End If
If j = UBound(v) Then
o1 = o1 & Trim(o2)
End If
Next
MsgBox o1
Any help is greatly appreciated!