Excel VBA - sheet being protected too quickly

ryansin

I have a VBA macro that loops through cells whose values have been changed and amends the values of other cells in the same row.

Public Sub Worksheet_Change (ByVal Target As Range)
    Dim r As Integer
    Application.ScreenUpdating = false
    ' Unprotect the sheet
    ActiveSheet.Unprotect("password")

    Set newRange = Range("K:K")

    If Not Application.Intersect(newRange, Range(Target.Address)) Is Nothing Then
        For Each cell in Target.Cells       
            ' Change the values of cells in the same row
            r = cell.Row

            Cells(r, 2).Value = "New Value"
            Cells(r, 3).Value = "New Value"   ' Debug highlights this line
            Cells(r, 4).Value = "New Value"
            Cells(r, 5).Value = "New Value"

        Next


    End If

    ' Reprotect the sheet
    ActiveSheet.Protect Password:="password", AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, _
        AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    Application.ScreenUpdating = True

End Sub

Without unprotecting/reprotecting the sheet the macro works fine, but when added it generates a Runtime Error Application-Defined or Object-Defined error, but not before changing the first cell value Cells(r, 2).Value = "New Value".

I can only assume that this is because the sheet is unprotected at the start and the first cell value change completes before the sheet is then locked (perhaps running in a separate thread to the For loop?). The macro then errors at the following line because it is trying to make a change to a protected sheet.

How can I fix this and prevent the sheet from locking too quickly?

Gary's Student

You are changing cells with the event macro.

You must:

Application.EnableEvents = False

before the For loop and

Application.EnableEvents = True

after the For loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Specifying User Permissions in VBA for Protected Excel Sheet

From Dev

How to disable save and protected sheet prompt in excel using VBA

From Dev

VBA editing cells in protected sheet

From Dev

Excel protected sheet BeforeDoubleClick event

From Dev

vba excel copy only visible cells on key press ctrl+c for protected sheet

From Dev

Autofilter a protected excel sheet opened from access

From Dev

Autofilter a protected excel sheet opened from access

From Dev

Quickly delete all floating picture in Excel sheet

From Dev

Renaming sheet in Excel with reference to another sheet, VBA

From Dev

Excel VBA to add weekly sheet?

From Dev

Excel VBA sheet activation required?

From Dev

Got a loop in Excel sheet vba

From Dev

excel vba sort sheet numerically

From Dev

Excel VBA: reference to prev Sheet

From Dev

Using VBA to unprotect a sheet that has been protected with UserInterface Only = true

From Dev

VBA Copy Sheet.PROTECTED to new workbook ISSUE

From Dev

Remove protected view from Excel sheet opened programmatically in Access

From Dev

How to enable Cell re-size on protected Excel Sheet

From Dev

Excel: Find out who password protected a individual sheet on book

From Dev

VBA Excel add new sheet with number based on the previous sheet created

From Dev

Verify sheet name before using the excel sheet in vba

From Dev

Excel vba find text in sheet, copy range, paste to other sheet

From Dev

Excel VBA Create a new sheet with data from the last sheet

From Dev

Excel: change VBA action frome same sheet to another sheet

From Dev

How to disable add sheet button in Excel with VBA?

From Dev

Using a variable to select an Excel sheet in vba

From Dev

Excel VBA, Code name appearing with sheet name

From Dev

Excel Vba stops excution after deleting sheet

From Dev

Excel VBA select entire sheet using Range

Related Related

  1. 1

    Specifying User Permissions in VBA for Protected Excel Sheet

  2. 2

    How to disable save and protected sheet prompt in excel using VBA

  3. 3

    VBA editing cells in protected sheet

  4. 4

    Excel protected sheet BeforeDoubleClick event

  5. 5

    vba excel copy only visible cells on key press ctrl+c for protected sheet

  6. 6

    Autofilter a protected excel sheet opened from access

  7. 7

    Autofilter a protected excel sheet opened from access

  8. 8

    Quickly delete all floating picture in Excel sheet

  9. 9

    Renaming sheet in Excel with reference to another sheet, VBA

  10. 10

    Excel VBA to add weekly sheet?

  11. 11

    Excel VBA sheet activation required?

  12. 12

    Got a loop in Excel sheet vba

  13. 13

    excel vba sort sheet numerically

  14. 14

    Excel VBA: reference to prev Sheet

  15. 15

    Using VBA to unprotect a sheet that has been protected with UserInterface Only = true

  16. 16

    VBA Copy Sheet.PROTECTED to new workbook ISSUE

  17. 17

    Remove protected view from Excel sheet opened programmatically in Access

  18. 18

    How to enable Cell re-size on protected Excel Sheet

  19. 19

    Excel: Find out who password protected a individual sheet on book

  20. 20

    VBA Excel add new sheet with number based on the previous sheet created

  21. 21

    Verify sheet name before using the excel sheet in vba

  22. 22

    Excel vba find text in sheet, copy range, paste to other sheet

  23. 23

    Excel VBA Create a new sheet with data from the last sheet

  24. 24

    Excel: change VBA action frome same sheet to another sheet

  25. 25

    How to disable add sheet button in Excel with VBA?

  26. 26

    Using a variable to select an Excel sheet in vba

  27. 27

    Excel VBA, Code name appearing with sheet name

  28. 28

    Excel Vba stops excution after deleting sheet

  29. 29

    Excel VBA select entire sheet using Range

HotTag

Archive