Need VBA Code that protects cells with formulas but allows row deletion

Msamps

New Member
Joined
Sep 21, 2017
Messages
1
Hello Everyone,

First of all, I have to say, this forum has helped me immensely and I am so thankful for all the help offered here!

I have not been able to find a solution to this despite the extensive research I have attempted. I have a VBA code that will prevent the user from modifying any of the cells that contain a formula (code below). I am using this in a data collection excel template I developed and it will be used to create workbooks for nurses so they can track healthcare related information but I don't want them inadvertently delete any formulas. They will need the option to delete the row if needed in case they make an entry by mistake. This code is preventing me from doing so.

Also, protecting the worksheet is not an option because it will not allow the tool to work as intended.

I am stumped...

'Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim rCell As Range

Set rCell = Nothing
If Target.Cells.Count = 1 And Target.HasFormula Then
ActiveCell.Offset(0, 1).Select
MsgBox "Formulas cannot be modified"
'Allow ActiveRow.Delete = True'
'or?'
'Activerow.AllowDeletingRows = True'

ElseIf Target.Cells.Count > 1 Then

On Error Resume Next
Set rCell = Target.Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0

If Not rCell Is Nothing Then ActiveCell.Offset(0, 1).Select

End If


End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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