Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-12-2017, 12:18 AM
Mikedk64 Mikedk64 is offline msgbox when deleting cells Windows 10 msgbox when deleting cells Office 2010 64bit
Novice
msgbox when deleting cells
 
Join Date: Jul 2017
Posts: 4
Mikedk64 is on a distinguished road
Default msgbox when deleting cells

Hi,



Is there a way to display a msgbox, when someone is trying to delete cells in a worksheet?
For example, if I am trying to delete (A32, B32, C32, D32, E32, F32, G32, H32) I want a msgbox saying "Are you sure that you want to delete cells" And then a yes or no choise.
Reply With Quote
  #2  
Old 07-12-2017, 06:07 AM
ArviLaanemets ArviLaanemets is offline msgbox when deleting cells Windows 8 msgbox when deleting cells Office 2016
Expert
 
Join Date: May 2017
Posts: 873
ArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud of
Default

Hi

Practically the only event you can use is OnChange - which is fired AFTER you move cursor from the cell you did change - not very easy at least. When you can somehow to get information, what was the last cursor location before current one, and is this cell empty now, then maybe you can take a step back, when you check No - but not very likely.

And remember - the OnChange event is fired whenever you make any change on any worksheet! So your workbook will be slow down considerabely.
Reply With Quote
  #3  
Old 07-12-2017, 07:04 AM
NoSparks NoSparks is offline msgbox when deleting cells Windows 7 64bit msgbox when deleting cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

@ Mikedk64
Are you actually deleting the cells or clearing the contents ?
ie: the delete key clears the contents whereas right clicking gives option to clear contents or delete which then asks about shifting cells.
Reply With Quote
  #4  
Old 07-12-2017, 07:29 AM
Logit Logit is offline msgbox when deleting cells Windows 10 msgbox when deleting cells Office 2007
Expert
 
Join Date: Jan 2017
Posts: 533
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

Code:
Option Explicit

Sub Test3()
Dim answer As Variant
  answer = MsgBox("Are you certain these cells are to be deleted ?", vbYesNo + vbQuestion, "Delete Cells ?")
  If answer = vbYes Then
      Call Test1
   Else
     Exit Sub
End If
End Sub

Sub Test1()
   'Code here to delete or clear cells
   MsgBox "Running macro"
End Sub
Reply With Quote
  #5  
Old 07-12-2017, 08:12 AM
Mikedk64 Mikedk64 is offline msgbox when deleting cells Windows 10 msgbox when deleting cells Office 2010 64bit
Novice
msgbox when deleting cells
 
Join Date: Jul 2017
Posts: 4
Mikedk64 is on a distinguished road
Default

*Logit:

Where should I insert this code, and can i just copy paste it, or do I need to modify it
Quote:
Originally Posted by Logit View Post
Code:
Option Explicit

Sub Test3()
Dim answer As Variant
  answer = MsgBox("Are you certain these cells are to be deleted ?", vbYesNo + vbQuestion, "Delete Cells ?")
  If answer = vbYes Then
      Call Test1
   Else
     Exit Sub
End If
End Sub

Sub Test1()
   'Code here to delete or clear cells
   MsgBox "Running macro"
End Sub
Reply With Quote
  #6  
Old 07-12-2017, 08:14 AM
Mikedk64 Mikedk64 is offline msgbox when deleting cells Windows 10 msgbox when deleting cells Office 2010 64bit
Novice
msgbox when deleting cells
 
Join Date: Jul 2017
Posts: 4
Mikedk64 is on a distinguished road
Default

* NoSparks:

Sorry for not being clear I mean when I am pressing the delete key.
Quote:
Originally Posted by NoSparks View Post
@ Mikedk64
Are you actually deleting the cells or clearing the contents ?
ie: the delete key clears the contents whereas right clicking gives option to clear contents or delete which then asks about shifting cells.
Reply With Quote
  #7  
Old 07-12-2017, 08:25 AM
Logit Logit is offline msgbox when deleting cells Windows 10 msgbox when deleting cells Office 2007
Expert
 
Join Date: Jan 2017
Posts: 533
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

.
Paste the macro code into a Routine Module. You would activate it by way of a command button located on the worksheet that will have the cells deleted or cleared.

If you can be more specific about which cell / s you intend to delete or clear (and are you deleting the cell altogether or are you only clearing the contents from the cell) ?
Reply With Quote
  #8  
Old 07-12-2017, 04:29 PM
NoSparks NoSparks is offline msgbox when deleting cells Windows 7 64bit msgbox when deleting cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

If the message box is a must, you need the normal means of deleting the cell contents to initiate it.

As ArviLaanemets says, the Change event firing every time something changes will slow down your worksheet
but depending on the worksheet that may or may not be an issue.

Put this code into the sheet module and give it a try.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim aCell As Range

For Each aCell In Target.Cells
    If aCell.Value = "" Then
        If MsgBox("Are you sure you want to " & _
          "delete cell contents ?", 36, "DELETE CELLS ?") <> vbYes Then
              Application.EnableEvents = False
              Application.Undo
              Application.EnableEvents = True
        End If
    End If
    Exit For
Next aCell

End Sub
Reply With Quote
  #9  
Old 07-12-2017, 10:54 PM
ArviLaanemets ArviLaanemets is offline msgbox when deleting cells Windows 8 msgbox when deleting cells Office 2016
Expert
 
Join Date: May 2017
Posts: 873
ArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud of
Default

Quote:
Originally Posted by NoSparks View Post
If the message box is a must, you need the normal means of deleting the cell contents to initiate it.

As ArviLaanemets says, the Change event firing every time something changes will slow down your worksheet
but depending on the worksheet that may or may not be an issue.

Put this code into the sheet module and give it a try.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    Dim aCell As Range
 
For Each aCell In Target.Cells
    If aCell.Value = "" Then
        If MsgBox("Are you sure you want to " & _
          "delete cell contents ?", 36, "DELETE CELLS ?") <> vbYes Then
              Application.EnableEvents = False
              Application.Undo
              Application.EnableEvents = True
        End If
    End If
    Exit For
Next aCell
 
End Sub
Hi!

As I stressed, the change event is fired after you move to another cell! So p.e. you selected cell A2 and cleared cell contents. So long as you don't leave the cell, nothing happens. Then you pressed Tab key - the cursor moves to B2. When this cell is empty, your script undoes last change - which was moving to B2

Probably the only way will be saving at end of your scrip the current selected range to global variable, and at start of scrip (in your For . . Next cycle) you chek the range saved after previous change. And then you need 2 undo's whenever this range is empty and you want to undo deletion.
Reply With Quote
  #10  
Old 07-12-2017, 11:35 PM
NoSparks NoSparks is offline msgbox when deleting cells Windows 7 64bit msgbox when deleting cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

The file I tested with.
Attached Files
File Type: xlsm Delete_msgbox.xlsm (15.5 KB, 10 views)
Reply With Quote
  #11  
Old 07-13-2017, 12:01 AM
ArviLaanemets ArviLaanemets is offline msgbox when deleting cells Windows 8 msgbox when deleting cells Office 2016
Expert
 
Join Date: May 2017
Posts: 873
ArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud of
Default

Quote:
Originally Posted by NoSparks View Post
The file I tested with.
I myself never use any events in Excel VBA except Open event. I assumed, that Change event behaves like the built-in one which contros autocalculation - after you leave the cell. When not, then it is similar to contlrol's Change event in Access - it is fired after every keystroke or mouse operation. I.e. when I type 10-character word into some cell, the Change event is fired 10 times! OK. An additional cause never use it
Reply With Quote
  #12  
Old 07-13-2017, 07:14 AM
NoSparks NoSparks is offline msgbox when deleting cells Windows 7 64bit msgbox when deleting cells Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Hopefully the OP will try the suggestion.

Don't know if they'll return or not.

A week ago the OP got a solution suggested 13 minutes after posting this question at another forum and didn't acknowledge it for 4 days, apparently not getting it to work. Suspect they inserted a regular module and put the code there rather than in the worksheet module.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
msgbox when deleting cells Trigger Beep or MsgBox when key combo accidentally pressed NotQuiteANovice Word VBA 1 04-07-2017 05:41 PM
msgbox when deleting cells Deleting rows based on the text in certain cells VBA ThisGuyJohn Excel Programming 5 02-03-2017 03:16 PM
msgbox when deleting cells On Open MsgBox for use in Template sent out to colleagues to gather information Lancashire Lad Word VBA 2 10-26-2015 10:22 AM
Deleting info from specific table cells jamierbooth Word VBA 3 07-29-2014 03:20 PM
msgbox when deleting cells Make MsgBox appear... Jamtart PowerPoint 3 09-01-2012 08:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft