Warning Pop-Up

bergsmat

New Member
Joined
Sep 18, 2013
Messages
40
Hi I was wondering if there was anyway for a warning message to pop-up when a user tries to insert or delete rows from a workbook? Ideally I would like to have a simple message box appear telling them the dangers of adding/removing a row or column incorrectly. I would like it to give the user an option to continue (add/delete the row or column) or cancel (do nothing).

Any help would be great!

Tyler
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Something like this may be a start....

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    
    Dim retval As Integer
    If Target.Rows.Count = Sh.Rows.Count Or _
    Target.Columns.Count = Sh.Columns.Count Then
    'Acting on Column(s) or _
    'Acting on Rows(s)
    Msg001 = "Be warned, you are acting on items that will potentially destroy the operation of this workbook."
        retval = MsgBox(Msg001, vbOKCancel, "Warning!")
        If retval <> vbOK Then 'They Canceled
            Application.EnableEvents = False
            Application.Undo
            Application.EnableEvents = True
        Else 'They OKed
            'Do nothing
        End If
    End If
End Sub
 
Upvote 0
That worked great! Is there code that could be added to this that has the same effect if the rows or columns are trying to be hidden?

Tyler
 
Upvote 0
Technically, I think hiding and unhiding are formatting changes that do not initiate events.
The best I could see was when the Sheet was deactivated (i.e. Select another sheet); then check and alert

Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim retval As Integer
    Dim col As Range, rw As Range
    For Each col In Sh.UsedRange.Columns
        'Check Columns
        If col.Hidden Then
                    msg = "There are hidden columns in the worksheet (" & Sh.Name & ").  Unhide?"
                    retval = MsgBox(msg, vbYesNo, "Warning")
                    If retval = vbYes Then 'They OKd it
                        Application.EnableEvents = False
                            Sh.UsedRange.Columns.Hidden = False
                            Sh.UsedRange.Rows.Hidden = False
                        Application.EnableEvents = True
                    Else    'They didnt OK it
                        'Do nothing
                    End If
                    
        Else
            'Check Rows
            For Each rw In Sh.UsedRange.Rows
                If rw.Hidden Then
                    msg = "There are hidden rows in the worksheet (" & Sh.Name & ").  Unhide?"
                    retval = MsgBox(msg, vbYesNo, "Warning")
                    If retval = vbYes Then 'They OKd it
                        Application.EnableEvents = False
                            Sh.UsedRange.Rows.Hidden = False
                        Application.EnableEvents = True
                    Else    'They didnt OK it
                        'Do nothing
                    End If
                End If
            Next
        End If
    Next
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,474
Messages
6,125,024
Members
449,204
Latest member
LKN2GO

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