rabingupta
MIS
Hi Guys,
I would highly appreciate if you can help me to optimize my code.
My Problem:
Compare Col A (Total Rows = 35000) and Col B (1500) get all the values matching in Row A, where Col C = "Y". I have this code but its taking too long to run, Please help me optimize the code.
My Code:
Dim ForIndex As Integer
Dim ForIndex1 As Integer
Dim ForIndex3 As Integer
Dim ForIndex4 As Integer
Dim Counter As Integer
Sub Generate_Missing_Items()
Counter = 0
Application.ScreenUpdating = False
With Sheet3
Sh1LastRow = .Cells(Rows.Count, "F").End(xlUp).Row
Set Sh1Range = .Range("F1:F" & Sh1LastRow)
End With
With Sheet3
Sh2LastRow = .Cells(Rows.Count, "H").End(xlUp).Row
Set Sh2Range = .Range("H1:H" & Sh2LastRow)
End With
With Sheet3
Sh3LastRow = .Cells(Rows.Count, "I").End(xlUp).Row
Set Sh1Range = .Range("F1:F" & Sh3LastRow)
End With
For ForIndex1 = 2 To Sh2LastRow
For ForIndex = 2 To Sh1LastRow
If Sheet3.Range("H" & ForIndex1).Value = Sheet3.Range("F" & ForIndex).Value And UCase(Sheet3.Range("G" & ForIndex).Value) = "Y" Then
Counter = Counter + 1
Sheet5.Range("A" & Counter).Value = Sheet3.Range("F" & ForIndex).Value
Sheet5.Range("B" & Counter).Value = Sheet3.Range("G" & ForIndex).Value
End If
Next
Next
For ForIndex3 = 2 To Sh3LastRow
For ForIndex4 = 2 To Sh1LastRow
If Sheet3.Range("I" & ForIndex3).Value = Sheet3.Range("F" & ForIndex4).Value And UCase(Sheet3.Range("G" & ForIndex4).Value) = "Y" Then
Counter2 = Counter2 + 1
Sheet5.Range("C" & Counter2).Value = Sheet3.Range("F" & ForIndex).Value
Sheet5.Range("D" & Counter2).Value = Sheet3.Range("G" & ForIndex).Value
End If
Next
Next
Application.ScreenUpdating = True
Sheet3.Range("A" & 8).Value = Counter
End Sub
Thanks,
Rabin
I would highly appreciate if you can help me to optimize my code.
My Problem:
Compare Col A (Total Rows = 35000) and Col B (1500) get all the values matching in Row A, where Col C = "Y". I have this code but its taking too long to run, Please help me optimize the code.
My Code:
Dim ForIndex As Integer
Dim ForIndex1 As Integer
Dim ForIndex3 As Integer
Dim ForIndex4 As Integer
Dim Counter As Integer
Sub Generate_Missing_Items()
Counter = 0
Application.ScreenUpdating = False
With Sheet3
Sh1LastRow = .Cells(Rows.Count, "F").End(xlUp).Row
Set Sh1Range = .Range("F1:F" & Sh1LastRow)
End With
With Sheet3
Sh2LastRow = .Cells(Rows.Count, "H").End(xlUp).Row
Set Sh2Range = .Range("H1:H" & Sh2LastRow)
End With
With Sheet3
Sh3LastRow = .Cells(Rows.Count, "I").End(xlUp).Row
Set Sh1Range = .Range("F1:F" & Sh3LastRow)
End With
For ForIndex1 = 2 To Sh2LastRow
For ForIndex = 2 To Sh1LastRow
If Sheet3.Range("H" & ForIndex1).Value = Sheet3.Range("F" & ForIndex).Value And UCase(Sheet3.Range("G" & ForIndex).Value) = "Y" Then
Counter = Counter + 1
Sheet5.Range("A" & Counter).Value = Sheet3.Range("F" & ForIndex).Value
Sheet5.Range("B" & Counter).Value = Sheet3.Range("G" & ForIndex).Value
End If
Next
Next
For ForIndex3 = 2 To Sh3LastRow
For ForIndex4 = 2 To Sh1LastRow
If Sheet3.Range("I" & ForIndex3).Value = Sheet3.Range("F" & ForIndex4).Value And UCase(Sheet3.Range("G" & ForIndex4).Value) = "Y" Then
Counter2 = Counter2 + 1
Sheet5.Range("C" & Counter2).Value = Sheet3.Range("F" & ForIndex).Value
Sheet5.Range("D" & Counter2).Value = Sheet3.Range("G" & ForIndex).Value
End If
Next
Next
Application.ScreenUpdating = True
Sheet3.Range("A" & 8).Value = Counter
End Sub
Thanks,
Rabin