VBA to lock a sheet from changes

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I have the following code where if you change a percentage value in cell K1 it will calculate the upcoming years rates. Is there a way to add a toggle button that would disable the code from filling in the calculated fields as well as lock the values in I3:I54 so that no further changes can be made?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Application.ScreenUpdating = False
Application.CutCopyMode = False

Dim strFormulas(1 To 1) As Variant
Dim ws As Worksheet

Set ws = Worksheets("Sheet1")

If Not Intersect(Target, Range("K1")) Is Nothing Then
    
    If ws.Range("K1") <> "" Then
    
    With ThisWorkbook.Sheets("Sheet1")
        .Range("I3:I54").ClearContents
    
        strFormulas(1) = "=IFERROR(MROUND(H3+(H3*$K$1),5),"""")"
        
        .Range("I3").Formula = strFormulas
        .Range("I3").Copy
        .Range("I3:I54").PasteSpecial Paste:=xlPasteFormulas
        .Range("I3").Select
    End With
    
    Else
        ws.Range("I3:I54").ClearContents
    End If

End If

Application.ScreenUpdating = True
Application.CutCopyMode = True

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I usually put such "settings" in a worksheet called Control. I would also give a range name to the cell such as "DoUpdates". Wherever you put the toggle switch it would have a value of true or false. That is a good use of "Data Validation". Let's say that the toggle switch (cell) named DoUpdates is in Sheet 1.

VBA Code:
Dim bDoUpdate As Boolean

bDoUpdate = Thisworkbook.Worksheets("Sheet1").Range("DoUpdates").Value  '<= you could use a cell address here if desired.

If not bDoUpdate then Exit Sub

If needed there are lots of websites that explain range names. Very powerful!
 
Upvote 0
I usually put such "settings" in a worksheet called Control. I would also give a range name to the cell such as "DoUpdates". Wherever you put the toggle switch it would have a value of true or false. That is a good use of "Data Validation". Let's say that the toggle switch (cell) named DoUpdates is in Sheet 1.

VBA Code:
Dim bDoUpdate As Boolean

bDoUpdate = Thisworkbook.Worksheets("Sheet1").Range("DoUpdates").Value  '<= you could use a cell address here if desired.

If not bDoUpdate then Exit Sub

If needed there are lots of websites that explain range names. Very powerful!
Thanks for the answer OaklandJim!

I'm really sorry but kind of still figuring out where to put this code. Would it be within the code above or would I put it in a Module with it's own sub? I'd prefer to put this lock in on the 10 sheets for each location rather than a Control sheet (which ironically I already have :p). That way I can lock the individual locations each rather than them all.
 
Upvote 0
Thanks for the answer OaklandJim!

I'm really sorry but kind of still figuring out where to put this code. Would it be within the code above or would I put it in a Module with it's own sub? I'd prefer to put this lock in on the 10 sheets for each location rather than a Control sheet (which ironically I already have :p). That way I can lock the individual locations each rather than them all.
Just realized i'd put the code within the ToggleButton code.....ugghhhh
 
Upvote 0
VBA Code:
Sub LocationButton()
 
If Sheets("Location Forecast").Shapes("Button 6").TextFrame.Characters.Text = "Locked" Then
    ActiveSheet.Unprotect
    Sheets("Location Forecast").Shapes("Button 6").TextFrame.Characters.Text = "Unlocked"
    Call UnlockLocation
Else
    ActiveSheet.Unprotect
    Sheets("Location Forecast").Shapes("Button 6").TextFrame.Characters.Text = "Locked"
    Call LockLocation
    ActiveSheet.Range("K1").Select
End If
 
End Sub

Sub LockLocation()

    Sheets("Location Forecast").Range("K1,I3:I55").Locked = True
    ActiveSheet.Protect
    ActiveSheet.EnableSelection = xlUnlockedCells

End Sub

Sub UnlockLocation()

    Sheets("Location Forecast").Range("K1,I3:I55").Locked = False
    ActiveSheet.Protect
    ActiveSheet.EnableSelection = xlUnlockedCells
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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