How to separate on very long line of code in VBA

A

Andrew

Hello,
I'm trying to figure out how to put a single command onto several
lines. This is necessary when the line of code is much wider than the
editing screen. I see in examples that people use _ to separate lines
of code. When I try this my code turns red and VBA tells me I have an
error. Can someone please explain how this is done.

thanks,
Andy
 
C

Clif McIrvin

Andrew said:
Hello,
I'm trying to figure out how to put a single command onto several
lines. This is necessary when the line of code is much wider than the
editing screen. I see in examples that people use _ to separate lines
of code. When I try this my code turns red and VBA tells me I have an
error. Can someone please explain how this is done.

thanks,
Andy


you need a space before the _
 
C

Clif McIrvin

Clif McIrvin said:
you need a space before the _


from the VBA help glossary:
<quote>
line-continuation character
The combination of a space followed by an underscore ( _) used in the
development environment to extend a single logical line of code to two
or more physical lines. However, you can't use a line-continuation
character to continue a line of code within a string expression.</quote>

If the preceeding space wasn't you problem, did you enter a <cr> after
the underscore?

From the VBE window, press F1 for help and search for line continuation
for more information.
 
A

Andrew

from the VBA help glossary:
<quote>
line-continuation character
The combination of a space followed by an underscore ( _) used in the
development environment to extend a single logical line of code to two
or more physical lines. However, you can't use a line-continuation
character to continue a line of code within a string expression.</quote>

If the preceeding space wasn't you problem, did you enter a <cr> after
the underscore?

From the VBE window, press F1 for help and search for line continuation
for more information.

Thanks for the tip. I was trying to break a string. That explains
it.
 
C

Clif McIrvin

from the VBA help glossary:
<quote>
line-continuation character
The combination of a space followed by an underscore ( _) used in the
development environment to extend a single logical line of code to two
or more physical lines. However, you can't use a line-continuation
character to continue a line of code within a string
expression.</quote>

If the preceeding space wasn't you problem, did you enter a <cr> after
the underscore?

From the VBE window, press F1 for help and search for line
continuation
for more information.

Thanks for the tip. I was trying to break a string. That explains
it.
...........
You can break a string by using the & concatenation operator:

strLongValue = "First part of a very long string" & _
"second part of a very long string"

Glad you got it sorted!
 
N

norie

Thanks for the tip.  I was trying to break a string.  That explains
it.- Hide quoted text -

- Show quoted text -
Andrew

If it's a string you don't need to have everything on one line, you
can try something like this which is along the lines of Cliff's post.

strSQL = strSQL & "SELECT Field1, Field2, Field3, Field4 "
strSQL = strSQL & "FROM table1 "
strSQL = strSQL & "WHERE ID = 3 AND Field2 Like 'Something*' "

That's an example for an SQL statement but you can use the same idea
for any string.

One advantage of using this method is that if you are creating a
string for something like an SQL query or
perhaps a database connection is that you can comment out lines,
change lines, add lines etc

That should make it far easier to debug if you are getting errors or
the string just isn't appearing as you want it.
 

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