2017-09-15 75 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。我不確定我是否正確引用它? –