简体   繁体   中英

Vlookup function returning same results for different row cells

I am trying to get this result:

预期结果

Currently my vbc script gives me this:

当前 vba 代码生成的结果

This is the first sheet where the vlookup is made from

用于 vlookup 的 Sheet1

Sub Suivi()

With ActiveSheet
.Unprotect Password:="capreit"
Rows("2:" & Rows.Count).ClearContents
Worksheets("Suivi Déménagement").Cells.Borders.LineStyle = xlNone

Dim i As Integer


j = 2
For i = 4 To Sheets("Tableau").Range("A4").SpecialCells(xlLastCell).Row

    If Sheets("Tableau").Cells(i, 2).Value = "Nouveau locataire" Or Sheets("Tableau").Cells(i, 2).Value = "Décès" Or Sheets("Tableau").Cells(i, 2).Value = "Skip" And Evaluate("OR(ISNUMBER(MATCH({""*-*""},{""" & Sheets("Tableau").Cells(i, 1).Value & """},0)))") Then

        Sheets("Suivi Déménagement").Cells(j, 1) = Sheets("Tableau").Cells(i, 1)
        Sheets("Suivi Déménagement").Cells(j, 2).Formula = "=IFERROR(VLookup($A2,Tableau!$A:$T,4,FALSE),"""")"
        Sheets("Suivi Déménagement").Cells(j, 3).Formula = "=IFERROR(VLookup($A2,Tableau!$A:$T,3,FALSE),"""")"
        Sheets("Suivi Déménagement").Cells(j, 4).Formula = "=IFERROR(VLookup($A2,Tableau!$A:$T,2,FALSE),"""")"
        Sheets("Suivi Déménagement").Cells(j, 5).Formula = "=IFERROR(VLookup($A2,Tableau!$A:$T,18,FALSE),"""")"
        Sheets("Suivi Déménagement").Cells(j, 6).Formula = "=IFERROR(VLookup($A2,Tableau!$A:$T,19,FALSE),"""")"

  j = j + 1

      End If

        Next i

You're using the same vlookup with the same value and the same criteria but expecting different results.

I believe your specific problem is with "$A2". This should be dynamic. The next row should be $A3, then $A4, and so on.

You can achieve this by treating your vlookup as a string. Break it after the $A to use your variable then rejoin the string.

For example: "...RROR(VLookup($A" & j & ", Tabl...."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM