Break line of code into multiple lines

coop123

Board Regular
Joined
Dec 18, 2018
Messages
66
Office Version
  1. 365
Hi

I am trying to split a long line of code into multiple line, I have entered space then under score and hit carrage return but keep getting error message compile error: expected end of statement. could someone explain what I am doing wrong.

ActiveCell.FormulaR1C1 = _
"=IF(RC[-3]=1201,""MWO GEN"",IF(RC[-3]=1202,""PROD GEN"",IF(RC[-3]=1203,""INC INSP"",IF(RC[-3]=1221,""MWO PROD"",IF(RC[-3]=3011,""PURCH"",IF(RC[-3]=3041,""MAINT"",IF(RC[-3]=3201,""ENG"",IF(RC[-3]=5000,""MD OFFICE"",IF(RC[-3]=5011,""MD OFFICE"",IF(RC[-3]=5042,""QC"",IF(RC[-3]=5051,""FINANCE"",IF(RC[-3]=5052,""HR"",IF(RC[-3]=5053,""EDP"",IF(RC[-3]=5054,""EDP""))))))))))))))"

See code with split entered giving error message

ActiveCell.FormulaR1C1 = _
"=IF(RC[-3]=1201,""MWO GEN"",IF(RC[-3]=1202,""PROD GEN"", _
IF(RC[-3]=1203,""INC INSP"",IF(RC[-3]=1221,""MWO PROD"",IF(RC[-3]=3011,""PURCH"",IF(RC[-3]=3041,""MAINT"",IF(RC[-3]=3201,""ENG"",IF(RC[-3]=5000,""MD OFFICE"",IF(RC[-3]=5011,""MD OFFICE"",IF(RC[-3]=5042,""QC"",IF(RC[-3]=5051,""FINANCE"",IF(RC[-3]=5052,""HR"",IF(RC[-3]=5053,""EDP"",IF(RC[-3]=5054,""EDP""))))))))))))))"
Thanks for any assistance

coop123
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You can't split within the formula string as the underscore will become part of the formula. You need to close the string and then concatenate with the next line - eg:

VBA Code:
ActiveCell.FormulaR1C1 = _
"=IF(RC[-3]=1201,""MWO GEN"",IF(RC[-3]=1202,""PROD GEN""," & _
"IF(RC[-3]=1203,""INC INSP"",IF(RC[-3]=1221,""MWO PROD"",IF(RC[-3]=3011,""PURCH"",IF(RC[-3]=3041,""MAINT"",IF(RC[-3]=3201,""ENG"",IF(RC[-3]=5000,""MD OFFICE"",IF(RC[-3]=5011,""MD OFFICE"",IF(RC[-3]=5042,""QC"",IF(RC[-3]=5051,""FINANCE"",IF(RC[-3]=5052,""HR"",IF(RC[-3]=5053,""EDP"",IF(RC[-3]=5054,""EDP""))))))))))))))"
 
Upvote 0
Solution
This is not answering your question but I think you would be better served by having a lookup table which is much easier to maintain than a very messy nested if statement.
Sample below:

20221222 VBA Vlookup instead of nested IF coop123.xlsm
ABCDEF
1
2
3Cost CentreDescriptionCost Centre3011
41201MWO GENVlookupPURCH=VLOOKUP(E3,$A$4:$B$17,2,FALSE)
51202PROD GEN
61203INC INSP
71221MWO PROD
83011PURCH
93041MAINT
103201ENG
115000MD OFFICE
125011MD OFFICE
135042QC
145051FINANCE
155052HR
165053EDP
175054EDP
VLookup
Cell Formulas
RangeFormula
E4E4=VLOOKUP(E3,$A$4:$B$17,2,FALSE)
F4F4=FORMULATEXT(E4)
 
Upvote 0
Hi Rory

That solved my problem. It makes sence now you have explained.

Thanks again

coop123
 
Upvote 0

Forum statistics

Threads
1,215,204
Messages
6,123,630
Members
449,109
Latest member
Sebas8956

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