VBA Editor.. Long line / Line too long

Thebatfink

Active Member
Joined
Apr 8, 2007
Messages
410
Hi,

Its probably really simple but I'm scratching my head. I am programically inserting formulas into workbooks, and the formula for this range of cells is quite long. It spills over a line in the VBA editor but for some reason I don't seem to be able to use the usual underscore to continue on the next line?!

For example

Code:
sht.Range("AE" & rnumber & "").Formula = "=IF(OR($I" & rnumber & "="""",$AD" & rnumber & "=""""),"""",IF($I" & rnumber & "=""BLAHBLAHBLAHBLAH"",""BLAHBLAHBLAH"",IF(LEFT($I" & rnumber & ",2)=""BLAH"",""BLAHBLAH"",IF($I" & rnumber & "-$AD" & rnumber & "<=-1,""Yes"",""No"".......

its obviously longer than that but if I put for example the underscore in and some quotes

Code:
sht.Range("AE" & rnumber & "").Formula = "=IF(OR($I" & rnumber & "="""",$AD" & rnumber & "=""""),"""",IF($I" & rnumber & "=""BLAHBLAHBLAHBLAH"",""BLAHBLAHBLAH"",IF(LEFT($I" & rnumber & ",2)" _
"=""BLAH"",""BLAHBLAH"",IF($I" & rnumber & "-$AD" & rnumber & "<=-1,""Yes"",""No"".......

it doesn't like it at all. I'd appreciate it if someone can kick me in the right direction!

Thanks!
Batfink
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try breaking it on an "IF( " as in

sht.Range("AE" & rnumber & "").Formula = "=IF(OR($I" & rnumber & "="""",$AD" & rnumber & "=""""),"""", _
IF($I" & rnumber & "=""BLAHBLAHBLAHBLAH"",""BLAHBLAHBLAH"",IF(LEFT($I" & rnumber & ",2)" "=""BLAH"",""BLAHBLAH"",IF($I" & rnumber & "-$AD" & rnumber & "<=-1,""Yes"",""No"".......
 
Upvote 0
Oh my.. I'm sure every now and again 99% of my brain cells go dormant.

Thanks for the help guys. Sorry to be so stupid!
 
Upvote 0
Actually no. It still doesn't work, just tried it. Thats why I started putting quotes in because I suspect as its in quotes to begin with, the underscore isn't seen as normal.

Code:
sht.Range("AG" & rnumber & "").Formula = "=IF(R6="""", ""BLAHBLAH"",""BLAH"")"

simple one above ..

Code:
sht.Range("AG" & rnumber & "").Formula = "=IF(R6="""", _
    ""BLAHBLAH"",""BLAH"")"

this doesnt work. Compile error - expected end of statement. I suspect its because what I'm breaking is within quotes (like why I need to double up all quotes in the formula etc). I just can't get my head round how the syntax should be.

Thanks!
 
Upvote 0
Okay, the answer is: you can't break a line of VBA code inside a string. If you want to break a line inside a string you have to terminate the first portion with ", insert the underscore, then commence the next line with an ampersand and a new opening quote for the second portion of the string.

Using your example:-
Code:
sht.Range("AG" & rnumber & "").Formula = "=IF(R6="""",""BLAHBLAH"",""BLAH"")"
becomes:-
Code:
sht.Range("AG" & rnumber & "").Formula = "=IF(R6="""",""BLAHBL[COLOR=red][B]" _[/B][/COLOR]
[COLOR=red][B]  & "[/B][/COLOR][COLOR=#000000]AH"",""BLAH"")"
[/COLOR]
or:-
Code:
sht.Range("AG" & rnumber & "").Formula = "=IF(R6="""",""BLAHBLAH""[COLOR=red][B]" _[/B][/COLOR]
[COLOR=red][B]  & "[/B][/COLOR],""BLAH"")"

If you can't get it to work, post the entire line of code - without any breaks in it - and I'll take a look at it later tonight when I get home.
 
Upvote 0
After you resolve your syntax issue, I find it easier when building a complex formula to assign it to a string variable. You can print it to the Immediate window and see if it's well-formed, rather than just getting an uninformative 1004 error.
Code:
    Dim sFrm        As String
 
    sFrm = "= big complicated formula spread over " & _
           "several lines"
    Debug.Print sFrm
    Range("A1").Formula = sFrm
 
Upvote 0
Morning,

Thanks Ruddles, that's solved my problem :-) works well now, much appreciated thanks for your time!

Shg, I'm all for tips from the folks in the know, I'll give that a try, thanks very much!

Cheers
Batfink
 
Last edited:
Upvote 0
I find it easier when building a complex formula to assign it to a string variable. You can print it to the Immediate window and see if it's well-formed, rather than just getting an uninformative 1004 error.
thumbs-up-150x150.jpg
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,486
Members
452,917
Latest member
MrsMSalt

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