Private Sub in endless loop

30136353

Board Regular
Joined
Aug 14, 2019
Messages
105
Hi Guys,

I have this private sub that gets stuck on an endless loop, error handling just shows the first line?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Change the cell colour and set the update flag to 1

Dim rngtemp1 As Range
Dim rngTemp3 As Range
Dim wsTemp As Worksheet
Dim lngLastRow As Long
Dim changedCell As Range

If ThisWorkbook.ActiveSheet.Name = "Network Data" Then


    Set wsTemp = ThisWorkbook.Worksheets("Network Data")
    
    lngLastRow = wsTemp.Range("D" & Rows.Count).End(xlUp).Row
    Set rngtemp1 = wsTemp.Range("h2:Z" & Trim(Str(lngLastRow)))
    
    If Not Intersect(Target, rngtemp1) Is Nothing Then
        With Target.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        Set rngTemp3 = Intersect(Target.EntireRow, wsTemp.Range("AD:AD"))
        rngTemp3.Value = "1"
            End With
    End If
End If

   If Not Intersect(Range("V:V"), Target) Is Nothing Then


        For Each changedCell In Intersect(Range("V:V"), Target).Cells
   
            If changedCell.Offset(0, -3).Value = changedCell.Value Then
            changedCell.Offset(0, 1).Value = "Completed"
            End If
            
        Next changedCell
    End If

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You are changing cells on a sheet from inside its Change event, which will therefore get triggered recursively. Use:

Code:
Application.EnableEvents = False

to disable events while changing the cells, then

Code:
Application.EnableEvents = True

to reset before the code ends.
 
Upvote 0
You are changing cells on a sheet from inside its Change event, which will therefore get triggered recursively. Use:

Code:
Application.EnableEvents = False

to disable events while changing the cells, then

Code:
Application.EnableEvents = True

to reset before the code ends.
Thanks for the input. That's it working, much appreciated !
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,613
Members
449,238
Latest member
wcbyers

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