urahulnair
New Member
- Joined
- Jul 28, 2014
- Messages
- 4
I used this macro to copy contents from one excel sheet to another, by comparing two columns and finding a matching cell. The problem is that this macro is taking a long time(close to three days) to complete. Can someone please help me to make things faster.(There are close to 4,00,000 records in both the sheets to compare against.
Code:
Option Explicit
Sub MatchAndCopy()
Dim sheet01 As Worksheet, sheet02 As Worksheet
Dim count As Range, matchingCell As Long
Dim RangeInSheet1 As Variant
Dim RangeInSheet2 As Variant
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
Set sheet01 = Worksheets("Sheet1")
Set sheet02 = Worksheets("Sheet2")
Set RangeInSheet1 = sheet01.Columns(1)
Set RangeInSheet2 = sheet02.Range("A2", sheet02.Range("A" & Rows.count).End(xlUp))
For Each count In RangeInSheet2
matchingCell = 0
On Error Resume Next
matchingCell = Application.Match(count, RangeInSheet1, 0)
On Error GoTo 0
If matchingCell <> 0 Then
Application.StatusBar = "Please wait while data is being copied, Processing count : " & count
sheet01.Range("F" & matchingCell).Value = count.Offset(, 1)
sheet01.Range("G" & matchingCell).Value = count.Offset(, 2)
sheet01.Range("H" & matchingCell).Value = count.Offset(, 3)
sheet01.Range("I" & matchingCell).Value = count.Offset(, 4)
sheet01.Range("J" & matchingCell).Value = count.Offset(, 5)
End If
Next count
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub