Confused Changed Event

mgw1138

New Member
Joined
Mar 9, 2014
Messages
33
I have dropdowns on my sheet at I1, I6, I11, I16 and I21. When I select a new value in I1 from the dropdown box I immediately get the following error

"method "_default" of object range failed" After maybe 5 different errors Excel terminates.


Private Sub Worksheet_Change(ByVal Target As Range)
Dim CA As Integer
CA = 0
If Not Intersect(Target, Range("I1")) Is Nothing Then CA = 1 < ------ Debugger stops here.
If Not Intersect(Target, Range("I6")) Is Nothing Then CA = 6
If Not Intersect(Target, Range("I11")) Is Nothing Then CA = 11
If Not Intersect(Target, Range("I16")) Is Nothing Then CA = 16
If Not Intersect(Target, Range("I21")) Is Nothing Then CA = 21
If CA = 0 Then Exit Sub

Dim X As Integer
For X = 2 To 7
If Cells(CA, 9) = Cells(CA, X) Then Exit For
Next X

' these next 5 lines do formating based on the dropdown activated.
Cells(CA, 9) = Cells(CA, X)
Cells(CA + 1, 9) = Cells(CA + 1, X)
Cells(CA + 2, 9) = Cells(CA + 2, X)
If Cells(CA + 3, X) = 1 Then Target.Font.Color = 0
If Cells(CA + 3, X) = 2 Then Target.Font.Color = 255
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You seem to have created a condition that is triggering an infinite loop.

In the instances where you set CA = 1 when you get to the line below you essentially trigger the loop to repeat.

If CA = 1 then you essentially update cell I1 = Cells(CA, X)
VBA Code:
Cells(CA, 9) = Cells(CA, X)

This will result in it eventually crashing from the runaway loop.

You can see this if you put a break in the debugger and step through the code, it will hit this line
Code:
Cells(CA, 9) = Cells(CA, X)

And retriggers the change.

These lines never get called.

Code:
Cells(CA + 1, 9) = Cells(CA + 1, X)
Cells(CA + 2, 9) = Cells(CA + 2, X)
If Cells(CA + 3, X) = 1 Then Target.Font.Color = 0
If Cells(CA + 3, X) = 2 Then Target.Font.Color = 255


Here you can see I put a value in I1 which triggered the change, this will result in an infinite loop when it tries to update I1 again.

1652661855098.png
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,457
Messages
6,124,941
Members
449,197
Latest member
k_bs

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