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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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
i'm not getting any results with this example.
 
Upvote 0
Are you changing 1 cell at a time, or pasting into a number of cells?
 
Upvote 0

Attachments

  • sheet1.png
    sheet1.png
    26.4 KB · Views: 8
Upvote 0
OK, untested, but try
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
    Dim fnd As Range
    
    If Target.CountLarge > 1 Then Exit Sub
    If Not Application.Intersect(Target, Range("C2:C50")) Is Nothing Then
        If Target = "" And Target.Offset(, -1) <> "" Then Exit Sub
            With Sheet2
                Set fnd = .Range("E2:E5000").Find(Target.Value, , , xlWhole, , , False, , False)
            End With
            If fnd Is Nothing Then
                MsgBox Target.Value & " Not found"
                Exit Sub
            End If
            Target.Offset(, -1).Value = fnd.Value & "-" & fnd.Offset(, -3).Value
        End If
    End If
End Sub
 
Upvote 0
Fluff,
post 15 didn't populate the answer but with minor modification it works perfectly. thanks a bunch. it does exactly what i want it to do.
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
    Dim fnd As Range
    
    If Target.CountLarge > 1 Then Exit Sub
    If Not Application.Intersect(Target, Range("C2:C50")) Is Nothing Then
        If Target = "" Then Exit Sub
            With Sheet2
                Set fnd = .Range("E2:E5000").Find(Target.Value, , , xlWhole, , , False, , False)
            End With
            If fnd Is Nothing Then
                MsgBox Target.Value & " Not found"
                Exit Sub
            Else
            Target.Offset(, -1).Value = fnd.Value & "-" & fnd.Offset(, -3).Value
        End If
    End If

End Sub
 
Upvote 0
Glad you got it to work & thanks for the feedback
 
Upvote 0
try changing these two lines:
VBA Code:
   wsr = ws.Range(Cells(1, 3), Cells(5000, 3))
   wsr2 = ws.Range(Cells(1, 5), Cells(5000, 5))

to :
Code:
    With Worksheets("Sheet1")
    wsr = Range(.Cells(1, 3), .Cells(5000, 3))
    End With
    With Worksheets("Sheet2")
    wsr2 = Range(.Cells(1, 5), .Cells(5000, 5))
    End With
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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