Home » excel » Excel VBA – 'IF' 'OR' formula causing Compile error: Syntax error

Excel VBA – 'IF' 'OR' formula causing Compile error: Syntax error

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have written a simple ‘IF’ ‘OR’ formula in excel which checks if cells value is equal to a six character product code; the ‘IF’ test simple asks does cell A5 = “[product code]”. If it does the formula returns ‘Yes’, if not ‘No’.

I have tried to write a simple macro to input the formula for me. I seem to be getting an error which stats:

“Compile error: Syntax error”

My formula shorted slightly is:

=IF(OR($A5="OIASLS",$A5="BMA003",$A5="CBRNSR",$A5="DTAPCO", [I have shorted the formula from here].................Yes","No").

My VBA code is:

Sub LevelTwoCourse()
'Formula to test if Product is a level 2 product. If Product code matches any code listed below formula returns "yes"
    Range("AF5").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(OR(RC1=""OIASLS"",RC1=""BMA003"",RC1=""CBRNSR"",RC1=""DTAPCO"",RC1=""DTAPMC"",RC1=""DTAPMD"",RC1=""DTBFDR"",RC1=""DTBFFL"",RC1=""DTBLAS"",RC1=""DTDVFL"",RC1=""DTDVFR"",RC1=""DTDVVF"",RC1=""DTHLDR"",RC1=""DTHVCR"",RC1=""DTHVDR"",RC1=""DTHVHO"",RC1=""DTHVHR"",RC1=""DTLG3D"",RC1=""DTLG7D"",RC1=""DTLGAF"",RC1=""DTLGAS"",RC1=""DTLGEF"",RC1=""DTLGEX"",RC1=""DTLGFR"",R"& _
        "GHP"",RC1=""DTLGMC"",RC1=""DTMPOP"",RC1=""DTMPRF"",RC1=""DTOSMD"",RC1=""DTOTLD"",RC1=""DTRSDE"",RC1=""DTRSHP"",RC1=""DTRSRA"",RC1=""DTRSRC"",RC1=""DTRSRE"",RC1=""DTRSSP"",RC1=""DTRSSS"",RC1=""DTRSST"",RC1=""DTRSVH"",RC1=""DTRSYT"",RC1=""DTSCS1"",RC1=""DTTLMC"",RC1=""DTTLMD"",RC1=""FCFSHE"",RC1=""FCFSHS"",RC1=""FSCHVR"",RC1=""FSRU08"",RC1=""HRDRPM"",RC1=""IMASCM"",RC"& _
        "M"",RC1=""IMTHCM"",RC1=""IMTHWM"",RC1=""INPDMR"",RC1=""OBATTP"",RC1=""OIBACF"",RC1=""OIBAE2"",RC1=""OIBAEC"",RC1=""OIBAER"",RC1=""OIBAEY"",RC1=""OIBAM3"",RC1=""OIBAMT"",RC1=""OIBRFC"",RC1=""OIBRFR"",RC1=""OIFA4N"",RC1=""OIFBTF"",RC1=""OIHMSC"",RC1=""OIIE3D"",RC1=""OIIE5D"",RC1=""OIIEDA"",RC1=""OILOL2"",RC1=""OILOMA"",RC1=""OIRTOD"",RC1=""OISKFM"",RC1=""OISKFN"",RC1="& _
        "",RC1=""OISKIA"",RC1=""OISKIB"",RC1=""OISKPB"",RC1=""OISKPR"",RC1=""OISKSW"",RC1=""OISKTR"",RC1=""OISKWR"",RC1=""OISPLR"",RC1=""OISRTR"",RC1=""OIUKRO"",RC1=""OIWSBA"",RC1=""OIWSBB"",RC1=""OIWSBC"",RC1=""OIWSBD"",RC1=""OIWSBE"",RC1=""OIWSBF"",RC1=""OIWSBG"",RC1=""DTEXE1"",RC1=""FSRWMM"",RC1=""OIAPPP"",RC1=""OIAPPR"",RC1=""OIASLR"",RC1=""BMA001"",RC1=""BMA002"",RC1="""& _
        "RC1=""CBRNB2"",RC1=""CBRNBR"",RC1=""CBRNGI"",RC1=""CBRNGR"",RC1=""CBRNLO"",RC1=""CBRNSI"",RC1=""DTAPCR"",RC1=""DTAWDM"",RC1=""DTBFFR"",RC1=""DTBFIR"",RC1=""DTDVCB"",RC1=""DTDVCR"",RC1=""DTLGBR"",RC1=""DTOTHD"",RC1=""DTRSCE"",RC1=""DTRSCP"",RC1=""DTUSD1"",RC1=""FCFSDT"",RC1=""FCFSHR"",RC1=""FCSKRT"",RC1=""FSENG1"",RC1=""FSENG2"",RC1=""FSENG3"",RC1=""FSENG4"",RC1=""FS"& _
        "1=""FSRU02"",RC1=""FSRU03"",RC1=""FSRU04"",RC1=""FSRU05"",RC1=""FSRU06"",RC1=""FSRU07"",RC1=""FSRU09"",RC1=""FSRU10"",RC1=""FSRU11"",RC1=""FSRU12"",RC1=""FSRU15"",RC1=""FSRU16"",RC1=""FSRU17"",RC1=""FSRU18"",RC1=""FSRV01"",RC1=""FSRV02"",RC1=""FSRV03"",RC1=""FSRV04"",RC1=""FSRV05"",RC1=""FSRV06"",RC1=""FSRV07"",RC1=""FSRV08"",RC1=""FSRV09"",RC1=""FSRV10"",RC1=""FSRV"& _
        """FSRV12"",RC1=""FSRV15"",RC1=""FSRV16"",RC1=""FSRV17"",RC1=""FSRV18"",RC1=""HSSKPT"",RC1=""HSSLFT"",RC1=""INPLRF"",RC1=""OBATTW"",RC1=""OIBABI"",RC1=""OIBAR2"",RC1=""OIBASA"",RC1=""OIFA2N"",RC1=""OIFBCE"",RC1=""OIFBTE"",RC1=""OIFBTI"",RC1=""OIFHCO"",RC1=""OIFSUR"",RC1=""OIHCHM"",RC1=""OIHRPS"",RC1=""OIHSTD"",RC1=""OILOHC"",RC1=""OIRTIN"",RC1=""OISKEX"",RC1=""OISKHA"& _
        "OISKHF"",RC1=""OISKII"",RC1=""OISKMT"",RC1=""OISKSS"",RC1=""OISKUH"",RC1=""OISLEI"",RC1=""OIUKEC"",RC1=""OIUSCH"",RC1=""OIUSCR"",RC1=""OIUSLA""),""Yes"",""No"")"
End Sub

Any clues?

How to&Answers:

I agree with Chuff! Instead of writing such a huge formula, why not keep that list in a separate column and then check if the relevant cell matches that value?

For example, let’s say we maintain a list in Col A of Sheet2 (See Screenshot below)

So a simple formula in say B1 of Sheet1 to check if A1 has that word or not will exactly solve your problem. The other benefit is that you can increase/reduce your list in Col A of Sheet2 without amending the formula

=IF(COUNTIF(Sheet2!A:A,Sheet1!A1)>0,"Yes","No")

The VBA code for the above will be

Range("B1").Formula = "=IF(COUNTIF(Sheet2!A:A,Sheet1!A1)>0,""Yes"",""No"")"

You may now amend the above formula/code to suit your needs 🙂

ScreenShot

enter image description here