我之前發佈了另一個與此問題接近的問題,但實際上它有所不同。我有這個VLOOKUP代碼,它接受來自用戶的輸入以獲取文件以使用VLOOKUP。它運行在我的一個宏中,當我運行整個事情時,但如果我自己運行private sub,我會在第一個VLOOKUP行上收到錯誤消息1004。然後我嘗試更改代碼以使用FormulaR1C1,並且最終使用該版本正常工作。爲什麼它不能使用我當前的代碼,但它在我使用FormulaR1C1時起作用?VLOOKUP適用於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,但它不想工作,除非我使用它。
嘗試把公式中的地方與R1C1樣式,然後用'.Formula',看看是什麼樣子的A1風格 – SeanC
我不知道你是什麼意思。有沒有簡單的方法來自動轉換它?在代碼的結尾處,第一個VLOOKUP行處於R1C1風格,並且其中一個實際工作。但是接下來會出現錯誤。我能不能手動做R1C1線並將其轉換爲A1? – Robillard
你有這個命令'Range(「T2」)。FormulaR1C1'。如果你在那一刻停止執行,你應該能夠看到它在A1風格中的樣子,如果你在範圍(「T2」)。這應該會幫助你改變你喜歡的風格的公式 – SeanC