2016-10-21 39 views
0

Hellow畢竟,低於原來的記錄語句的工作:運行時錯誤插入變量插入式聲明

Dim lastrow, prodflow As String 

lastrow = Range("C65536").End(xlUp).Row  'Determine last row where data ends 

Range("G2:G" & lastrow).Formula = "=VLOOKUP(LEFT(RC[-1],FIND(""-"",RC[-1],1)+4),'[Process Flows.xlsx]TABLE_X'!R1C1:R999C70,14,FALSE)" 

然而,當我試圖用一個變量prodflow更換TABLE_X,在我的公式語句出現運行時錯誤:

Dim lastrow, prodflow As String 

lastrow = Range("C65536").End(xlUp).Row  'Determine last row where data ends 
prodflow = Cells(2, 2)      'Read value in cell B2 and store as string 

Range("G2:G" & lastrow).Formula = "=VLOOKUP(LEFT(RC[-1],FIND(""-"",RC[-1],1)+4),'[Process Flows.xlsx]" & prodflow & "'!R1C1:R999C70,14,FALSE)" 

我在這個問題上搜索了一下,所有類似問題的答案都表明我所做的是正確的。還有什麼我錯過了嗎?謝謝!

+0

什麼是錯誤編號?如果將'.value'添加到'prodflow = Cells(2,2).value',該怎麼辦?如果在'prodflow = cells(....')之後添加'Debug.Print prodflow',您可以在** Immediate window **中找到什麼值? –

+0

額外的建議 - 嘗試將'.Formula'改爲'.FormulaR1C1' –

回答

0

嘗試生成式的字符串,然後將其分配到的範圍內的公式:

暗淡F = 「= A + B」

範圍。 FORMULA = F

從immidiate窗口中獲取f結果並在excel中檢查它...如果它工作,它應該在您的代碼中工作。