How to loop faster?

BRB1983

Board Regular
Joined
Aug 29, 2019
Messages
61
i have a code when on cell change then loop and fine matching value. then offset and paste in cell to left.
code works fine but need it to work faster. Any help would be appreciated.
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Sheet1.Range("C2:C50")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then
      
Dim i As Long
Dim j As Long
Dim lastrow As Long
Dim ws As Worksheet, ws2 As Worksheet
    Set ws = Sheet1
    Set ws2 = Sheet2
    For i = 2 To 5000
        If IsEmpty(ws.Range("C" & i)) Then
            Exit For
        End If
    For j = 2 To 5000
        If IsEmpty(ws2.Range("E" & j)) Then
            Exit For
        End If
If ws.Range("C" & i).Text = ws2.Range("E" & j).Text Then
    If ws.Range("B" & i).Value = "" Then
        ws.Range("B" & i).Value = ws2.Range("E" & j).Value & "-" & ws2.Range("B" & j).Value
    Exit For
End If
    End If
    Next j
    Next i
End If

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I noticed that you did not close the loops with a next. Also I think one loop would be enough to do both functions
VBA Code:
For i = 2 To 5000
        If IsEmpty(ws.Range("C" & i)) Then
            Exit For
        End If
        If IsEmpty(ws2.Range("E" & i)) Then
            Exit For
        End If
Next i

Unless I am missing something, this should help.
 
Upvote 0
try using variant arrays instead of referencing the worksheet multiple times in a loop.
this code in untested but should show you how to do it ans will be much faster:
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Sheet1.Range("C2:C50")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
    Is Nothing Then
     
Dim i As Long
Dim j As Long
Dim lastrow As Long
Dim ws As Worksheet, ws2 As Worksheet
    Set ws = Sheet1
    Set ws2 = Sheet2
    wsr = ws.Range(Cells(1, 3), Cells(5000, 3))
    wsr2 = ws.Range(Cells(1, 5), Cells(5000, 5))
   
    For i = 2 To 5000
        If (wsr(i, 1))="" Then
            Exit For
        End If
    For j = 2 To 5000
        If (wsr2(j, 1))="" Then
            Exit For
        End If
If wsr(i, 1) = wsr2(i, 1) Then
    If ws.Range("B" & i).Value = "" Then
        ws.Range("B" & i).Value = wsr(j, 1) & "-" & wsr2(j, 1)
    Exit For
End If
    End If
    Next j
    Next i
End If

End Sub
 
Upvote 0
They need to be declared as Variant
Also why are you looping through 5000 cells on both sheets, when the code will only be triggered if you change C2:C50?
 
Upvote 0
They need to be declared as Variant
Also why are you looping through 5000 cells on both sheets, when the code will only be triggered if you change C2:C50?
sheet 2 needs to be at 5000 but sheet 1 will not exceed 50
 
Upvote 0
In that case for the first loop you can change this For i = 2 To 5000 to For i = 2 To 50 for both codes, which will speed it up
 
Upvote 0
I didn't provide it, that was offthelip
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top