skull_eagle
Board Regular
- Joined
- Mar 25, 2011
- Messages
- 89
Hi Everyone,
I have written a formula containing 21 IF statements with 51 AND statements within.
I'm running a loop to to apply this formula to each row in a defined area, my problem is VBA won't let me run the formula over multiple rows. I'm using the " _" to try and get it to read the formula as a single line and it ain't working.
I keep getting Syntax errors or expected end statement errors.
This is an example:
ActiveCell.FormulaR1C1 = "=IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(16,0,0), RC[-12]>TIME(10,30,0), RC[-2]>TIME(16,0,0)), (TIME(0,30,0))+(RC[-2]-TIME(16,0,0)), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(10,30,0), RC[-12]<=TIME(16,0,0), RC[-2]<=TIME(16,0,0)), TIME(0,15,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]>TIME(10,30,0)), (TIME(0,30,0))+(RC[-2]-TIME(10,30,0)), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(16,0,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-13]=""Sat/Sun""), RC[-2]-TIME(8,0,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]=0, RC[-12]>=TIME(8,0,0)), RC[-2]-RC[-12], IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"",.......etc etc etc
I would prefer to make this look more like:
ActiveCell.FormulaR1C1 = _
"= IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(16,0,0), RC[-12]>TIME(10,30,0), RC[-2]>TIME(16,0,0)), (TIME(0,30,0))+(RC[-2]-TIME(16,0,0)), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(10,30,0), RC[-12]<=TIME(16,0,0), RC[-2]<=TIME(16,0,0)), TIME(0,15,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]>TIME(10,30,0)), (TIME(0,30,0))+(RC[-2]-TIME(10,30,0)), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(16,0,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-13]=""Sat/Sun""), RC[-2]-TIME(8,0,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]=0, RC[-12]>=TIME(8,0,0)), RC[-2]-RC[-12], _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]=0, RC[-12]<TIME(8,0,0)), (RC[-2]-RC[-12])+(TIME(8,0,0)-RC[-12]), p _<> ' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]>=1, RC[-12]<=TIME(18,0,0)), (TIME(18,0,0)-RC[-12])+(RC[-2]-TIME(8,0,0)), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]>=1, RC[-12]>TIME(18,0,0)), RC[-2]-TIME(8,0,0), """"))))))))))))"
...........etc etc etc
I'm only new to VBA and I find it so much easier to bash all these if's and and's into a formula than messing around with arrays etc.
I'm really hoping someone can help me and Excel VBA actually has this capablitiy.
Thanks
I have written a formula containing 21 IF statements with 51 AND statements within.
I'm running a loop to to apply this formula to each row in a defined area, my problem is VBA won't let me run the formula over multiple rows. I'm using the " _" to try and get it to read the formula as a single line and it ain't working.
I keep getting Syntax errors or expected end statement errors.
This is an example:
ActiveCell.FormulaR1C1 = "=IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(16,0,0), RC[-12]>TIME(10,30,0), RC[-2]>TIME(16,0,0)), (TIME(0,30,0))+(RC[-2]-TIME(16,0,0)), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(10,30,0), RC[-12]<=TIME(16,0,0), RC[-2]<=TIME(16,0,0)), TIME(0,15,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]>TIME(10,30,0)), (TIME(0,30,0))+(RC[-2]-TIME(10,30,0)), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(16,0,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-13]=""Sat/Sun""), RC[-2]-TIME(8,0,0), IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]=0, RC[-12]>=TIME(8,0,0)), RC[-2]-RC[-12], IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"",.......etc etc etc
I would prefer to make this look more like:
ActiveCell.FormulaR1C1 = _
"= IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(16,0,0), RC[-12]>TIME(10,30,0), RC[-2]>TIME(16,0,0)), (TIME(0,30,0))+(RC[-2]-TIME(16,0,0)), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(10,30,0), RC[-12]<=TIME(16,0,0), RC[-2]<=TIME(16,0,0)), TIME(0,15,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]<=TIME(10,30,0), RC[-2]>TIME(10,30,0)), (TIME(0,30,0))+(RC[-2]-TIME(10,30,0)), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Standard"", RC[-12]>TIME(16,0,0), RC[-2]<=TIME(10,30,0)), TIME(0,15,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-13]=""Sat/Sun""), RC[-2]-TIME(8,0,0), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]=0, RC[-12]>=TIME(8,0,0)), RC[-2]-RC[-12], _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]=0, RC[-12]<TIME(8,0,0)), (RC[-2]-RC[-12])+(TIME(8,0,0)-RC[-12]), p _<> ' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]>=1, RC[-12]<=TIME(18,0,0)), (TIME(18,0,0)-RC[-12])+(RC[-2]-TIME(8,0,0)), _
' IF(AND(RC[-15]=""Off-site"", RC[-14]=""Urgent"", RC[-1]>=1, RC[-12]>TIME(18,0,0)), RC[-2]-TIME(8,0,0), """"))))))))))))"
...........etc etc etc
I'm only new to VBA and I find it so much easier to bash all these if's and and's into a formula than messing around with arrays etc.
I'm really hoping someone can help me and Excel VBA actually has this capablitiy.
Thanks