我在excel中編寫了一個簡單的'IF''OR'公式,它檢查單元格值是否等於六個字符的產品代碼; 'IF'測試簡單地要求單元格A5 =「[產品代碼]」。如果是,則公式返回'是',如果不是'否'。Excel VBA - 'IF''OR'公式導致編譯錯誤:語法錯誤
我試圖寫一個簡單的宏來爲我輸入公式。我似乎得到其統計的錯誤:略短
"Compile error: Syntax error"
我的公式是:
=IF(OR($A5="OIASLS",$A5="BMA003",$A5="CBRNSR",$A5="DTAPCO", [I have shorted the formula from here].................Yes","No").
我的VBA代碼:
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
任何線索?
以一個快速瀏覽一下OR語句,它看起來好像你的條件有點過在連續換行符。例如,OR的第一行以字母「R」結尾,下一行以「GPH」開頭。我很好奇你爲什麼不在產品列表範圍上使用vlookup,這會更自然地做到這一點?如果產品列表發生變化,這也會使其更易於維護。 – chuff
+ 1 Chuff好主意。 –