PDA

View Full Version : How to continue typing code onto another line



andi
03-11-2008, 10:23 AM
Dear All,

I am very new to VBA coding, but will be doing alot over the coming months, so I will probably become quite a regular for your expert help!

Please bare with me if this is a really obvious, easy to solve problem.

Within Word, I have created a Form that has a Combo Box.

I have then gone into Design Mode and the combo box successfully so far.

The problem is I am having is, as I have so many entries I wish to have in the Combo Box I have reached the end of the line and I am unable to type anymore on that line. I have tried dropping down a line and continued typing with the speech marks, but that just returns errors.

If it helps, my Code is;

Private Sub Document_Open()
Dim vWorkType, vWorkTypes
vWorkTypes = Array(" ", "Commercial Lease - Landlord", "Commercial Lease - Tenant", "Commercial Lease - Licence to Assign", "Commercial - Purchase", "Commercial - Sale", "Commercial - Option", "Commercial - Development Agreement", "Commercial - Financing", "Commercial - Planning S106", "House Builder - Purchase", "House Builder - Sale", "House Builder - Plot Sales", "House Builder - Option", "House Builder - Development Agreement", "House Builder - Financing", "House Builder - Planning S106", "Local Authority - Purchase", "Local Authority - Sale", "Local Authority - Lease", "Local Authority - Secondment", "Local Authority - Planning S106", "Development Agreement", "Planning - Other", "Secured Lending", "Shareholders Agreeements", "LLP Conversions", "LLP Agreements", "Reorganisations", "Distribution Agreement", "Supply Agreement", "Terms & Conditions of Business", "Secondment", "General Commercial", "Software", "Trademarks", "Copyright", "Confidential Information", "Patents", "Design Rights")

For Each vWorkType In vWorkTypes
ComboBox1.AddItem vWorkType
Next
ComboBox1.ListIndex = 0
End Sub

All I want to do is add more items to my Combo Box, please help!

In advance, thank you for your help,

Any Feedback, help or suggestions will be very useful.

Kind Regards,

Andi

Tinbendr
03-11-2008, 12:01 PM
Word doesn't support more than 25 entries with the forms dropdown.

You'll have to move this into a Userform dropdown.

andi
03-12-2008, 02:12 AM
I already have 40 items in my Combo Box though.

The problem is, within the VBA code window, I have now run out of typing space as I have reached the end of the line, all I want to do is continue typing onto another line, but when I do so, errors appear in my code. (Like expecting a close bracket or a speech mark)

Again, thank you for your help in advance,

Andi

mdmackillop
03-12-2008, 05:23 AM
Hi Andi,
Welcome to VBAX
Use Space and Underscore to break code and the Ampersand to join split text .

Sub LongCode()
MsgBox "This is long code", _
vbCritical
MsgBox "This is longer text" & _
" and needs to be split on two lines", _
vbInformation
End Sub

andi
03-12-2008, 05:29 AM
Mdmackillop,

That is very very helpful, thank you very much.

Easy when you know how!

Tinbendr
03-12-2008, 07:06 AM
Yes, of course! :doh: I was trying to make it too difficult.

You can also add the entire list at one time without looping.

Private Sub Document_Open()
Dim vWorkTypes As Variant
vWorkTypes = Array(" ", "Commercial Lease - Landlord", "Commercial Lease - Tenant", _
"Commercial Lease - Licence to Assign", "Commercial - Purchase", _
"Commercial - Sale", "Commercial - Option", _
"Commercial - Development Agreement", "Commercial - Financing", _
"Commercial - Planning S106", "House Builder - Purchase", _
"House Builder - Sale", "House Builder - Plot Sales", _
"House Builder - Option", "House Builder - Development Agreement", _
"House Builder - Financing", "House Builder - Planning S106", _
"Local Authority - Purchase", "Local Authority - Sale", _
"Local Authority - Lease", "Local Authority - Secondment", _
"Local Authority - Planning S106", "Development Agreement", _
"Planning - Other", "Secured Lending", _
"Shareholders Agreeements", "LLP Conversions", "LLP Agreements", _
"Reorganisations", "Distribution Agreement", _
"Supply Agreement", "Terms & Conditions of Business", _
"Secondment", "General Commercial", "Software", "Trademarks", _
"Copyright", "Confidential Information", "Patents", "Design Rights")

UserForm1.ComboBox1.List = vWorkTypes

UserForm1.ComboBox1.ListIndex = 0
UserForm1.Show
End Sub

fumei
03-12-2008, 09:45 AM
Tinbendr, while you are correct that a FormField has a max of 25 items....it is NOT a formfield. The clue in the OP:

"I have then gone into Design Mode" - my bold.

and:

"ComboBox1.AddItem"

Therefore, it is not a formfield. It has nothing to do with a userform. It could be, but the fact that the original code was in Document_Open also points to a ActiveX combobox IN the document, not a userform.

Excellent point/suggestion about using the List property, rather than a loop. It is always slower to use a loop. Although you do not have to declare the variable as a Variant. Any (and all) unspecified variables ARE Variants.
Private Sub Document_Open()
Dim vWorkTypes()

vWorkTypes = Array(" ", "Commercial Lease - Landlord", _
"Commercial Lease - Tenant", _
"Commercial Lease - Licence to Assign", _
"Commercial - Purchase", "Commercial - Sale", _
"Commercial - Option", "Commercial - Development Agreement", _
"Commercial - Financing", "Commercial - Planning S106", _
"House Builder - Purchase", "House Builder - Sale", _
"House Builder - Plot Sales", "House Builder - Option", _
"House Builder - Development Agreement", _
"House Builder - Financing", "House Builder - Planning S106", _
"Local Authority - Purchase", "Local Authority - Sale", _
"Local Authority - Lease", "Local Authority - Secondment", _
"Local Authority - Planning S106", "Development Agreement", _
"Planning - Other", "Secured Lending", _
"Shareholders Agreeements", "LLP Conversions", _
"LLP Agreements", "Reorganisations", "Distribution Agreement", _
"Supply Agreement", "Terms & Conditions of Business", _
"Secondment", "General Commercial", "Software", _
"Trademarks", "Copyright", "Confidential Information", _
"Patents", "Design Rights")

With ComboBox1
.List = vWorkTypes
.ListIndex = 0
End With
End Sub


andi, I am not sure about having the first item as blank. Usually a notice is there, something like:
vWorkTypes = Array("Please select an item", _
"Commercial Lease - Landlord", _
"Commercial Lease - Tenant",
....etc.With ListIndex = 0, the user will see "Please select an item", rather than a blank. Not a critical point, but something to consider. If you have later processing that picks up what they selected, you can logic out ListIndex 0, or use it to know that they did NOT select something.

fumei
03-12-2008, 09:54 AM
Also, if I may add/reiterate....

Use Space and Underscore to break code and the Ampersand to join split text .

This applies to any code posted here as well.....ahem.