Sub Macro3()
'
'
Const MySecretPassword = "Hello"
If ActiveSheet.Range("J49") Is Nothing Then Exit Sub
On Error GoTo Protect
If ActiveSheet.Range(J49).Value = "Password" Then
ActiveSheet.Range("A1:R37").Locked = True
Else
ActiveSheet.Range("A1:R37").Locked = False
End If
Protect:
ActiveSheet.Protect MySecretPassword
End Sub
I also tried:
Sub Macro4()
'
' Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.Target = Range("J49") Then
If ActiveSheet.Target.Value = "Password" Then
ActiveSheet.Unprotect Password = "Hello"
Else
ActiveSheet.Protect Password = "Hello", DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End If
End Sub
and
Sub Macro5()
'
' Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("J49") Is Nothing Then
ActiveSheet.Unprotect
If Target.Value = "Pascal" Then
Target.Offset(0, 1).Locked = False
Else
Target.Offset(0, 1).Locked = True
End If
ActiveSheet.Protect
End If
'
'
End Sub
I'm trying to make an Excelfile with different sheets, in which I want to lock a certain range of cells when Cell J49 is filled with Password. I have browsed the forum to see solutions, but I'm struggling to get it to work. 1 person can be in charge for this file, that's why I really want the password protection. Can anyone point my mistake out?
Try something like that
Sub pass_test()
sheet_password = "secret"
range_password = "Pascal"
target_value = ActiveSheet.Range("J49").Value
target_address = "A1:B2"
If target_value <> "" Then
ActiveSheet.Unprotect Password:=sheet_password
If target_value = range_password Then
ActiveSheet.Range(target_address).Locked = False
Else
ActiveSheet.Range(target_address).Locked = True
End If
ActiveSheet.Protect Password:=sheet_password
End If
End Sub
Related
I haven't used VBA before so I'm really new to this :-) The below is the code I am currently using , and simply need to lock all area's of the sheet (with out using the sheet name) apart from A13:A377, B1, D3:D4, D13:D377, F13:I377. I can't protect the sheet because the VBA won't work. Help please...
Private Sub Worksheet_Change(ByVal Target As Range)
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 1 Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & " & " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
unlock cells and protect sheet
I don't see any relation between your description and the code you have shared! Please find below a proposal to unlock a union of cells and protect the sheet (without password!)
Option Explicit
Sub UnlockCells_and_Protect()
Dim actSheet As String
actSheet = "Sheet2" ' choose whatever you need
'actSheet = ActiveSheet.Name
'actSheet = Sheets(3).Name
'actsheet = "SpecialSheet"
Call UnprotectSheet(actSheet)
Call LockAll(actSheet)
Call UnlockRange(actSheet, "A13:A377,B1,D3:D4,D13:D377,F13:I377")
Call ProtectSheet(actSheet)
End Sub
Sub UnlockRange(sheetName As String, RangeReference As String)
With Sheets(sheetName).Range(RangeReference)
.Locked = False
.FormulaHidden = False
'you might want to mark the unlocked cells for debugging
Sheets(sheetName).Range(RangeReference).Interior.Color = vbYellow
End With
End Sub
Sub ProtectSheet(sheetName As String)
Sheets(sheetName).Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Sub ProtectActiveSheet()
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Sub UnprotectSheet(sheetName As String)
Sheets(sheetName).Unprotect
End Sub
Sub UnprotectActiveSheet()
ActiveSheet.Unprotect
End Sub
Sub LockAll(sheetName As String)
Sheets(sheetName).Cells.Locked = True
Sheets(sheetName).Cells.FormulaHidden = False
'if you marked the unlocked cells yellow you change
'them back to white with lock/unlock all
Sheets(sheetName).Cells.Interior.Color = vbWhite
End Sub
Sub UnlockAll(sheetName As String)
Sheets(sheetName).Cells.Locked = False
Sheets(sheetName).Selection.FormulaHidden = False
'if you marked the unlocked cells yellow you change
'them back to white with lock/unlock all
Sheets(sheetName).Cells.Interior.Color = vbWhite
End Sub
I have a spreadsheet where I have adapted two pieces of VBA code to perform two different double click event actions.
The 1st piece of code enters a "✓" in a specific range of cells when double clicked and removes it when double clicked again:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("H2:H600,M2:V600")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
End Sub
The 2nd piece of code enters a date/time stamp in a range of cells when double clicked:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Coded by SunnyKow - 16/09/2016
Application.EnableEvents = False
On Error GoTo ErrorRoutine
'You can change the range here
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
'Update only if cell is empty
If Target = "" Then
Target = Now
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
Because you cannot have two double click events in single worksheet (as separate VBA code), how do I merge these two pieces of VBA so that it is a single piece of code with two distinct actions based on the cell range selected. Would appreciate any help to resolve this.
It looks like an if statement will do the trick
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
On Error GoTo ErrorRoutine
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
'Update only if cell is empty
If Target = "" Then
Target = Now
End If
Cancel = True
ElseIf Not Intersect(Target, Range("H2:H600,M2:V600")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
On Error GoTo ErrorRoutine
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
If Target = "" Then
Target = Now
End If
Cancel = True
ElseIf Not Intersect(Target, Range("M2:V600")) Is Nothing Then
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
ElseIf Not Intersect(Target, Range("H2:H600")) Is Nothing Then
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
Target.Offset(0, 18) = Now
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
what is wrong with this code?
Private Sub Worksheet_Change(ByVal Target As Range)
If IsEmpty(Sheet5.Range("A26").Value) = True Then
Sheet5.Rows("26:27").EntireRow.Hidden = True
ElseIf IsEmpty(Sheet5.Range("A26").Value) = False Then
Sheet5.Rows("26:27").EntireRow.Hidden = False
End If
End Sub
Can you please try it?
If IsEmpty(Worksheets("Sheet5").Range("A26").Value) = True Then
Worksheets("Sheet5").Rows("26:27").EntireRow.Hidden = True
ElseIf IsEmpty(Worksheets("Sheet5").Range("A26").Value) = False Then
Worksheets("Sheet5").Rows("26:27").EntireRow.Hidden = False
End If
Useful link:
https://learn.microsoft.com/en-us/office/vba/api/excel.range(object)
I found an answer!
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
If Sheet5.Range("A26").Value = "" Then
Sheet5.Rows("26:27").EntireRow.Hidden = True
Else
Sheet5.Rows("26:27").EntireRow.Hidden = False
End If
End Sub
Application.EnableEvents = True
I had to change
Private Sub Worksheet_Change(ByVal Target As Range)
to
Private Sub Worksheet_Calculate()
I needed to use calculate because the "" is created by formula therefore a calculation.
I want to know my error in my VBA code in my Excel and want some cells to be blocked if a another cell value is true.
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.Cells(35, "CD").Value = True Then
ActiveSheet.Range("R29:AA38").Locked = True
Else
ActiveSheet.Range("R29:AA38").Locked = False
End If
End Sub
can you help me with that please!
Try this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Ws As Worksheet
Set Ws = Target.Worksheet
Ws.Unprotect "pw" ' change to your password
If CBool(Ws.Cells(35, "CD")) = True Then
If MsgBox("Do you want to Lock the cells", vbYesNo) = vbYes Then
Ws.Range("R29:AA38").Locked = True
else
Ws.Range("R29:AA38").Locked = False ' delete this line if you don't need it
Application.EnableEvents = False
Ws.Range("R29:AA38").ClearContents
Application.EnableEvents = True
end if
else
Ws.Range("R29:AA38").Locked = False
end if
Ws.Protect "pw" ' change to your password
End Sub
I have tried a lot of variations for this ( all found online, since I am a novice on this) and cant' get it to work. Where am I going wrong?
I have a spreadsheet with 2 cells that can be changed with drop down, for each cell I need a different sub to activate on change. I can make each of these work individually, but not on combining them.
Can you help, please?
This is what I have so far:
Private Sub Worksheet_Change(ByVal Target As Range)
'hides currencies that aren't required
'On Error GoTo 99
If Not Intersect(Target, Range("B8")) Is Nothing Then
Columns("F:F").EntireColumn.Hidden = False
Columns("E:E").EntireColumn.Hidden = False
Application.EnableEvents = False
If Range("B8").Text = "" Then
Columns("F:F").EntireColumn.Hidden = True
Columns("E:E").EntireColumn.Hidden = False
GoTo Letscontinue
Else
Columns("F:F").EntireColumn.Hidden = False
Columns("E:E").EntireColumn.Hidden = True
GoTo Letscontinue
End If: End If
Exit Sub
'adds new lines for addenda during contract live cycle
If Not Intersect(Target, Range("B168")) Is Nothing Then
If Range("B168").Text = "Yes" Then
Range("B172").Select
If Range("B172").Text = "Yes" Then
Range("B176").Select
If Range("B176").Text = "Yes" Then
Range("B180").Select
If Range("B176").Text = "Yes" Then
Else
ActiveCell.Offset(-2, 0).Rows("1:4").EntireRow.Select
Selection.Copy
ActiveCell.Offset(4, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown
ActiveCell.Offset(3, 1).Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-4]C+1"
Selection.ClearContents
ActiveCell.FormulaR1C1 = "No"
ActiveCell.Offset(1, 0).Range("A1").Select
GoTo Letscontinue
End If: End If: End If: End If: End If
Exit Sub
Letscontinue:
Application.EnableEvents = True
Exit Sub
99:
Resume Letscontinue
End Sub
A general template could resemble:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("B8")) Is Nothing Then
'
' your code
'
End If
If Not Intersect(Target, Range("B168")) Is Nothing Then
'
' your code
'
End If
Application.EnableEvents = True
End Sub
The reason this is failing is because you have an Exit Sub statement outside your first If block
GoTo Letscontinue
End If: End If
Exit Sub <----- Remove this
This Exit Sub statement will always be hit as it is outside of the If block, so this line of code
If Not Intersect(Target, Range("B168")) Is Nothing Then ....
will never be reached.
You should remove the Exit Sub at the bottom of your code as well. As this will mean that the Application.EnableEvents will not be hit.
Exit Sub <---- Remove this
Letscontinue:
Application.EnableEvents = True
Exit Sub