Preventing formulas being entered into cells via VBA (with added msgbox)

M1111

New Member
Joined
Feb 3, 2017
Messages
6
Hi,

I can't seem to get my code to pick up a Worksheet_Change event for when a cells has a formula.

This is what I have so far (the top part is to prevent pasting of formulas, I have highlighted the relevant code in red).

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)


Dim Rng As Range
Dim c As Range


If Intersect(ActiveCell, Range("B6:B25")) Is Nothing Then


Exit Sub


        ElseIf Not Intersect(ActiveCell, Range("B6:B25")) Is Nothing And Application.CutCopyMode = xlCopy Then


            Application.EnableEvents = False


                Application.Undo
                Target.PasteSpecial Paste:=xlPasteValues
            
                Application.EnableEvents = True


[COLOR=#ff0000][B]        ElseIf Intersect(ActiveCell, Range("X6:X25")) Is Nothing Then[/B][/COLOR]

[COLOR=#ff0000][B]Exit Sub[/B][/COLOR]

[COLOR=#ff0000][B]        ElseIf Not Intersect(ActiveCell, Range("X6:X25")) Is Nothing Then[/B][/COLOR]

[COLOR=#ff0000][B]        Application.EnableEvents = False[/B][/COLOR]

[COLOR=#ff0000][B]        For Each c In Target.Cells[/B][/COLOR]
[COLOR=#ff0000][B]            If c.HasFormula Then[/B][/COLOR]
[COLOR=#ff0000][B]            Application.Undo[/B][/COLOR]
[COLOR=#ff0000][B]            MsgBox "You must not enter a formula"[/B][/COLOR]
[COLOR=#ff0000][B]            End If[/B][/COLOR]
[COLOR=#ff0000][B]        Next[/B][/COLOR]
[COLOR=#ff0000][B]End If[/B][/COLOR]

[COLOR=#ff0000][B]Application.EnableEvents = True[/B][/COLOR]

[COLOR=#ff0000][B]End Sub[/B][/COLOR]

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Managed to fix this one myself, here's the code. I changed it to a Worksheet_Calculate:

Code:
Private Sub Worksheet_Calculate()

Dim c As Range


        If Intersect(ActiveCell, Range("X6:X25")) Is Nothing Then
        
Exit Sub


        ElseIf Not Intersect(ActiveCell, Range("X6:X25")) Is Nothing Then
        
        Application.EnableEvents = False
    
        For Each c In Range("X6:X25")
            If c.HasFormula = True Then
            Application.Undo
            MsgBox "You must not enter a formula"
            End If
        Next


Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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