VLOOKUP适用于FormulaR1C1,但不是正则expression式?

我之前又发布了一个与这个问题很接近的问题,但实际上却是不同的。 我有这个VLOOKUP代码,从用户获取input文件以使用VLOOKUP。 它运行在我的一个macros,当我运行整个事情,但如果我自己运行私人小组,我得到第一个VLOOKUP行上的错误消息1004。 然后,我尝试更改代码以使用FormulaR1C1,并最终使用该版本正常工作。 为什么不使用我当前的代码工作,但它使用FormulaR1C1时工作?

Sub NEWTRY() ' ' Create_VLOOKUP_Using_Old_Kronos_Full_File Macro ' ' Dim iRet As Integer Dim strPrompt As String Dim strTitle As String ' Promt strPrompt = "Please select the last Kronos Full File before the dates of this HCM Report." & vbCrLf & _ "This will be used to find the Old Position, Org Unit, and Old Cost Center." & vbCrLf & _ "For example, if the date of this report is 7-28-17 thru 8-25-17, the closest Kronos Full File you would want to use is 7-27-17." ' Dialog's Title strTitle = "Last Kronos Full File for Old Positions" 'Display MessageBox iRet = MsgBox(strPrompt, vbOK, strTitle) Dim LR As Long Dim X As String Dim lNewBracketLocation As Long X = Application.GetOpenFilename( _ FileFilter:="Excel Files (*.xls*),*.xls*", _ Title:="Choose the Kronos Full File.", MultiSelect:=False) Dim wbk As Workbook Set wbk = Workbooks.Open(Filename:=X, ReadOnly:=True) Dim shtName As String shtName = wbk.Worksheets(1).name wbk.Close 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("T2").FormulaR1C1 = "=VLOOKUP(RC11,'" & X & "]'!R3C2:R9846C49,13,0)" ActiveWorkbook.ActiveSheet.Range("U2").Formula = "=VLOOKUP($E2,'" & X & "]'!$B$1:$AP$99999,41,0)" Range("V2").Formula = "=VLOOKUP($E2,'" & X & "]shtName'!$B$1:$AP$99999,18,0)" 

问题是我相信最后3行,或者是如何读X并把它放在那里。 VLOOKUPS的最后3行是错误的地方,除了R1C1的第一行实际工作。 我正在尝试与其他行的其他版本,但他们不工作。

我宁愿不使用R1C1,但它不想工作,除非我使用它。

那么,您正在试图在名称是所选path的最后一部分的工作表上进行查找?

在你的查找之前添加一行msgbox x ,这样你就可以确保x按照你的意图进行计算…对于我来说,它返回:

 c:\path\[filename.xlsm 

什么是x的例子? …粘贴的3个公式是:

 =VLOOKUP(RC11,'c:\path\[filename.xlsm]'!R3C2:R9846C49,13,0) =VLOOKUP($E2,'c:\path\[filename.xlsm]'!$B$1:$AP$99999,41,0) =VLOOKUP($E2,'c:\path\[filename.xlsm]shtName'!$B$1:$AP$99999,18,0)