Probleme de fonction vlookup

mokiman -  
 mokiman -
Bonjour,

J'ai apparemment un probleme de vlookup dans ma macro, je ne sais pas si c'est un probleme de range ou autre mais je galere bien. Pourriez vous m'aider, m'eclairer, me sauver svp?

Voici la macro (en gros j'ouvre 2 fichier, reprendre les commentaires du 2eme fichier sur le premier, commentaires se trouvant sur la colonne 37 par rapport à un recherche sur la colonne 36...) :

Sub supervlooklook()
'

Dim Fichier1 As Variant
Dim Fichier2 As Variant
Dim lastcell As Integer
Dim x As String
Dim LT As String
Dim nb As Variant
Dim MyRange As Range

With Application
Fichier1 = .GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls")
End With
' Exit on Cancel

If Fichier1 = False Then
MsgBox "No file 1 was selected."
Exit Sub
End If

Application.ScreenUpdating = False
' --------------
Workbooks.Open Filename:=Fichier1
Fichier1 = ActiveWorkbook.Name
' --------------

With Application
Fichier2 = .GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls")
End With
' Exit on Cancel

If Fichier2 = False Then
MsgBox "No file 2 was selected."
Exit Sub
End If
'--------------------------

Workbooks.Open Filename:=Fichier2
Fichier2 = ActiveWorkbook.Name

lastcell = Workbooks(Fichier1).Sheets(1).Cells(2, 1).End(xlDown).Row

Workbooks(Fichier1).Sheets(1).Range("AK1").Value = "Comment"

For i = 2 To lastcell

LT = Workbooks(Fichier1).Sheets(1).Cells(i, 36).Value

On Error Resume Next

Set MyRange = Workbooks(Fichier2).Sheets(1).Range("AJ1:AK" & lastcell)

If Workbooks(Fichier1).Sheets(1).Cells(i, 37).Value = "" Then Workbooks(Fichier1).Sheets(1).Cells(i, 37).Value = Application.WorksheetFunction.VLookup(LT, MyRange, 2, False)

Next i

End Sub
A voir également:

1 réponse

mokiman
 
c'est bon probleme resolu , j'ai finalement utilisé une boucle For (trouvé sur un autre forum). Soulution bien plus simple finalement.

sub MaMacroAMoi()
dim cll as range
dim cll2 as range
For each cll in workbooks("toto" ).worksheets("tutu" ).range ("A1:A300" )
For each cll2 in workbooks("lolo" ).worksheets("lulu" ).range ("B5:B300" )
if cll.value = cll2.value then
workbooks("toto" ).worksheets("tutu" ).cells(cll.row,cll.column + 3).value =workbooks("lolo" ).worksheets("lulu" ).cells(cll2.row,cll2.column+1).value (la valeur dans la colonne à droite sera écrite colonne D dans le premier fichier)
end if
next
next
end sub

source : http://forum.ruemontgallet.com/ruemontgallet/Programmation/vb-vba/excel-recherchev-multicritere-sujet_22344_1.htm
0