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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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