简体   繁体   中英

Excel Macro - Unlock, Spellcheck, Lock

I have a macro that needs to unlock all the sheets in a workbook, run the spell checker, and lock all the sheets (with the original column/cell formatting allowed). The locking errors out each time and I can't figure out why.

I know this doesn't include the formatting aspect, but here's what I've got.

Sub SpellChecker()

'unprotect all sheets
For i = 1 To Sheets.Count
    Sheets(i).Unprotect "Password"
Next i

'select all sheets
Dim ws As Worksheet
For Each ws In Sheets
    If ws.Visible Then ws.Select (False)
Next

'run spellchecker
Application.CommandBars.FindControl(ID:=2).Execute

'protect sheets  
For i = 1 To Sheets.Count
    Sheets(i).Protect "Password"
Next i

'selects one sheet/deselect all
Sheets("Sheet1").Select

End Sub

At the point of protecting the sheets, you still have all the sheets selected.

Select just one before

Sheets("Sheet1").Select

'protect sheets
For I = 1 To Sheets.Count
    Sheets(I).Protect "Password"
Next I

However, perhaps doing them one sheet at a time is an idea..?

Sub SpellChecker()

    For Each ws In Sheets
        If ws.Visible Then
            ws.Unprotect "Password"
            ws.Select
            Application.CommandBars.FindControl(ID:=2).Execute
            ws.Protect "Password"
        End If
    Next

End Sub

Here is a method that does not require that the Worksheets ever be unprotected - instead, it will change the Protection on Protected Sheets to allow VBA to edit cells (but not allow the User to edit them) - however, this requires Range.CheckSpelling instead of Application.CommandBars.FindControl(ID:=2).Execute

Sub CheckAllSpelling()
    Dim CheckSheet As Worksheet, CheckRange As Range, CheckCell As Range, SheetVisible AS XlSheetVisibility
    'Loop through Worksheets in the Workbook
    For Each CheckSheet In ThisWorkbook.Worksheets
        'Allow VBA to edit a Protected Sheet, but not the User
        If CheckSheet.ProtectContents Then CheckSheet.Protect Password:="Password", UserInterfaceOnly:=True
        'Filter for Cells with Text to check
        On Error Resume Next
        Set CheckRange = CheckSheet.UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues)
        On Error GoTo 0
        'If there are Cells to Check
        If Not CheckRange Is Nothing Then
            SheetVisible = CheckSheet.Visible
            'Loop through cells
            For Each CheckCell In CheckRange.Cells
                With CheckCell
                    'If there is a typo, show the cell and Spellcheck it
                    If Not Application.CheckSpelling(.Text) Then
                        CheckSheet.Visible= xlSheetVisible
                        CheckSheet.Activate
                        .Select
                        .Show
                        DoEvents
                        'This next line is to fix a bug when checking a single cell
                        CheckSheet.Range(.MergeArea.Address & ", " & .MergeArea.Address) _
                        .CheckSpelling
                    End If
                End With
            Next CheckCell
            CheckSheet.Visible= SheetVisible
        End If
        'Tidy up the Loop
        Set CheckRange = Nothing
    Next CheckSheet
    'Same message as normal Spellcheck
    MsgBox "Spell check complete.  You're good to go!", vbExclamation
End Sub

(Note the fix for a bug where checking a Single Cell will instead check the entire Sheet)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM