2017-09-15 73 views
0

我一直在研究以下代码,其中Excel电子表格有多个选项卡。它应该在另一个选项卡上执行Vlookup,并在表中显示具有“是”或“否”的各种安全类型和字段。根据不同的字段,它将执行BDP功能或返回“#N/A字段不适用”。使用If,Vlookup和Match语句,但收到“预期编译错误:语句结束”

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

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

它应该是'细胞(R,C).Formula = “= IF ...”' –

+0

它也应该是'MATCH(B4,'必填字段控制'!$ A $ 1:$ CA $ 1,0)' – Jeeped

回答

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 
+0

我没有具体说明,但我对MATCH的评论还包括从$ B $ 1:$ CA $ '到'$ A $ 1:$ CA $'。当然,除非OP有意要抵消1列。 – Jeeped

+0

我按照建议进行了更改,并且不再显示错误。我实际上遇到了执行VBA的麻烦。当我运行它时,没有输出(即BDP功能或N/A字段不适用,不在Excel中) RC1意在指单元格,当前当r = 2时。 C = 2。在这种情况下RC1应该是B1。我不确定我是否正确引用它? –