在VLOOKUP问题中使用文件位置

我从这里的另一个答案得到了这个代码,它比他做的更好,因为你实际上可以select你从哪里拉你的文件。 但是好像我无法在VLOOKUP中得到完全正确的文件名? 在VLOOKUP之后,我收到错误1004。 也许还有其他的错误。 我复制这个代码,然后取代我需要的,但我需要另一双眼睛。 提前致谢。

Dim x As String Dim lNewBracketLocation As Long x = Application.GetOpenFilename( _ FileFilter:="Excel Files (*.xls*),*.xls*", _ Title:="Choose previous quarter's file", MultiSelect:=False) MsgBox "You selected " & x 'Find the last instance in the string of the path separator "\" lNewBracketLocation = InStrRev(x, Application.PathSeparator) 'Edit the string to suit the VLOOKUP formula - insert "[" x = Left$(x, lNewBracketLocation) & "[" & Right$(x, Len(x) - lNewBracketLocation) Range("V2").Select ActiveCell.FormulaR1C1 = "=VLOOKUP($E2,'" & x & "]file_2017072732'!$B$5:$AP$9486,18,FALSE)" ' ERROR 1004 Selection.AutoFill Destination:=Range("V2:V177") Range("V2:V177").Select 

当我到达这一点时,它显示x等于"C:\Name\Name\Name\[Filename.xlsx"

这是它应该是什么格式?

问题不在于x的值,它看起来像是一个有效的格式。

问题在于将使用A1表示法编写的公式分配给使用其FormulaR1C1属性的单元格。

更改

 ActiveCell.FormulaR1C1 = "=VLOOKUP($E2,'" & x & "]file_2017072732'!$B$5:$AP$9486,18,FALSE)" 

要么

 ActiveCell.Formula = "=VLOOKUP($E2,'" & x & "]file_2017072732'!$B$5:$AP$9486,18,FALSE)" 

要么

 ActiveCell.FormulaR1C1 = "=VLOOKUP(RC5,'" & x & "]file_2017072732'!R5C2:R9486C42,18,FALSE)" 

它应该是可以的。