2016-10-21 44 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中检查它...如果它工作,它应该在您的代码中工作。