Multiple listbox problem

A

Amy

I want to create multiple list boxes in a user form, and then have the
selections appear in corresponding bookmarks.

I found this bit of code, but I don't understand why selections for both
Activity1 and Activity2 appear in the Activity2 bookmark. How can I make sure
that only Activity2 selections appear in Activity2 bookmark? Thanks!

For i = 1 To Activity1.ListCount
If Activity1.Selected(i - 1) Then
myString = myString + Activity1.List(i - 1) & vbCrLf
End If
Next
ActiveDocument.Bookmarks("Activity1").Range.InsertBefore myString

For i = 1 To Activity2.ListCount
If Activity2.Selected(i - 1) Then
myString = myString + Activity2.List(i - 1) & vbCrLf
End If
Next
ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
 
R

Russ

Reset myString =""
Between the loops.
I want to create multiple list boxes in a user form, and then have the
selections appear in corresponding bookmarks.

I found this bit of code, but I don't understand why selections for both
Activity1 and Activity2 appear in the Activity2 bookmark. How can I make sure
that only Activity2 selections appear in Activity2 bookmark? Thanks!

For i = 1 To Activity1.ListCount
If Activity1.Selected(i - 1) Then
myString = myString + Activity1.List(i - 1) & vbCrLf
End If
Next
ActiveDocument.Bookmarks("Activity1").Range.InsertBefore myString

For i = 1 To Activity2.ListCount
If Activity2.Selected(i - 1) Then
myString = myString + Activity2.List(i - 1) & vbCrLf
End If
Next
ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
 
R

Russ

Since this is the beginner's forum, I'll explain a little more.
The line:
myString = myString + ...

Probably should be:
myString = myString & ...
And means set myString equal to the current contents of myString and
concatenate whatever follows. The plus (+) should be left for arithmetic
operations like MyNumber = MyNumber + 1 to increment MyNumber.

Between the loops you still have the old stuff in myString, until you reset
it to an empty string with "". So after you use myString with the bookmark,
reset it.
 
A

Amy

I see. Looks good. Thanks!

Russ said:
Since this is the beginner's forum, I'll explain a little more.
The line:
myString = myString + ...

Probably should be:
myString = myString & ...
And means set myString equal to the current contents of myString and
concatenate whatever follows. The plus (+) should be left for arithmetic
operations like MyNumber = MyNumber + 1 to increment MyNumber.

Between the loops you still have the old stuff in myString, until you reset
it to an empty string with "". So after you use myString with the bookmark,
reset it.
 
A

Amy

Is there a way to remove the paragraph mark from the last list item? As it is
now, I have an extra paragraph at the end of the list.

Thank you!
Amy
 
R

Russ

Yes below the Next line use:
myString = Left(myString, Len(myString) - 1)

Nested functions usually are parsed from the inside out.
So first the number of characters in myString are found with Len().
1 is subtract from that number.
The result is used to tell Left() how many characters on the left side of
the myString we want.
And finally, myString is set equal to that result.

So we are dropping off the last character, which was a vbCrLf character.
 
A

Amy

Ah interesting. I'd like to give users the option to not have to fill out
everything. With this line, seems like they need to fill out all the
listboxes. Is that true?

Amy
 
R

Russ

Amy,
The code snippet that you have shown is apparently inserting selected
choices at bookmarks and that is all I see it doing. I don't see where it is
validating the choices. If you want the computer to do something different,
then you need to tell it to do something different. A computer tries to do
what you tell it to do, not necessarily what you intend it to do. I have no
idea why you believe users are forced to fill out everything. Are there
error messages that pop up when everything is not filled out? Can the user
not skip over a listbox? Is that the way you set up the form?

"With this line, seems like they need to fill out all the
listboxes. Is that true?" What line are you talking about? You must have
forgotten to include the line you were talking about in your reply. The line
I gave you, *myString = Left(myString, Len(myString) - 1)*, just dropped off
the last paragraph mark, like you requested. You were suppose to insert it
under the two lines in the code snippet that just had the word "Next".
Didn't it remove the last paragraph mark for you?
 
A

Amy

When I add myString = Left(myString, Len(myString) - 1) after Next, I get an
error message that says "Run-time error 5, Invalid procedure call or
argument." Then when I switch to the VB screen, myString = Left(myString,
Len(myString) - 1) is highlighted.

This error occurs when I leave the listbox blank. When I remove myString =
Left(myString, Len(myString) - 1), then I do not get an error when I leave
the listbox blank. What is the reason for this? I've provided more of my code
below, for your reference. Thanks so much for your assistance! Amy

My code is:

Private Sub CommandButton1_Click()
Dim i As Long
Dim myString As String

With ActiveDocument

For i = 1 To Activity1.ListCount
If Activity1.Selected(i - 1) Then
myString = myString & Activity1.List(i - 1) & vbCrLf
End If
Next
myString = Left(myString, Len(myString) - 1)
ActiveDocument.Bookmarks("Activity1").Range.InsertBefore myString
myString = ""

For i = 1 To Activity2.ListCount
If Activity2.Selected(i - 1) Then
myString = myString & Activity2.List(i - 1) & vbCrLf
End If
Next
myString = Left(myString, Len(myString) - 1)
ActiveDocument.Bookmarks("Activity2").Range.InsertBefore myString
myString = ""

End With

Me.Hide

End Sub
 
R

Russ

Encapsulate that line with an empty string check.
If Not myString = "" then
myString = Left(myString, Len(myString) - 1)
End If
 
R

Russ

I think you get an error because with an empty string the Len (or length) is
zero, then trying to subtract a 1 to get rid of a trailing paragraph mark
gives a -1, which the Left() function does not expect, it wants positive
numbers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top