If you want to avoid deleting rows from Parking
WorkSheet, in the event Workbook_SheetActivate
you should put something like this:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Parking" Then
'CODE TO BLOCK DELETING ROWS
Else
'CODE TO ALLOW DELETING ROWS
End If
End Sub
Sh
is the Worksheet you activate when you change from 1 worksheet to a different one, so when you activate Parking
then the code will trigger blocking deleting rows. If it's not PArking
then it will allow deleting rows.
UPDATE: You must type this code in the VBA editor, in ThisWorkbook
Object.
Once donde, you don't need to pass a parameter, it will work by itself. Events are triggered when a specific action takes places, and don't need the user to pass a parameter (they get it instantly). In this case, the event triggers when user changes from 1 worksheet to another worksheet, and the parameter is the target worksheet (it's stored in the parameter Sh
) but you don't need to call it with lines of codes. It will trigger by itself.
You can read more info here about this event:
Workbook.SheetActivate event
If you want to know more about other events, check:
Excel VBA Events
IMPORTANT: Once you have code in any event, that event will be ready to trigger at anytime. In this case, if any other macro of you activates sheet Parking
, then it will be triggered.
Also, as other users have posted,your codes just prevents deleting rows from command bar, but probably users can still delete rows with rightClick of mouse, or keyboards shortcuts, or other macros.
If you really want to protect that sheet from any deleting, best option is, indeed, @MathieuGuindon answer, protecting the sheet. That way would be more secure.
Worksheet.Protection
– Mathieu GuindonWorkbook_SheetActivate
to get the name of the active sheet if it changes,check the name and if it's the one you want to block, then activate your code. IF not, deactivate your code. – Foxfire And Burns And Burns