Macro code to check for text in a cell before running farther into the macro code

Snake Eyes

Board Regular
Joined
Dec 14, 2010
Messages
103
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a macro that changes the text and formatting of a cell to indicate if the worksheet is protected or not.
When I run the protection macro it runs fine however, If that button is clicked again I get this error...
Macro Error.JPG


I'm trying to piece together code from a somewhat similar macro to bypass the error and stop the macro from running if clicked by mistake after the worksheet has already been protected.
i.e. The cells already contain the text "Protected".

This is what I have so far...
VBA Code:
Sub Protect_Positions01_10()
'
' Protect_Positions01_10 Macro
'
'   Sets Up the Indicator Cell
With Range("I2:K3")
    Dim IllegalCharacter(1) As String, i As Integer
    IllegalCharacter(1) = "Protected"
For i = 1 To 7
If InStr(.Text, (IllegalCharacter(i))) = "Protected" Then
    MsgBox "The worksheet is already protected."
Exit Sub
End If

    Range("I2:K3").Select
    ActiveCell.FormulaR1C1 = "Protected"
    Range("I2:K3").Select

This code doesn't actually stop the macro when the cells contain "Protected".

There's likely something simple I'm missing or maybe even a better way.
Thanks for any assistance.
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Not sure what all that code is for, when it seems you could just use
VBA Code:
If Range("I2").Value <> "Protected" Then
    'do something
End If
 
Upvote 0
Thank You Z51.
I figured I was overcomplicating things.
This is the final code that works...
Excel Formula:
If Range("I2").Value = "Protected" Then
MsgBox "The worksheet is already protected."
Exit Sub
End If
 
Upvote 0
Solution

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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