如何将文件名插入 VLOOKUP 函数
How can I insert a file name into a VLOOKUP function
我几乎完成了这个宏,它允许用户 select 一个文件路径,然后它会从另一个 selected 文件中执行 vlookup
文件。到目前为止,我的代码如下所示:
Sub SelectFile()
Dim x As String
Dim lNewBracketLocation As Long
x = Application.GetOpenFilename()
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("AA2").FormulaR1C1 = "=VLOOKUP(RC[-1]C23&R[-1]C24,'" & x & "]Access'!C3:C27,6,0)"
文件 selected 后,VLOOKUP 需要如下所示:
=VLOOKUP($W2&$X2,'S:\Finance Forecast\Inventory Folders15\LW[08-AUG-Access Export.xlsx]Access'!$C:$AA,6,FALSE)
当我 运行 宏时,我不断收到 运行 时间错误“1004”:VLOOKUP 公式行上的应用程序定义或对象定义错误
我是 VBA 的新手,如有任何帮助,我们将不胜感激。
此致,
卢克
公式中的 C 太多了。应该是:
Range("AA2").FormulaR1C1 = "=VLOOKUP(R[-1]C23&R[-1]C24,'" & x & "]Access'!C3:C27,6,0)"
而不是
Range("AA2").FormulaR1C1 = "=VLOOKUP(RC[-1]C23&R[-1]C24,'" & x & "]Access'!C3:C27,6,0)"
我几乎完成了这个宏,它允许用户 select 一个文件路径,然后它会从另一个 selected 文件中执行 vlookup
文件。到目前为止,我的代码如下所示:
Sub SelectFile()
Dim x As String
Dim lNewBracketLocation As Long
x = Application.GetOpenFilename()
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("AA2").FormulaR1C1 = "=VLOOKUP(RC[-1]C23&R[-1]C24,'" & x & "]Access'!C3:C27,6,0)"
文件 selected 后,VLOOKUP 需要如下所示:
=VLOOKUP($W2&$X2,'S:\Finance Forecast\Inventory Folders15\LW[08-AUG-Access Export.xlsx]Access'!$C:$AA,6,FALSE)
当我 运行 宏时,我不断收到 运行 时间错误“1004”:VLOOKUP 公式行上的应用程序定义或对象定义错误 我是 VBA 的新手,如有任何帮助,我们将不胜感激。
此致, 卢克
公式中的 C 太多了。应该是:
Range("AA2").FormulaR1C1 = "=VLOOKUP(R[-1]C23&R[-1]C24,'" & x & "]Access'!C3:C27,6,0)"
而不是
Range("AA2").FormulaR1C1 = "=VLOOKUP(RC[-1]C23&R[-1]C24,'" & x & "]Access'!C3:C27,6,0)"