VLOOKUP 适用于 FormulaR1C1,但不适用于常规公式



我之前发布了另一个与这个问题接近的问题,但实际上是不同的。我有这个 VLOOKUP 代码,它从用户那里获取输入以获取要使用 VLOOKUP 的文件。当我运行整个事情时,它在我的一个宏中工作,但是如果我自己运行私有子,我会在第一行 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,但除非我使用它,否则它不想工作。

因此,您正在尝试对名称为所选路径最后一部分的工作表进行查找?

在查找之前添加一行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)

最新更新