Syntax Error for formula

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
171
Office Version
  1. 365
Hi ,

I am recording my macro to input a formula , it is amending the original formula to the below but when I try and run it , it creates a syntax error.

Anyone any ideas what is causing this issue.

Thanks,

ActiveCell.FormulaR1C1 = _
"=IF(AND(ISNUMBER(SEARCH(""Mr"",RC[1]))=TRUE,ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))=TRUE),""Staff ID"",IF(AND(ISNUMBER(SEARCH(""Ms"",RC[1]))=TRUE,ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))=TRUE),""Staff ID"",IF(AND(ISNUMBER(SEARCH(""Miss"",RC[1]))=TRUE,ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))=TRUE),""Staff ID"",IF(AND(ISNUMBER(SEARCH(""Mrs"",RC[1]))=TRUE,ISNUMBER(SEARCH(""IENDD""& _
"1]))=TRUE),""Staff ID"",IF(AND(ISNUMBER(SEARCH(""/"",RC[1]))=TRUE,ISNUMBER(SEARCH(""IENDD"",R[1]C[1]))=TRUE),""Date"",""n/a"")))))"
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
if you use F9 button.it can help you to evaluate the selecting stretch by this way You can figure out the Syntax Error
 
Upvote 0
There are actually two syntax errors: a VBA syntax error and an Excel syntax error. The latter would cause a runtime error.

The syntax corrections are in red below. I also cleaned up the recorded macro code, removing unnecessary elements. That is prudent to do; recorded macros often have extraneous code. Finally, I broke up the strings differently in order to improve reability.

Rich (BB code):
ActiveCell.FormulaR1C1 = _
 "=IF(AND(ISNUMBER(SEARCH(""Mr"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""Ms"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""Miss"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""Mrs"",RC[1])),ISNUMBER(SEARCH(""IENDD""" & _
",R[2]C[1))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""/"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[1]C[1]))),""Date"",""n/a"")))))"

The above is formatted in order to highlight the VBA syntax error (missing close-quote in the 5th line).

The following is how I would actual format it:

Rich (BB code):
ActiveCell.FormulaR1C1 = _
"=IF(AND(ISNUMBER(SEARCH(""Mr"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""Ms"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""Miss"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""Mrs"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))),""Staff ID""," & _
"IF(AND(ISNUMBER(SEARCH(""/"",RC[1])),ISNUMBER(SEARCH(""IENDD"",R[1]C[1]))),""Date"",""n/a"")))))"

The Excel IF expression could be rewritten more efficiently. ISNUMBER(SEARCH("IENDD",R[2]C[1])) needs to be done only once. But that's another story altogether.
 
Upvote 0
PS....
The Excel IF expression could be rewritten more efficiently. ISNUMBER(SEARCH("IENDD",R[2]C[1])) needs to be done only once. But that's another story altogether.

Okay, I'll share....

Code:
ActiveCell.FormulaR1C1 = _
"=IF(ISNUMBER(SEARCH(""IENDD"",R[2]C[1]))=FALSE,""n/a""," & _
"IF(ISNUMBER(SEARCH(""Mr"",RC[1])),""Staff ID""," & _
"IF(ISNUMBER(SEARCH(""Ms"",RC[1])),""Staff ID""," & _
"IF(ISNUMBER(SEARCH(""Miss"",RC[1])),""Staff ID""," & _
"IF(ISNUMBER(SEARCH(""Mrs"",RC[1])),""Staff ID""," & _
"IF(ISNUMBER(SEARCH(""/"",RC[1])),""Date"",""n/a""))))))"
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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