Je reçois cette erreur 1004 en essayant d'utiliser ce vlookup. J'utilise une fenêtre pour sélectionner un fichier puis ce fichier est utilisé dans le vlookup. Je le fais dans une autre macro que j'ai et j'ai utilisé essentiellement le même code. Mais pour une raison quelconque, celui-ci ne fonctionne pas. Quelqu'un peut-il voir des problèmes flagrants? Je ne peux pas comprendre ce que je fais mal.Erreur 1004 Erreur définie par l'application ou définie par l'objet à l'aide de vlookup?
je reçois l'erreur sur la Première formule RECHERCHEV juste après la « Avec ws »
Dim iRet As Integer
Dim strPrompt As String
Dim strTitle As String
Dim shtName As String
' Prompt
strPrompt = "Please select the last Kronos Full File before the dates of this Report." & vbCrLf & _
"For example, if the date of this report is 9-8-17, you would want to use the closest date Kronos Full File." & vbCrLf & _
"If one was not ran in the past couple days, then run a new Kronos Full File, and then choose that file."
' Dialog's Title
strTitle = "Latest Kronos Full File"
'Display MessageBox
iRet = MsgBox(strPrompt, vbOK, strTitle)
Dim Window2 As String
Dim X As String
Dim lNewBracketLocation As Long
Dim wb2 As Workbook
Window2 = Application.GetOpenFilename(_
FileFilter:="Excel Files (*.xls*),*.xls*", _
Title:="Choose the Newest Kronos Full File", MultiSelect:=False)
Set wb2 = Workbooks.Open(Filename:=Window2, ReadOnly:=True)
shtName = wb2.Worksheets(1).name
wb2.Close
MsgBox "You selected " & Window2
'Find the last instance in the string of the path separator "\"
lNewBracketLocation = InStrRev(Window2, Application.PathSeparator)
'Edit the string to suit the VLOOKUP formula - insert "["
X = Left$(Window2, lNewBracketLocation) & "[" & Right$(Window2, Len(Window2) - lNewBracketLocation)
With ws
.Range("M2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$E$99999,4,0)"
.Range("N2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$C$99999,2,0)"
.Range("O2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$U$99999,20,0)"
.Range("P2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$Q$99999,16,0)"
.Range("Q2").Formula = "=VLOOKUP($K2,'" & X & "]shtName'!$B$2:$S$99999,18,0)"
End With
Êtes-vous sûr que x = ce que vous pensez qu'il devrait être? –
Bonne question Je suppose que c'est probablement ce que c'est, mais je l'ai copié à partir de mon autre code et ceux qui fonctionnent. Je vais y jeter un autre coup d'oeil. – Robillard
Vous n'avez pas besoin de '.Range (" M2 "). Formula =" = VLOOKUP ($ K2, '"& X &"] "& shtName &"'! $ B $ 2: $ E $ 99999,4,0) "' comme 'shtName' est une variable? – SJR