Crashing Code

Overdriven Pub

New Member
Joined
May 3, 2019
Messages
3
I'm trying to make a spreadsheet such that when the day changes (Cell B2), an operator changes another cell and a third cell is cleared. On top of that, whenever a value is entered into a fourth cell, it is added to a fifth cell and the fourth cell clears. My code is below. When I enter a value in the fourth cell, I get the message "Method 'Value' of object 'Range' failed." When I change Cell B2, no errors or issues arise. When I debug, the fourth line from the bottom is highlighted.

Private Sub Worksheet_Change(ByVal target As Range) Dim KeyCells As Range


Set KeyCells = Range("B2")

If Not Application.Intersect(KeyCells, Range(target.Address)) _
Is Nothing Then


Range("A2").Value = Range("A2").Value - Range("F2").Value
Range("F2").Clear
Range("F2").NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"


End If


Set KeyCells = Range("G2")

If Not Application.Intersect(KeyCells, Range(target.Address)) _
Is Nothing Then


Range("F2").Value = Range("F2").Value + Range("G2").Value *THIS LINE IS HIGHLIGHTED WHEN I DEBUG*
Range("G2").Clear

End If
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Before the error message populates, all functions are carried out as expected. I have to end the program or debug, which is finalized by ending the program.
 
Upvote 0
Hi, welcome to the forum! When you are changing cells within a change event macro you need to toggle events on and off to prevent the code recursively calling itself over and over again.

You should add this line before you attempt to change any cells on the worksheet:

Code:
Application.EnableEvents = False

And this afterwards.

Code:
Application.EnableEvents = True
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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