How to loop through all worksheets in a workbook and spell check them all before closing/saving?

jberman

New Member
Joined
Dec 12, 2016
Messages
10
HI There, I appreciate anyones help. I have a macro that should loop through all the worksheets in my workbook and run a spell checker on all the sheets before saving and/or closing the document. This is what I have so far but it doesn't seem to work. ALSO Note that my worksheet only has two sheets in it but both are password protected using the word "expenses" as the password.

Here is my code...
Private Sub Workbook_BeforeClose(Cancel As Boolean)Dim ws As Worksheets
With ActiveSheet
.Unprotect ("expenses")
For Each ws In Worksheet
Cells.CheckSpelling
Next
.Protect ("expenses")
End With
End Sub


second code....

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheets
With ActiveSheet
.Unprotect ("expenses")
For Each ws In Worksheets
Cells.CheckSpelling
Next
.Protect ("expenses")
End With
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You also need to qualify Cells with ws.
For Example...


Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet

For Each ws In Worksheets
 ws.Unprotect ("expenses")
 ws.Cells.CheckSpelling
 ws.Protect ("expenses") 
Next

End Sub

or

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet

For Each ws In Worksheets

With ws
    .Unprotect ("expenses")
    .Cells.CheckSpelling
    .Protect ("expenses") 
Next

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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