Private Sub Routine is not functioning

%3CLINGO-SUB%20id%3D%22lingo-sub-2190954%22%20slang%3D%22en-US%22%3EPrivate%20Sub%20Routine%20is%20not%20functioning%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2190954%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20would%20be%20the%20reason%20for%2C%20not%20working%20Private%20sub%20routine%20automatically%20whenever%20any%20changes%20done%20in%20range.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3C%2FP%3E%3CP%3E%3CSTRONG%3Ewhenever%20I%20restart%20the%20excel%20it%20will%20work%20for%20a%20little%20time%20and%20later%20it%20become%20not%20respondive.%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eplease%20help%20me%20on%20this%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2190954%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2191026%22%20slang%3D%22en-US%22%3ERe%3A%20Private%20Sub%20Routine%20is%20not%20functioning%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2191026%22%20slang%3D%22en-US%22%3EWithout%2C%20at%20the%20very%20least%2C%20the%20macro%20code%20to%20look%20at%2C%20it's%20very%20hard%20to%20say%20(a%20copy%20of%20the%20workbook%20would%20be%20ideal%2C%20if%20there%20is%20no%20sensitive%20information%20or%20if%20the%20sensitive%20information%20could%20be%20deleted).%3CBR%20%2F%3E%3CBR%20%2F%3EBut%2C%20taking%20a%20shot%20in%20the%20dark%2C%20does%20your%20change%20event%20make%20changes%20to%20the%20worksheet%3F%20If%20so%2C%20then%20you%20could%20potentially%20have%20an%20infinite%20loop%20(where%20the%20change%20event%20makes%20a%20change%20that%20triggers%20itself).%20If%20you%20are%20making%20changes%20to%20the%20worksheet%2C%20then%20you%20will%20need%20to%20disable%20events%20before%20you%20make%20the%20changes%3A%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20False%3CBR%20%2F%3E%3CBR%20%2F%3Eand%20re-enable%20events%20before%20the%20macro%20exits%3A%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20False%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2191426%22%20slang%3D%22en-US%22%3ERe%3A%20Private%20Sub%20Routine%20is%20not%20functioning%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2191426%22%20slang%3D%22en-US%22%3EI%20tried%20this%2C%20its%20not%20working.%3CBR%20%2F%3E%3CBR%20%2F%3EPlease%20see%20the%20code%20below.%20Its%20a%20data%20entry%20form%2C%20and%20the%20codes%20are%20using%20for%20jumping%20to%20next%20cell%20after%20the%20entry.%3CBR%20%2F%3E%3CBR%20%2F%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3EMyVal%20%3D%20Range(%22Total4%22).Value%3CBR%20%2F%3E%3CBR%20%2F%3EWith%20ActiveSheet.Tab%3CBR%20%2F%3ESelect%20Case%20MyVal%3CBR%20%2F%3ECase%20Is%20%26gt%3B%200%3CBR%20%2F%3E.Color%20%3D%20vbBlack%3CBR%20%2F%3ECase%20Is%20%3D%200%3CBR%20%2F%3E.Color%20%3D%20vbRed%3CBR%20%2F%3ECase%20Else%3CBR%20%2F%3E.ColorIndex%20%3D%20xlColorIndexNone%3CBR%20%2F%3EEnd%20Select%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22b%3Ab%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%201).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22c%3Ac%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(1%2C%20-1).Activate%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22e%3Ae%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%201).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22f%3Af%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(1%2C%20-1).Activate%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22h%3Ah%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%201).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22i%3Ai%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(1%2C%20-1).Activate%3CBR%20%2F%3E%3CBR%20%2F%3E'OB%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d18%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-6%2C%200).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22d12%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d11%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(7%2C%203).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g18%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-6%2C%200).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22g12%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g11%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(3%2C%20-3).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d22%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%203).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g22%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(1%2C%20-3).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d16%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-3%2C%203).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g16%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%20-3).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d20%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22d19%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(1%2C%203).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22g20%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g19%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(1%2C%20-3).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d10%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3E%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22d9%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d8%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(2%2C%203).Activate%3CBR%20%2F%3E%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g10%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22g9%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(-1%2C%200).Activate%3CBR%20%2F%3E%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g8%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%20-3).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EIf%20Not%20Intersect(Target%2C%20Me.Range(%22d26%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%203).Activate%3CBR%20%2F%3E%3CBR%20%2F%3EElseIf%20Not%20Intersect(Target%2C%20Me.Range(%22g26%22))%20Is%20Nothing%20Then%3CBR%20%2F%3ETarget.Offset(0%2C%20-3).Activate%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2191427%22%20slang%3D%22en-US%22%3ERe%3A%20Private%20Sub%20Routine%20is%20not%20functioning%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2191427%22%20slang%3D%22en-US%22%3EIts%20working%20first%20time%20after%20the%20excel%20restarts.%3C%2FLINGO-BODY%3E
Contributor

Hi

 

What would be the reason for, not working Private sub routine automatically whenever any changes done in range. 

 

Private Sub Worksheet_Change(ByVal Target As Range)

whenever I restart the excel it will work for a little time and later it become not respondive. 

 

please help me on this

 

6 Replies
Without, at the very least, the macro code to look at, it's very hard to say (a copy of the workbook would be ideal, if there is no sensitive information or if the sensitive information could be deleted).

But, taking a shot in the dark, does your change event make changes to the worksheet? If so, then you could potentially have an infinite loop (where the change event makes a change that triggers itself). If you are making changes to the worksheet, then you will need to disable events before you make the changes:
Application.EnableEvents = False

and re-enable events before the macro exits:
Application.EnableEvents = False
I tried this, its not working.

Please see the code below. Its a data entry form, and the codes are using for jumping to next cell after the entry.

Private Sub Worksheet_Change(ByVal Target As Range)
MyVal = Range("Total4").Value

With ActiveSheet.Tab
Select Case MyVal
Case Is > 0
.Color = vbBlack
Case Is = 0
.Color = vbRed
Case Else
.ColorIndex = xlColorIndexNone
End Select
End With

If Not Intersect(Target, Me.Range("b:b")) Is Nothing Then
Target.Offset(0, 1).Activate
ElseIf Not Intersect(Target, Me.Range("c:c")) Is Nothing Then
Target.Offset(1, -1).Activate

End If
If Not Intersect(Target, Me.Range("e:e")) Is Nothing Then
Target.Offset(0, 1).Activate
ElseIf Not Intersect(Target, Me.Range("f:f")) Is Nothing Then
Target.Offset(1, -1).Activate

End If
If Not Intersect(Target, Me.Range("h:h")) Is Nothing Then
Target.Offset(0, 1).Activate
ElseIf Not Intersect(Target, Me.Range("i:i")) Is Nothing Then
Target.Offset(1, -1).Activate

'OB
End If
If Not Intersect(Target, Me.Range("d18")) Is Nothing Then
Target.Offset(-6, 0).Activate
ElseIf Not Intersect(Target, Me.Range("d12")) Is Nothing Then
Target.Offset(-1, 0).Activate
End If
If Not Intersect(Target, Me.Range("d11")) Is Nothing Then
Target.Offset(7, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g18")) Is Nothing Then
Target.Offset(-6, 0).Activate
End If
If Not Intersect(Target, Me.Range("g12")) Is Nothing Then
Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("g11")) Is Nothing Then
Target.Offset(3, -3).Activate
End If

If Not Intersect(Target, Me.Range("d22")) Is Nothing Then
Target.Offset(0, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g22")) Is Nothing Then
Target.Offset(1, -3).Activate
End If

If Not Intersect(Target, Me.Range("d16")) Is Nothing Then
Target.Offset(-3, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g16")) Is Nothing Then
Target.Offset(0, -3).Activate
End If

If Not Intersect(Target, Me.Range("d20")) Is Nothing Then
Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("d19")) Is Nothing Then
Target.Offset(1, 3).Activate
End If

If Not Intersect(Target, Me.Range("g20")) Is Nothing Then
Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("g19")) Is Nothing Then
Target.Offset(1, -3).Activate
End If

If Not Intersect(Target, Me.Range("d10")) Is Nothing Then
Target.Offset(-1, 0).Activate

ElseIf Not Intersect(Target, Me.Range("d9")) Is Nothing Then
Target.Offset(-1, 0).Activate
End If
If Not Intersect(Target, Me.Range("d8")) Is Nothing Then
Target.Offset(2, 3).Activate

ElseIf Not Intersect(Target, Me.Range("g10")) Is Nothing Then
Target.Offset(-1, 0).Activate
End If

If Not Intersect(Target, Me.Range("g9")) Is Nothing Then
Target.Offset(-1, 0).Activate

ElseIf Not Intersect(Target, Me.Range("g8")) Is Nothing Then
Target.Offset(0, -3).Activate
End If
If Not Intersect(Target, Me.Range("d26")) Is Nothing Then
Target.Offset(0, 3).Activate

ElseIf Not Intersect(Target, Me.Range("g26")) Is Nothing Then
Target.Offset(0, -3).Activate
End If

End Sub
Its working first time after the excel restarts.

@Sameer_Kuppanath_Sulta 

 

Your code is fine and it should work as you haven't disabled Events in your change event code.
Is there any other code where you have disabled the Events with the following line?
Application.EnableEvents = False

If so please make sure that you enable the Events again before the End Sub or Exit Sub if any in your code.

Btw try this version and see if this works for you. This is a workaround but you will have to figure it out why Events are being disabled. To check that when your code stops working, open the Immediate window by pressing Ctrl+G and type ?Application.EnableEvents in there and hit Enter.
What do you get in the Immediate Window, a True or a False?

 

Private Sub Worksheet_Change(ByVal Target As Range)
MyVal = Range("Total4").Value

On Error GoTo Skip

With ActiveSheet.Tab
Select Case MyVal
Case Is > 0
.Color = vbBlack
Case Is = 0
.Color = vbRed
Case Else
.ColorIndex = xlColorIndexNone
End Select
End With

If Not Intersect(Target, Me.Range("b:b")) Is Nothing Then
    Target.Offset(0, 1).Activate
ElseIf Not Intersect(Target, Me.Range("c:c")) Is Nothing Then
    Target.Offset(1, -1).Activate
End If

If Not Intersect(Target, Me.Range("e:e")) Is Nothing Then
    Target.Offset(0, 1).Activate
ElseIf Not Intersect(Target, Me.Range("f:f")) Is Nothing Then
    Target.Offset(1, -1).Activate
End If

If Not Intersect(Target, Me.Range("h:h")) Is Nothing Then
    Target.Offset(0, 1).Activate
ElseIf Not Intersect(Target, Me.Range("i:i")) Is Nothing Then
    Target.Offset(1, -1).Activate
'OB
End If

If Not Intersect(Target, Me.Range("d18")) Is Nothing Then
    Target.Offset(-6, 0).Activate
ElseIf Not Intersect(Target, Me.Range("d12")) Is Nothing Then
    Target.Offset(-1, 0).Activate
End If

If Not Intersect(Target, Me.Range("d11")) Is Nothing Then
    Target.Offset(7, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g18")) Is Nothing Then
    Target.Offset(-6, 0).Activate
End If

If Not Intersect(Target, Me.Range("g12")) Is Nothing Then
    Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("g11")) Is Nothing Then
    Target.Offset(3, -3).Activate
End If

If Not Intersect(Target, Me.Range("d22")) Is Nothing Then
    Target.Offset(0, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g22")) Is Nothing Then
    Target.Offset(1, -3).Activate
End If

If Not Intersect(Target, Me.Range("d16")) Is Nothing Then
    Target.Offset(-3, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g16")) Is Nothing Then
    Target.Offset(0, -3).Activate
End If

If Not Intersect(Target, Me.Range("d20")) Is Nothing Then
    Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("d19")) Is Nothing Then
    Target.Offset(1, 3).Activate
End If

If Not Intersect(Target, Me.Range("g20")) Is Nothing Then
    Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("g19")) Is Nothing Then
    Target.Offset(1, -3).Activate
End If

If Not Intersect(Target, Me.Range("d10")) Is Nothing Then
    Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("d9")) Is Nothing Then
    Target.Offset(-1, 0).Activate
End If

If Not Intersect(Target, Me.Range("d8")) Is Nothing Then
    Target.Offset(2, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g10")) Is Nothing Then
    Target.Offset(-1, 0).Activate
End If

If Not Intersect(Target, Me.Range("g9")) Is Nothing Then
    Target.Offset(-1, 0).Activate
ElseIf Not Intersect(Target, Me.Range("g8")) Is Nothing Then
    Target.Offset(0, -3).Activate
End If

If Not Intersect(Target, Me.Range("d26")) Is Nothing Then
    Target.Offset(0, 3).Activate
ElseIf Not Intersect(Target, Me.Range("g26")) Is Nothing Then
    Target.Offset(0, -3).Activate
End If
Skip:
Application.EnableEvents = True
End Sub

@Sameer_Kuppanath_Sulta 

 

I don't see any issues with your code. By chance, do you have other event procedures, such as "selection_change"?

 

Another thing you could try, is setting a break point in your code (click in the gray area to the left and you see a circle with the line highlighted - click again to remove it). When your code runs, it will stop on this line. Then, you can hit the F8 key to step through it and follow the flow of your program. Perhaps it will help identify where the issue is.

JMB17_0-1615133971162.png

 

 

 

See I tried. still its not working, but its working all "open" excel sheet closes and restart the excel.

So my doubt is on other excel sheets opened and which had already codes.