使用If,Vlookup和Match语句但收到“编译错误预期:语句结束”

时间:2017-09-15 18:29:00

标签: excel vba if-statement compilation vlookup

我一直在研究以下代码,其中excel电子表格上有多个标签。它应该在另一个选项卡上执行Vlookup,其中一个表显示各种安全类型和字段,其中包含“是”或“否”。根据字段,它将执行BDP功能或返回“#N / A字段不适用”。

我尝试使用双引号,但在以下行之后仍然收到错误

Cells(r, c)"=If(VLOOKUP(EQUITY, 'Mandatory Field Control'!$A$1:$CA$7, MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=""Yes"", ""=BDP(RC1,R1C)"", ""#N/A Field Not Applicable"")""

我如何解决这个问题,或者我是否错过任何报价?

以下VBA:

r = 2
While Cells(r, "A") <> ""
    c = 2
    For c = 2 To 79
                    'Cells(r, c) = "=BDP(Cells(" & r & "," & c & "), Cells(1," & c & "))"
        If InStr(RC1, "EQUITY") <> 0 Then

            Cells(r, c)"=If(VLOOKUP(EQUITY, 'Mandatory Field Control'!$A$1:$CA$7, MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=""Yes"", ""=BDP(RC1,R1C)"", ""#N/A Field Not Applicable"")"

        ElseIf InStr(RC1, "GOVT") <> 0 Then

            Cells(r, c)"=If(VLOOKUP(GOVT, 'Mandatory Field Control'!$A$1:$CA$7, MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=Yes, ""=BDP(RC1,R1C)"", ""#N/A Field Not Applicable"")"

        ElseIf InStr(RC1, "CORP") <> 0 Then

            Cells(r, c)"=If(VLOOKUP(CORP, 'Mandatory Field Control'!$A$1:$CA$7, MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=Yes, ""=BDP(RC1,R1C)"", ""#N/A Field Not Applicable"")"

        ElseIf InStr(RC1, "INDEX") <> 0 Then

            Cells(r, c)"=If(VLOOKUP(INDEX, 'Mandatory Field Control'!$A$1:$CA$7, MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=Yes, ""=BDP(RC1,R1C)"", ""#N/A Field Not Applicable"")"

        ElseIf InStr(RC1, "COMDTY") <> 0 Then

            Cells(r, c)"=If(VLOOKUP(COMDTY, 'Mandatory Field Control'!$A$1:$CA$7, MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=Yes, ""=BDP(RC1,R1C)"", ""#N/A Field Not Applicable"")"

        ElseIf InStr(RC1, "MTGE") <> 0 Then

            Cells(r, c)"=If(VLOOKUP(MTGE, 'Mandatory Field Control'!$A$1:$CA$7, MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=Yes, ""=BDP(RC1,R1C)"", ""#N/A Field Not Applicable"")"

结束如果

    Next c
    r = r + 1
Wend

1 个答案:

答案 0 :(得分:1)

您应该使用:

Cells(r,c).Formula = "=Formula Here"   

编辑:您还必须在

中的Yes周围缺少双引号
MATCH(B4,'Mandatory Field Control'!$B$1:$CA$1), FALSE)=Yes

在所有行中但第一行。 正如Jeeped所说,您的Match函数应该更新为包含01 / -1,具体取决于您希望它返回的内容。

出于个人喜好,我还会使用Select Case代替If Then来提高可读性:

Select Case True
    Case(RC1 Like "*EQUITY*") 'I assume RC1 is a variable
        Cells(r,c).Formula = "=Formula here"
    Case(RC1 Like "*GOVT*")
        Cells(r,c).Formula = "=Formula here"
        ...
End Select