You are here:

Excel/Excel PROTECTED WORKSHEETS & ALLOWABLE ACTIONS

Advertisement


Question
QUESTION: Hi Tom,

You helped me out in the question I asked back in February that is at the link pasted below this question.  I am wondering if you can advise me how to include more than once sheet to exclude from the spell check code that is run by a macro I have pasted below?

As it stands now i am excluding the "Valuation" sheet.  I am looking to exclude two more sheets.  Maybe it would be easier to write the code to only check certain sheets.  If you can provide help with either one it is much appreciated.

Thanks again,

Will



Current Code is:

Sub spell_check_allsheets()
Dim WS As Worksheet
Application.ScreenUpdating = False
For Each WS In ActiveWorkbook.Worksheets

If LCase(WS.Name) <> LCase("valuation") Then
 WS.Select  '< moved inside the IF test
 ActiveSheet.Unprotect Password:="password"
 Cells.CheckSpelling SpellLang:=1033
 ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
      False, Password:="password"
End If
Next WS
Application.ScreenUpdating = True
Worksheets("Capex").Select
End Sub


Link to original question
http://en.allexperts.com/q/Excel-1059/2013/2/protected-worksheets-allowable-acti

ANSWER: William,


Sub spell_check_allsheets()
Dim WS As Worksheet
Application.ScreenUpdating = False
For Each WS In ActiveWorkbook.Worksheets

If LCase(WS.Name) <> LCase("valuation") And _
  LCase(WS.Name) <> LCase("sheet2") And _
  LCase(WS.Name) <> LCase("sheet3")  Then
WS.Select  '< moved inside the IF test
ActiveSheet.Unprotect Password:="password"
Cells.CheckSpelling SpellLang:=1033
ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
     False, Password:="password"
End If
Next WS
Application.ScreenUpdating = True
Worksheets("Capex").Select
End Sub


Replace sheet2 and sheet3 with the names of the sheets you wish to exclude.

--
Regards,
Tom Ogilvy



---------- FOLLOW-UP ----------

QUESTION: Hi Tom,
I am just about there on this user form.  I’ve included the additional code to exclude additional sheets from the spell check code you helped me with in the recent past.  I am running into the issue with having either the “Capex Narrative” sheet or the “write up” sheet hidden based on a the type of project request being submitted.
I run into the  “Run-time error ‘1004’.  This error is what prompted the code within the if statement originally so it helpped when I added the “Form controls” tab to the code to skip during spell check as you suggested.   If I put both sheets, (“Capex narrative” and “write up” ), in the if statement code below the code runs fine, but I don’t spell check these two sheets.  Is there a way to unhide the sheet that is hidden, either the “capex narrative” or the “write up”, then run the spell check code below, and then re-hide the sheet?  Or do you have additional code that will get me around this issue? The tricky part is it is either one or the other that is hidden and it’s not the same sheet each time.

***current code***
Sub spell_check_allsheets()
Dim WS As Worksheet
Application.ScreenUpdating = False
For Each WS In ActiveWorkbook.Worksheets
If LCase(WS.Name) <> LCase("valuation") And _
 LCase(WS.Name) <> LCase("Form Controls") And _
 LCase(WS.Name) <> LCase("Supporting Doc") And _
 LCase(WS.Name) <> LCase("disposal detail") And _
 LCase(WS.Name) <> LCase("additional Support") And _
 LCase(WS.Name) <> LCase("Form Setup") And _
 LCase(WS.Name) <> LCase("Capex One Pager") And _
 LCase(WS.Name) <> LCase("Capex three page summary") And _
 LCase(WS.Name) <> LCase("Disposal Summary") Then
WS.Select  '< moved inside the IF test
ActiveSheet.Unprotect Password:="password"
Cells.CheckSpelling SpellLang:=1033
ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
    False, Password:="password"
End If
Next WS
Application.ScreenUpdating = True
Worksheets("Capex").Select
End Sub


Thanks again. And as always your time is appreicated.

Answer
William,

I understand you to say, that if you want to spellcheck a sheet and it is hidden, then you get an error.  So this code will unhide such a sheet, process it, then hide it again


Sub spell_check_allsheets()
Dim WS As Worksheet
Dim bHidden As Boolean
Application.ScreenUpdating = False
For Each WS In ActiveWorkbook.Worksheets
bHidden = False
If LCase(WS.Name) <> LCase("valuation") And _
 LCase(WS.Name) <> LCase("Form Controls") And _
 LCase(WS.Name) <> LCase("Supporting Doc") And _
 LCase(WS.Name) <> LCase("disposal detail") And _
 LCase(WS.Name) <> LCase("additional Support") And _
 LCase(WS.Name) <> LCase("Form Setup") And _
 LCase(WS.Name) <> LCase("Capex One Pager") And _
 LCase(WS.Name) <> LCase("Capex three page summary") And _
 LCase(WS.Name) <> LCase("Disposal Summary") Then
 If WS.Visible = xlSheetHidden Then
   bHidden = True
   WS.Visible = xlSheetVisible
 End If
   WS.Select  '< moved inside the IF test
   ActiveSheet.Unprotect Password:="password"
     Cells.CheckSpelling SpellLang:=1033
   ActiveSheet.Protect DrawingObjects:=False, contents:=True, Scenarios:= _
    False, Password:="password"
 If bHidden Then
   WS.Visible = xlSheetHidden
 End If
End If
Next WS
Application.ScreenUpdating = True
Worksheets("Capex").Select
End Sub

Hopefully that is what you wanted.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.