Help Needed for user form command button

freeisgood

New Member
Joined
Jan 18, 2008
Messages
31
Hello All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Codes below is for a command button of a user form (codes found on the web), for each execution of this command, items selected by users is separated by a comma. My goal is to have them separate by a break line. I added “char(10)” to line 6 "strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem)". It works, with the exception that it start by a break line then entry selected by users. <o:p></o:p>
<o:p></o:p>
I am seeking help to make the necessary adjustment to remove the unnecessary break line (first one only). I am learning VBA and a quick explanation would be greatly appreciated.<o:p></o:p>
<o:p></o:p>
Code:<o:p></o:p>
Private Sub CommandButton1_Click()<o:p></o:p>
<o:p></o:p>
Dim lngItem As Long, strItems<o:p></o:p>
<o:p></o:p>
With ListBox1<o:p></o:p>
For lngItem = 0 To ListBox1.ListCount - 1<o:p></o:p>
If ListBox1.Selected(lngItem) Then<o:p></o:p>
strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem) – 1<o:p></o:p>
<o:p></o:p>
End If<o:p></o:p>
Next lngItem<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
ActiveCell.Value = Replace(strItems, ",", "", 1, 1)<o:p></o:p>
<o:p></o:p>
Unload Me<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
Regards<o:p></o:p>
VL<o:p></o:p>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hello,

I think I understand. Maybe the addition of an IF like:

Code:
If strItems = "" Then
    srtitems = strItems & "," & ListBox1.List(lngItem) – 1
Else
    strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem) – 1
End If

in place of:

Code:
    strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem) – 1

-Jeff
 
Upvote 0
Also wanted to add it may be a good Idea to set srtItems to "" in the beginning.

Here is an updated version:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br><br><SPAN style="color:#00007F">Dim</SPAN> lngItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, strItems<br><br>strItems = ""<br><br>    <SPAN style="color:#00007F">With</SPAN> ListBox1<br>        <SPAN style="color:#00007F">For</SPAN> lngItem = 0 <SPAN style="color:#00007F">To</SPAN> ListBox1.ListCount - 1<br>            <SPAN style="color:#00007F">If</SPAN> ListBox1.Selected(lngItem) <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> strItems = "" <SPAN style="color:#00007F">Then</SPAN><br>                    strItems = strItems & "," & ListBox1.List(lngItem) – 1<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem) – 1<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> lngItem<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br>    ActiveCell.Value = Replace(strItems, ",", "", 1, 1)<br><br>Unload Me<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Hi Repairman615,<!-- / sig -->
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Thanks for trying, but your alternative doesn’t work. I am getting an “Compile error: Syntax error”.

VL<o:p></o:p>
 
Upvote 0
Hello,

I did have a typo in the variable.

Did you catch srtItem and change to strItem?

I copied and pasted codes from post #3 which contain no typo, still same error pop up.
C:\Documents and Settings\vxl693\Desktop\VBA Command Button Error.JPG
 
Upvote 0
I found the mistake, there no “-1” needed for line #8 and #10. Should be:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
If strItems = "" Then<o:p></o:p>
strItems = strItems & "," & ListBox1.List(lngItem)<o:p></o:p>
Else<o:p></o:p>
strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem)<o:p></o:p>
End If<o:p></o:p>
<o:p> </o:p>
Thanks for your help Repairman615!<o:p></o:p>
 
Upvote 0
Hmm,

I am at a lose for certainty, yet maybe the "-1" should go inside the parens??


I am tyring to duplicate on a userform etc. I also get a syntax error.

after changing:
Code:
strItems = strItems & "," & Chr(10) & ListBox1.List([COLOR=red]lngItem) – 1[/COLOR]

to:
Code:
strItems = strItems & "," & ListBox1.List([COLOR=red]lngItem - 1)[/COLOR]

Seemed to debug without error.

Does that also work for you?

Also if the first entry was selected, would this be attempting to access list item -1? I get an error there.

Jeff
 
Upvote 0
You are welcome! :)

That seemed to also take care of the selecting the first item error I ws getting.

Does the activecell return the expected results?
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top