PC Review


Reply
Thread Tools Rate Thread

Breaking up long line of code doesn't work

 
 
Hector Fernandez
Guest
Posts: n/a
 
      17th Dec 2006
I am trying to perform a single event to multiple ranges but for some
reason I can't get the macro to work if I write it on multiple lines.

If, for example, I write:

Sub Clear_Ranges()

Range("range1,range2,range3") = clear

End Sub

The macro works just fine and clears the ranges. However, if I do the
following:

Sub Clear_Ranges()

Range("range1, _
range2, _
range3") = clear

End Sub

I get an error message "Compile Error: Expected: List separator or )"

I thought that using a space and underscore was suppose to allow you to
write a single line of code across several lines in order to make it
easier to read or fit in a page.

I intend to have a lot more than three ranges in the macro and it will
be very messy and long if I try to put it on a single line.

What am I doing wrong?
Is there an easier way to perform the same thing on several different
ranges?

Thanks.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?R2FyeScncyBTdHVkZW50?=
Guest
Posts: n/a
 
      17th Dec 2006
Try UNION()


Sub lots_of_ranges()
Dim range1 As Range, range2 As Range, range3 As Range, r As Range
Set range1 = Range("A1:A10")
Set range2 = Range("B4:b56")
Set range3 = Range("Z100:Z103")

Set r = Union(range1, range2, range3)
r.Clear
End Sub




--
Gary's Student


"Hector Fernandez" wrote:

> I am trying to perform a single event to multiple ranges but for some
> reason I can't get the macro to work if I write it on multiple lines.
>
> If, for example, I write:
>
> Sub Clear_Ranges()
>
> Range("range1,range2,range3") = clear
>
> End Sub
>
> The macro works just fine and clears the ranges. However, if I do the
> following:
>
> Sub Clear_Ranges()
>
> Range("range1, _
> range2, _
> range3") = clear
>
> End Sub
>
> I get an error message "Compile Error: Expected: List separator or )"
>
> I thought that using a space and underscore was suppose to allow you to
> write a single line of code across several lines in order to make it
> easier to read or fit in a page.
>
> I intend to have a lot more than three ranges in the macro and it will
> be very messy and long if I try to put it on a single line.
>
> What am I doing wrong?
> Is there an easier way to perform the same thing on several different
> ranges?
>
> Thanks.
>
>

 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      17th Dec 2006
Usually but it appears, in this case, no. So, just make several lines..
Range("range1,range2,range3") = clear
Range("range4,range5,range6") = clear
Of course, if contiguos, not necessary.

range("a2, c2:f2 ,l2")
--
Don Guillett
SalesAid Software
(E-Mail Removed)
"Hector Fernandez" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I am trying to perform a single event to multiple ranges but for some
> reason I can't get the macro to work if I write it on multiple lines.
>
> If, for example, I write:
>
> Sub Clear_Ranges()
>
> Range("range1,range2,range3") = clear
>
> End Sub
>
> The macro works just fine and clears the ranges. However, if I do the
> following:
>
> Sub Clear_Ranges()
>
> Range("range1, _
> range2, _
> range3") = clear
>
> End Sub
>
> I get an error message "Compile Error: Expected: List separator or )"
>
> I thought that using a space and underscore was suppose to allow you to
> write a single line of code across several lines in order to make it
> easier to read or fit in a page.
>
> I intend to have a lot more than three ranges in the macro and it will
> be very messy and long if I try to put it on a single line.
>
> What am I doing wrong?
> Is there an easier way to perform the same thing on several different
> ranges?
>
> Thanks.
>



 
Reply With Quote
 
Hector Fernandez
Guest
Posts: n/a
 
      17th Dec 2006
Gary, thanks for the response - unfortunately your method is more work
than simply writing

Range("range1") = clear
Range("range2") = clear
....and so forth

Is there some reason why the approach you suggest would be better?

Don, unless someone comes up with an alternative, I may have to resort
to your suggestion.

Thanks.

 
Reply With Quote
 
Mike Fogleman
Guest
Posts: n/a
 
      17th Dec 2006
You should not use line continuation within a parentheses. You should use
Don's suggestion.

Mike F
"Hector Fernandez" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Gary, thanks for the response - unfortunately your method is more work
> than simply writing
>
> Range("range1") = clear
> Range("range2") = clear
> ...and so forth
>
> Is there some reason why the approach you suggest would be better?
>
> Don, unless someone comes up with an alternative, I may have to resort
> to your suggestion.
>
> Thanks.
>



 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      17th Dec 2006
Hector,
That is a text string you are breaking up.
You need quote marks around each segment.
Using just line breaks won't work.

"This is a very long text string" can be split into three lines this way...
"This is a very " & _
"long " & _
"text string"
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"Hector Fernandez"
<(E-Mail Removed)>
wrote in message
I am trying to perform a single event to multiple ranges but for some
reason I can't get the macro to work if I write it on multiple lines.

If, for example, I write:
Sub Clear_Ranges()
Range("range1,range2,range3") = clear
End Sub

The macro works just fine and clears the ranges. However, if I do the
following:
Sub Clear_Ranges()
Range("range1, _
range2, _
range3") = clear
End Sub

I get an error message "Compile Error: Expected: List separator or )"
I thought that using a space and underscore was suppose to allow you to
write a single line of code across several lines in order to make it
easier to read or fit in a page.
I intend to have a lot more than three ranges in the macro and it will
be very messy and long if I try to put it on a single line.
What am I doing wrong?
Is there an easier way to perform the same thing on several different
ranges?
Thanks.

 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      17th Dec 2006
Did you try that for this case?

--
Don Guillett
SalesAid Software
(E-Mail Removed)
"Jim Cone" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hector,
> That is a text string you are breaking up.
> You need quote marks around each segment.
> Using just line breaks won't work.
>
> "This is a very long text string" can be split into three lines this
> way...
> "This is a very " & _
> "long " & _
> "text string"
> --
> Jim Cone
> San Francisco, USA
> http://www.realezsites.com/bus/primitivesoftware
>
>
> "Hector Fernandez"
> <(E-Mail Removed)>
> wrote in message
> I am trying to perform a single event to multiple ranges but for some
> reason I can't get the macro to work if I write it on multiple lines.
>
> If, for example, I write:
> Sub Clear_Ranges()
> Range("range1,range2,range3") = clear
> End Sub
>
> The macro works just fine and clears the ranges. However, if I do the
> following:
> Sub Clear_Ranges()
> Range("range1, _
> range2, _
> range3") = clear
> End Sub
>
> I get an error message "Compile Error: Expected: List separator or )"
> I thought that using a space and underscore was suppose to allow you to
> write a single line of code across several lines in order to make it
> easier to read or fit in a page.
> I intend to have a lot more than three ranges in the macro and it will
> be very messy and long if I try to put it on a single line.
> What am I doing wrong?
> Is there an easier way to perform the same thing on several different
> ranges?
> Thanks.
>



 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      17th Dec 2006

Don,
'rng1, rng2, rng3 are named ranges... A1, B2, C3

Sub AAA()
Range("rng1," & _
"rng2," & _
"rng3").Value = "Hector"
End Sub

Sub BBB()
Range("rng1," & _
"rng2," & _
"rng3").Clear
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Don Guillett" <(E-Mail Removed)>
wrote in message
Did you try that for this case?
--
Don Guillett
SalesAid Software
(E-Mail Removed)


"Jim Cone"
<(E-Mail Removed)>
wrote in message
> Hector,
> That is a text string you are breaking up.
> You need quote marks around each segment.
> Using just line breaks won't work.
>
> "This is a very long text string" can be split into three lines this
> way...
> "This is a very " & _
> "long " & _
> "text string"
> --
> Jim Cone
> San Francisco, USA
> http://www.realezsites.com/bus/primitivesoftware
>
>
> "Hector Fernandez"
> <(E-Mail Removed)>
> wrote in message
> I am trying to perform a single event to multiple ranges but for some
> reason I can't get the macro to work if I write it on multiple lines.
>
> If, for example, I write:
> Sub Clear_Ranges()
> Range("range1,range2,range3") = clear
> End Sub
>
> The macro works just fine and clears the ranges. However, if I do the
> following:
> Sub Clear_Ranges()
> Range("range1, _
> range2, _
> range3") = clear
> End Sub
>
> I get an error message "Compile Error: Expected: List separator or )"
> I thought that using a space and underscore was suppose to allow you to
> write a single line of code across several lines in order to make it
> easier to read or fit in a page.
> I intend to have a lot more than three ranges in the macro and it will
> be very messy and long if I try to put it on a single line.
> What am I doing wrong?
> Is there an easier way to perform the same thing on several different
> ranges?
> Thanks.
>



 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      17th Dec 2006
i've use something like this before, seemed to work fine. using the same method
you show here

Range("this_is_range1,this_is_range2," & _
"this_is_range3,this_is_range4,this_is_range5, " & _
"this_is_range6,this_is_range7").Value = "hector"

--


Gary


"Jim Cone" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> Don,
> 'rng1, rng2, rng3 are named ranges... A1, B2, C3
>
> Sub AAA()
> Range("rng1," & _
> "rng2," & _
> "rng3").Value = "Hector"
> End Sub
>
> Sub BBB()
> Range("rng1," & _
> "rng2," & _
> "rng3").Clear
> End Sub
> --
> Jim Cone
> San Francisco, USA
> http://www.realezsites.com/bus/primitivesoftware
>
>
>
> "Don Guillett" <(E-Mail Removed)>
> wrote in message
> Did you try that for this case?
> --
> Don Guillett
> SalesAid Software
> (E-Mail Removed)
>
>
> "Jim Cone"
> <(E-Mail Removed)>
> wrote in message
>> Hector,
>> That is a text string you are breaking up.
>> You need quote marks around each segment.
>> Using just line breaks won't work.
>>
>> "This is a very long text string" can be split into three lines this
>> way...
>> "This is a very " & _
>> "long " & _
>> "text string"
>> --
>> Jim Cone
>> San Francisco, USA
>> http://www.realezsites.com/bus/primitivesoftware
>>
>>
>> "Hector Fernandez"
>> <(E-Mail Removed)>
>> wrote in message
>> I am trying to perform a single event to multiple ranges but for some
>> reason I can't get the macro to work if I write it on multiple lines.
>>
>> If, for example, I write:
>> Sub Clear_Ranges()
>> Range("range1,range2,range3") = clear
>> End Sub
>>
>> The macro works just fine and clears the ranges. However, if I do the
>> following:
>> Sub Clear_Ranges()
>> Range("range1, _
>> range2, _
>> range3") = clear
>> End Sub
>>
>> I get an error message "Compile Error: Expected: List separator or )"
>> I thought that using a space and underscore was suppose to allow you to
>> write a single line of code across several lines in order to make it
>> easier to read or fit in a page.
>> I intend to have a lot more than three ranges in the macro and it will
>> be very messy and long if I try to put it on a single line.
>> What am I doing wrong?
>> Is there an easier way to perform the same thing on several different
>> ranges?
>> Thanks.
>>

>
>



 
Reply With Quote
 
Hector Fernandez
Guest
Posts: n/a
 
      18th Dec 2006

Don, I live in Spain so it's late now, but will try Jim and Gary's
recommendations tomorrow morning (my time) and let you guys know how it
went.

Thanks to all of you.

Héctor

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to separate on very long line of code in VBA Andrew Microsoft Excel Programming 5 7th Nov 2010 12:17 PM
Code to space long code to next line Pamela Microsoft Access Forms 1 24th Dec 2009 02:11 PM
New line code doesn't work STom Microsoft C# .NET 5 17th Jan 2005 07:56 PM
line breaking rules or lines wrap like work 6.0 =?Utf-8?B?Y2FydmluNXN0cmluZw==?= Microsoft Word Document Management 1 29th Dec 2004 07:01 PM
Function getting WAY to long and complex!!!! And it still doesn't work! Craig Newell Microsoft Excel Worksheet Functions 0 11th May 2004 05:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:10 PM.