0

I have to compare 2 columns in 2 different sheets then add the missing cells in the second sheet. But the problem is that i used a loop and it takes too much time. Do someone have an idea of how to compare these 2 columns without using a loop.

    Set col_1 = Worksheets("Plan Traitement Risque").Range("B6:B700")

      With ThisWorkbook.Sheets("Analyse de risque")
        For i = 200 To 6 Step -1
          'Test si valeur cellule feuil1!=Ax est dans Plage col_2(feuil2!A1:A50))
        If Application.CountIf(col_1, .Range("B" & i).Value) = 0 Then
            'Delete ligne i feuil1
            .Rows(i).Copy
             Sheets("Plan Traitement Risque").Rows(i).Insert (xlShiftDown)
End If
    Next i
  End With
1
  • 1
    I avoid loops whenever I can, but you need it for comparing values. In your case the main problem is inserting rows, and CountIf(). You can improve the speed significantly if you copy both ranges to array, and write the second one back only once (it would helpful if you include the full function) Commented Sep 9, 2015 at 12:45

1 Answer 1

1

Make a vertical lookup in excel first and then search for NAs in the loop. It will be much faster. You can delete the lookup after your work is done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.