I am trying to write an efficient and faster VBA code for a mismatched corresponding values, that will:
- Check each value of column C against A1:A9000
- If found: Copy values of column B and C and paste them against the found cell value (in column B and Column C) and also delete the old mismatched entry.
Running a for loop ends up doing 9000*9000 calculation , making is super slow. I am a beginner and do not know a faster way to do it. I know that .Find is a lot faster than using for loop.
Below is the sample mismatched data:
Column A | Column B | Column C |
---|---|---|
XYZ1 | Comments for XYZ1 | XYZ1 |
XYZ3 | Comments for XYZ2 | XYZ2 |
XYZ5 | ||
XYZ6 | Comments for XYZ4 | XYZ4 |
XYZ8 | Comments for XYZ5 | XYZ5 |
XYZ9 |
Notice that Values in Column B and Column C will always match and correspond correctly to each other. The mismatch is between A AND B & C.
Here's the desired result:
Column A | Column B | Column C |
---|---|---|
XYZ1 | Comments for XYZ1 | XYZ1 |
XYZ3 | ||
XYZ5 | Comments for XYZ5 | XYZ5 |
XYZ6 | ||
XYZ8 | ||
XYZ9 |
Notice that Column A cannot be altered or changed.
Here's what I have so far but it takes just way too long to process the code:
Sub Realign()
For i = 2 To 9000
Set Found = Sheets("Sheet1").Range("A:A").Find(What:=Worksheets("Sheet1").Cells(i, 3).Value, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Found Is Nothing Then
Worksheets("Sheet1").Cells(i, 2).Value = ""
Worksheets("Sheet1").Cells(i, 3).Value = ""
Else
Found.Offset(0, 1).Value = Worksheets("Sheet1").Cells(i, 2).Value
Found.Offset(0, 2).Value = Worksheets("Sheet1").Cells(i, 3).Value
End If
Next
Call Delete1
End Sub
Sub Delete1()
For i = 2 To 9000
If Not Worksheets("Sheet1").Cells(i, 3).Value = Worksheets("Sheet1").Cells(i, 1).Value Then
Worksheets("Sheet1").Cells(i, 2).Value = ""
Worksheets("Sheet1").Cells(i, 3).Value = ""
End If
Next
End Sub