Nickerzzzzz
New Member
- Joined
- Oct 26, 2017
- Messages
- 11
Hi All,
Hoping someone can point me in the right direction with regards to optimizing some vba.
The below code takes 12 seconds to run against 3000 cells selected in each worksheet, it looks at one column in each worksheet, looks for a duplicate value and highlights the cell accordingly, the problem is I have 400,000 cells in the first worksheet and 800,000 cells in the second worksheet and it takes an age to run, is there a way to optimise the code so it runs faster?
My vba is limited as I'm pretty much starting out and I have adapted the code found on the forums to suit...from what I can gather I need to read All the cell values into memory/an array, identify the duplicates within the array, and then write the array back to the sheet (Master), trouble is I don't have a clue how to do this and cant find any examples which guide me sufficiently
Any help or pointers would be much appreciated...
********************
Sub CompareRanges()
Dim Count As Integer
Dim StartTime As Double
Dim MinutesElapsed As String
Dim WorkRng1 As Range, WorkRng2 As Range, Rng1 As Range, Rng2 As Range
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
xTitleId = "Find Duplicate String"
Set WorkRng1 = Application.InputBox("Master Range (smallest)", xTitleId, "", Type:=8)
Set WorkRng2 = Application.InputBox("Range To Compare:", xTitleId, Type:=8)
Application.ScreenUpdating = False
StartTime = Timer
For Each Rng1 In WorkRng1
rng1Value = Rng1.Value
For Each Rng2 In WorkRng2
If rng1Value = Rng2.Value Then
Rng1.Interior.Color = VBA.RGB(255, 0, 0)
Exit For
End If
Next
Next
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "The Code Took " & MinutesElapsed & " (hh:mm:ss) To Run", vbInformation
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
********************
Hoping someone can point me in the right direction with regards to optimizing some vba.
The below code takes 12 seconds to run against 3000 cells selected in each worksheet, it looks at one column in each worksheet, looks for a duplicate value and highlights the cell accordingly, the problem is I have 400,000 cells in the first worksheet and 800,000 cells in the second worksheet and it takes an age to run, is there a way to optimise the code so it runs faster?
My vba is limited as I'm pretty much starting out and I have adapted the code found on the forums to suit...from what I can gather I need to read All the cell values into memory/an array, identify the duplicates within the array, and then write the array back to the sheet (Master), trouble is I don't have a clue how to do this and cant find any examples which guide me sufficiently
Any help or pointers would be much appreciated...
********************
Sub CompareRanges()
Dim Count As Integer
Dim StartTime As Double
Dim MinutesElapsed As String
Dim WorkRng1 As Range, WorkRng2 As Range, Rng1 As Range, Rng2 As Range
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
xTitleId = "Find Duplicate String"
Set WorkRng1 = Application.InputBox("Master Range (smallest)", xTitleId, "", Type:=8)
Set WorkRng2 = Application.InputBox("Range To Compare:", xTitleId, Type:=8)
Application.ScreenUpdating = False
StartTime = Timer
For Each Rng1 In WorkRng1
rng1Value = Rng1.Value
For Each Rng2 In WorkRng2
If rng1Value = Rng2.Value Then
Rng1.Interior.Color = VBA.RGB(255, 0, 0)
Exit For
End If
Next
Next
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "The Code Took " & MinutesElapsed & " (hh:mm:ss) To Run", vbInformation
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
********************