Macros works by itself, but crashes Excel when Called

john316swan

Board Regular
Joined
Oct 13, 2016
Messages
66
Office Version
  1. 2019
Platform
  1. Windows
I have this worksheet change:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If (Range("O4")) = True Then
    Range("O4").Select
    Selection.ClearContents
    Call [B]DeleteConnection[/B]
    End If


    If Not Intersect(Target, Range("q71:r71")) Is Nothing Then
    Call MakeStatic
    End If
    
End Sub

The DeleteConnection works perfectly when I run it on it's own but when it is called (from the event change above) Excel crashes :(

Here is the DeleteConnection macro:

Code:
Sub DeleteConnection()
'
' DeleteConnection Macro
' Deletes External Data Collections and makes RET aid static
'


'
    Range("A4:K4").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("J7:N7").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Sheets("Values").Select
    Range("T116:V122").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("T123:U130").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("A1").Select
    ActiveWorkbook.Connections("YR-16 Weekly Report").Delete
    Sheets("Values").Select
    Sheets("RET").Visible = True
    Sheets("RET").Select
    Application.DisplayAlerts = False
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = True
    Sheets("TUG-E").Select
    Range("L4:N4").Select
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
John,

The worksheet change code and the calls it is making are resulting in changes to the sheet hence the worksheet change event is triggered ad-infinitum and Excel disappears up its own whatsit.

Try the below and see if that sorts it.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If (Range("O4")) = True Then
    Range("O4").Select
    Selection.ClearContents
    Call DeleteConnection
    End If




    If Not Intersect(Target, Range("q71:r71")) Is Nothing Then
    Call MakeStatic
    End If
  Application.EnableEvents = True  '  Must always reset this
End Sub

Hope that helps.
 
Upvote 0
If your called routines are changing cells (appears at quick glance they are) then you need to turn off events while they run. See if this does what you want:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If (Range("O4")) = True Then
    Range("O4").Select
    Selection.ClearContents
    Call DeleteConnection
    End If


    If Not Intersect(Target, Range("q71:r71")) Is Nothing Then
    Call MakeStatic
    End If
    Application.EnableEvents = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,013
Members
448,935
Latest member
ijat

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