My code wont work from button

michaelsmithgall

New Member
Joined
Feb 22, 2011
Messages
19
i wasnt sure how to explain this the header

I have a simple peice of code that clears a few cells. I went back and added another row and therefore updated the code. Now when click the button all my other cells clear as supposed to but my new one doesnt.. but if i run the moduile within the VBA window it works as expected.

The cell that isnt working is D4. i also tried running it with D2:D6 but that didnt work either. I cant figure it out

Private Sub ClearMDL_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Range("H14:H82").Select
Selection.ClearContents
Range("D2:D4").Select
Selection.ClearContents
Range("D5:D6").Select
Selection.ClearContents
Range("D8:D11").Select
Selection.ClearContents
Range("D22:D36").Select
Selection.ClearContents
Range("B40").Select
Selection.ClearContents


Sheets("MDL").Select
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Do you have some other events which 'do stuff' to D4?

Try stepping through the code using the F8 key - make sure program execution is going through the code and not disappearing off somewhere else. If it is, turn off event handling whilst you're clearing the cells (see below):-

By the way, this is neater:-
Code:
Application.EnableEvents = False
Range("H14:H82").ClearContents
Range("D2:D4").ClearContents
Range("D5:D6").ClearContents
Range("D8:D11").ClearContents
Range("D22:D36").ClearContents
Range("B40").ClearContents
Application.EnableEvents = True
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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