How to separate on very long line of code in VBA

  • Thread starter Thread starter Andrew
  • Start date Start date
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
 
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 _
 
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.
 
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.
 
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!
 
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.
 
Back
Top