1

我在 excel 中编写了一个简单的 'IF' 'OR' 公式,用于检查单元格值是否等于六字符产品代码;'IF' 测试简单地询问单元格 A5 = “[产品代码]”。如果是,则公式返回“是”,否则返回“否”。

我试图编写一个简单的宏来为我输入公式。我似乎收到了一个错误,其中统计信息:

“编译错误:语法错误”

我的公式稍微缩短的是:

=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

有什么线索吗?

4

1 回答 1

3

我同意查夫!与其编写如此庞大的公式,为什么不将该列表保存在单独的列中,然后检查相关单元格是否与该值匹配?

例如,假设我们维护一个列表(见下面Col ASheet2截图)

所以一个简单的公式B1Sheet1检查是否A1有这个词将完全解决你的问题。另一个好处是您可以在修改公式的情况下增加/减少您的列表Col ASheet2

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

上述的 VBA 代码将是

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

您现在可以修改上述公式/代码以满足您的需要:)

截屏

在此处输入图像描述

于 2013-01-14T05:13:37.497 回答