I have tried following VBA code, where I want to run this code for all available worksheets in active workbook, I think I am making small mistake and as I am beginner I am not able to find it out, please help to fix it up
Sub ProtectFormulas()
Dim strPassword As String
Dim ws As Worksheet
For Each ws In Sheets
ws.Activate
.Unprotect
.Cells.Locked = False
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
.Cells.SpecialCells(xlCellTypeFormulas).FormulaHidden = True
.Protect AllowDeletingRows:=True
strPassword = 123456
ActiveSheet.Protect Password:=strPassword
Next ws
End With
End Sub
Any help would be appriciated by word of thanks.
There are 3 issues with your code:
There is no With block.
The following 2 lines will error if there is no formula in one of the sheets:
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
.Cells.SpecialCells(xlCellTypeFormulas).FormulaHidden = True
Because if there is no formula then .Cells.SpecialCells(xlCellTypeFormulas) is Nothing and therefore nothing has no .Locked and no .FormulaHidden methods.
You mix using Sheets and Worksheets. Note that those are not the same!
Sheets is a collection of all type of sheets (worksheets, chart sheets, etc)
Worksheets is a collection of only type worksheet
If you declare Dim ws As Worksheet and there is for example a chart sheet in your file, then For Each ws In Sheets will error because you try to push a chart sheet into a variable ws that is defined as Worksheet and cannot contain a chart sheet. Be as specific as possible and use Worksheets whenever possible in favour of Sheets.
The following should work:
Option Explicit
'if this is not variable make it a constant and global so you can use it in any procedure
Const strPassword As String = "123456"
Sub ProtectFormulas()
'Dim strPassword As String
'strPassword = "123456" 'remove this here if you made it global
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws
.Activate 'I think this is not needed
.Unprotect Password:=strPassword 'unprotect probably needs your password too or this will not work once the worksheet was protected.
.Cells.Locked = False
Dim FormulaCells As Range
Set FormulaCells = Nothing 'initialize (because we are in a loop!)
On Error Resume Next 'hide error messages (next line throws an error if no forumla is on the worksheet
Set FormulaCells = .Cells.SpecialCells(xlCellTypeFormulas)
On Error Goto 0 ' re-enable error reporting! Otherwise you won't see errors if they occur!
If Not FormulaCells Is Nothing Then 'check if there were formulas to prevent errors if not
FormulaCells.Locked = True
FormulaCells.FormulaHidden = True
End If
.Protect AllowDeletingRows:=True, Password:=strPassword
End With
Next ws
End Sub
This macro worked in Excel 2013, but now that I have updated to 2016 it is no longer working. It is meant to lock cells across multiple worksheets in a workbook if they have been filled in.
Private Sub Workbook_BeforeSave()
'Resume to next line if any error occurs
On Error Resume Next
Dim WS_Count As Integer
Dim I As Integer
Dim Cell As Range
'Set WS_Count equal to the number of worksheets in the active workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
'loop through all of the Worksheets
For I = 1 To WS_Count
With ActiveWorkbook.Worksheets(I)
'first of all unprotect the entire sheet and unlock all cells
.Unprotect Password:="open"
.Cells.Locked = False
'Now search for non blank cells and lock them
'unlock blank cells
For Each Cell In ActiveWorkbook.Worksheets(I).UsedRange
If Cell.Value > "" Then
Cell.Locked = True
Else
Cell.Locked = False
End If
Next Cell
'Now protect the entire sheet
.Protect Password:="open"
End With
Next I
Exit Sub
End Sub
When On Error Resume Next is removed it is erroring out on Cell.Locked = True.
In Excel 2016, the workbook_BeforeSave method requires additional, non-optional parameters
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
You need to include these in your method's declaration, even if your code ignores them.
I solved the issue Noob Mistake I had the macro in the module rather than the workbook.
I need to show comments in certain merged cells that are unlocked.
I found code online to show all comments in a workbook. This will not work because my workbook needs to be protected, which means the macro will look at locked cells and end prematurely.
Sub Show_Comment()
On Error Resume Next
For Each ws In ActiveWorkbook.Sheets
Set allCommentRng = ws.Cells.SpecialCells(xlCellTypeComments)
For Each Rng In allCommentRng
Rng.Comment.Visible = True
Next
Next
On Error GoTo 0
End Sub
I need to know how to specify a merged range that has a comment. The first merged range is "C7:C8-E7:E8". If I know how to do one range, I will figure our how to do the others.
A picture of what the cell looks like as well as the button:
This not for school.
You must first unprotect worksheet before trying to manipulate it and after changes have been applies, restore protection.
For Each ws In ActiveWorkbook.Sheets
ws.Unprotect
Set allCommentRng = ws.Cells.SpecialCells(xlCellTypeComments)
For Each Rng In allCommentRng
Rng.Comment.Visible = True
Next Rng
ws.Protect
Next ws
Example above implies no password protection. To overcome password protection for worksheets use:
ws.Unprotect Password:="yourpasswordhere"
ws.Protect Password:="yourpasswordhere"
How about if you unprotect the worksheet before you run the macro and re-protect if after:
Sub Show_Comment()
On Error Resume Next
For Each ws In ActiveWorkbook.Sheets
ws.unprotect
Set allCommentRng = ws.Cells.SpecialCells(xlCellTypeComments)
For Each Rng In allCommentRng
Rng.Comment.Visible = True
Next Rng
ws.protect
Next ws
On Error GoTo 0
End Sub
Is there something wrong with my formula? I'm trying to use a formula to bring me to gotoreference(I.e f5) the cell to verify that cells are indeed right before proceeding or either msgbox prompting to proceed or that the workbook/worksheet cannot be found. Also sometimes the person leaves blank because it is quarterly data, I would like it to autoextract latest data (farthest) column.
I've two workbooks: one is my current workbook (Currentworkbook.xlsx) that I'm running the macro on. The other is Jedata.xlsx of 'Mysheettab' and few other workbooks not listed here, will be feeding information into Currentworkbook.xlsx of 'Sheet1'.
Option explicit
Sub Macro3()
' Macro3 Macro
'
' Keyboard Shortcut: Ctrl+q
'
Dim wb As Workbook
Dim ws As Worksheet
On Error Resume Next
Set wb = ActiveWorkbook("Jedata")
Set ws = ActiveWorkbook.Sheets("Mysheettab")
On Error GoTo 0
If ws Is Nothing Then
MsgBox "Data sheet not found"
Else
Activate.Windows ("wb")
Sheets("ws").Select
Application.Goto Reference:=Range("AG28:AG32").Select
Selection.Copy
Windows("Currentworkbook").Activate
Selection.Copy
Range("H10:H14").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub
I can see lot of errors in your code.
First things first. You avoid the use of .Activate/.Select. INTERESTING READ
Next regarding Activate.Windows ("wb"). Anything between the " will be considered as a string. I think you wanted to try
wb.Activate
But like I mentioned, you should avoid the use of .Activate/.Select. Your code can be written as (UNTESTED)
Sub Macro3()
Dim wb As Workbook, thiswb As Workbook
Dim ws As Worksheet, thisws As Worksheet
On Error GoTo Whoa
Set thiswb = thisowrkbook
Set thisws = thiswb.ActiveSheet
Set wb = Workbooks("Jedata")
Set ws = wb.Sheets("Mysheettab")
ws.Range("AG28:AG32").Copy thisws.Range("H10")
Application.CutCopyMode = False
Exit Sub
Whoa:
MsgBox Err.Description
End Sub
A common request in the online forums is for code to identify the unlocked cells within a sheet.
The standard solutions use a loop to iterate through each cell in the used portion of the active worksheet, testing each cell determine if it is locked or not. A code sample for this approach is listed below.
Given the inherent poor performance in looping through cell ranges what superior approaches are possible?
(NB: I do intend to add my own existing approach which was previously hosted on another forum as a potential approach - but I will accept another [suitable] method as the answer if it is provided)
Range Approach to identify unlocked cells
Sub SelectUnlockedCells()
`http://www.extendoffice.com/documents/excel/1053-excel-identify-select-locked-cells.html
Dim WorkRange As Range
Dim FoundCells As Range
Dim Cell As Range
On Error GoTo SelectUnlockedCells_Error
Set WorkRange = ActiveSheet.UsedRange
For Each Cell In WorkRange
If Cell.Locked = False Then
If FoundCells Is Nothing Then
Set FoundCells = Cell
Else
Set FoundCells = Union(FoundCells, Cell)
End If
End If
Next Cell
If FoundCells Is Nothing Then
MsgBox "All cells are locked."
Else
FoundCells.Select
End If
On Error GoTo 0
Exit Sub
SelectUnlockedCells_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
SelectUnlockedCells of Module Module1"
End Sub
Using SpecialCells to quickly identify unlocked cells
The code below - QuickUnlocked - uses a workaround to quickly generate a SpecialCells collection of error cells to identify the unlocked cell range.
The key code steps are:
Alter the Application to suppress errors, code and screenupdating
Attempt to unlock the ActiveWorkbook and/or the ActiveSheet if they are protected. Exit the code if unsuccessful
Make a replica of the current sheet
Delete any existing formula errors in the replica using SpecialCells
Protect the replica worksheet and with the coverage of error handling, add a deliberate formula error that will only populate the unlocked cells
Clean up and report the results Reset the Application settings
Warning that SpecialCells is restricted to 8192 Areas prior to Xl2010
As per this Microsoft KB article, Excel-2007 and earlier versions supports up to a maximum of 8,192 non-contiguous cells through VBA macros. Rather surprisingly, applying a VBA macro to more than 8192 SpecialCells Areas in these Excel versions, will not raise an error message, and the entire area under consideration will be treated as being part of theSpecialCells` range collection.
Quick Unlocked code
Sub QuickUnlocked()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim lCalc As Long
Dim bWorkbookProtected As Boolean
On Error Resume Next
'test to see if WorkBook structure is protected
'if so try to unlock it
If ActiveWorkbook.ProtectStructure Then
ActiveWorkbook.Unprotect
If ActiveWorkbook.ProtectStructure Then
MsgBox "Sorry, I could not remove the passsword protection from the workbook" _
& vbNewLine & "Please remove it before running the code again", vbCritical
Exit Sub
Else
bWorkbookProtected = True
End If
End If
Set ws1 = ActiveSheet
'test to see if current sheet is protected
'if so try to unlock it
If ws1.ProtectContents Then
ws1.Unprotect
If ws1.ProtectContents Then
MsgBox "Sorry, I could not remove the passsword protection from sheet" & vbNewLine & ws1.Name _
& vbNewLine & "Please remove it before running the code again", vbCritical
Exit Sub
End If
End If
On Error GoTo 0
'disable screenupdating, event code and warning messages.
'set calculation to manual
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
lCalc = .Calculation
.Calculation = xlCalculationManual
End With
On Error Resume Next
'check for existing error cells
Set rng1 = ws1.Cells.SpecialCells(xlCellTypeFormulas, 16)
On Error GoTo 0
'copy the activesheet to a new working sheet
ws1.Copy After:=Sheets(Sheets.Count)
Set ws2 = ActiveSheet
'delete any cells that already contain errors
If Not rng1 Is Nothing Then ws2.Range(rng1.Address).ClearContents
'protect the new sheet
ws2.Protect
'add an error formula to all unlocked cells in the used range
'then use SpecialCells to read the unlocked range address
On Error Resume Next
ws2.UsedRange.Formula = "=NA()"
ws2.Unprotect
Set rng2 = ws2.Cells.SpecialCells(xlCellTypeFormulas, 16)
Set rng3 = ws1.Range(rng2.Address)
ws2.Delete
On Error GoTo 0
'if WorkBook level protection was removed then reinstall it
If bWorkbookProtected Then ActiveWorkbook.Protect
'cleanup user interface and settings
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
lCalc = .Calculation
End With
'inform the user of the unlocked cell range
If Not rng3 Is Nothing Then
MsgBox "The unlocked cell range in Sheet " & vbNewLine & ws1.Name & " is " & vbNewLine & rng3.Address(0, 0)
Else
MsgBox "No unlocked cells exist in " & ws1.Name
End If
End Sub
Well, I've gone back to a loop, but I think this method is efficient because it only references those cells which are Unlocked (without selecting) using Next:
If the object is a range, this property emulates the TAB key, although
the property returns the next cell without selecting it.
On a protected sheet, this property returns the next unlocked cell. On
an unprotected sheet, this property always returns the cell
immediately to the right of the specified cell.
It stores the first (Next) Range.Address, loops through the others until it returns to this first one.
Sub GetUnlockedCells_Next()
Dim ws As Worksheet
Dim strFirst As String
Dim rngNext As Range
Dim strLocked As String
Set ws = Worksheets(1)
ws.Protect
Set rngNext = ws.Range("A1").Next
strFirst = rngNext.Address
Do
strLocked = strLocked & rngNext.Address & ","
Set rngNext = rngNext.Next
Loop Until rngNext.Address = strFirst
strLocked = Left(strLocked, Len(strLocked) - 1) 'remove the spare comma
ws.Range(strLocked).Select
ws.Unprotect
MsgBox strLocked
End Sub
Use Conditional Formatting with:- Use a formula to determine which cells to format, Format values where this formula is true: =CELL("protect",A1)=0 and Format of choice applied to occupied range?
I was looking for a way to clear the contents of my unlocked cells. The problem was that my sheet has hundreds, if not thousands, of unlocked cells and twice as many locked ones. Iterating through them was taking about 5-7 seconds and I wanted something more efficient.
brettdj's solution got me half way there, but having so many cells in my range broke the algorithm.
The line
Set rng3 = ws1.Range(rng2.Address)
Was not working because rng2's address was over the 256 character limit, so rng3 became "nothing".
I spent hours trying to work around the 256 limit but got nowhere. After almost giving up, I stumbled upon the "areas" object of a range. Life saver!
The adjusted code below works with sheets that have several unlocked cells. Thanks to brettdj for the original idea.
' Sub to clear unlocked cells.
Sub clearUnlockedCells()
On Error Resume Next
' If the Workbook is protected, unlock it.
Dim workbook_protected As Boolean
If ActiveWorkbook.ProtectStructure Then
workbook_protected = True
ActiveWorkbook.Unprotect
' If we failed to unlock the Workbook, error out and exit.
If ActiveWorkbook.ProtectStructure Then
MsgBox "Sorry, I could not remove the passsword protection from the workbook" _
& vbNewLine & "Please remove it before running the code again", vbCritical
Exit Sub
End If
End If
Dim source_sheet As Worksheet
Set source_sheet = ActiveSheet
' If the Worksheet is protected, unlock it.
Dim worksheet_protected As Boolean
If source_sheet.ProtectContents Then
worksheet_protected = True
source_sheet.Unprotect
' If we failed to unlock the Worksheet, error out and exit.
If source_sheet.ProtectContents Then
MsgBox "Sorry, I could not remove the passsword protection from sheet" & vbNewLine & source_sheet.name _
& vbNewLine & "Please remove it before running the code again", vbCritical
Exit Sub
End If
End If
On Error GoTo 0
' Disable screenupdating, event code and warning messages.
' Store the calculation and set it to manual.
Dim calc As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
calc = .Calculation
.Calculation = xlCalculationManual
End With
On Error Resume Next
' Check for existing error cells.
Dim tmp_rng As Range
Set tmp_rng = source_sheet.Cells.SpecialCells(xlCellTypeFormulas, 16)
On Error GoTo 0
' Copy the ActiveSheet to a new working sheet.
source_sheet.Copy After:=Sheets(Sheets.Count)
Dim tmp_sheet As Worksheet
Set tmp_sheet = ActiveSheet
' Delete any cells that already contain errors.
If Not tmp_rng Is Nothing Then tmp_sheet.Range(tmp_rng.Address).ClearContents
' Protect the new sheet and add an error formula to all unlocked cells in the
' used range, then use SpecialCells to read the unlocked range address.
tmp_sheet.Protect
On Error Resume Next
tmp_sheet.UsedRange.Formula = "=NA()"
tmp_sheet.Unprotect
' Get the range of cells with "=NA()" in them.
Set tmp_rng = tmp_sheet.Cells.SpecialCells(xlCellTypeFormulas, 16)
' Iterate through the range and create a mirror of that range in the source sheet.
Dim area As Range
Dim source_sheet_range As Range
Dim unlocked_cells As Range
For Each area In tmp_rng.Areas
Set source_sheet_range = source_sheet.Range(area.Address)
If unlocked_cells Is Nothing Then
Set unlocked_cells = source_sheet_range
Else
Set unlocked_cells = Union(unlocked_cells, source_sheet_range)
End If
Next area
' Delete the temp sheet.
tmp_sheet.Delete
On Error GoTo 0
' Protect the Workbook and Worksheet as necessary.
If workbook_protected Then ActiveWorkbook.Protect
If worksheet_protected Then source_sheet.Protect
' Cleanup user interface and settings.
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
.Calculation = calc
End With
' Clean up the unlocked cells.
unlocked_cells.ClearContents
End Sub
Hope that helps someone else. If you just want to select them instead of clearing them, then change the second to last line from .ClearContents to .Select.
Here's a general solution that is much faster than looping through ranges of cells and is much simpler, more straightforward, than cloning temporary worksheets, etc. It is relatively fast because it takes advantage of the high-speed compiled code in which Excel VBA's Find method is implemented.
Function GetUnlockedCells(SearchRange As Range) As Range 'Union
'
'Finds all unlocked cells in the specified range and returns a range-union of them.
'
'AUTHOR: Peter Straton
'
'*************************************************************************************************************
Dim FoundCell As Range
Dim FirstCellAddr As String
Dim UnlockedUnion As Range
'NOTE: When finding by format, you must first set the FindFormat specification:
With Application.FindFormat
.Clear
.Locked = False 'This is the key to this technique
End With
'NOTE: Unfortunately, the FindNext method does not remember the SearchFormat:=True specification so it is
'necessary to capture the address of the first cell found, use the Find method (instead) inside the find-next
'loop and explicitly terminate the loop when the first-found cell is found a second time.
With SearchRange
Set FoundCell = .Find(What:="", After:=.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=True)
If Not FoundCell Is Nothing Then
FirstCellAddr = FoundCell.Address
Do
' Debug.Print FoundCell.Address
If UnlockedUnion Is Nothing Then
Set UnlockedUnion = FoundCell.MergeArea 'Include merged cells, if any
Else
Set UnlockedUnion = Union(UnlockedUnion, FoundCell.MergeArea) ' "
End If
Set FoundCell = .Find(What:="", After:=FoundCell, SearchDirection:=xlNext, SearchFormat:=True)
Loop Until FoundCell.Address = FirstCellAddr
End If
End With
Application.FindFormat.Clear 'Cleanup
Set GetUnlockedCells = UnlockedUnion
End Function 'GetUnlockedCells
I was exploring this but I've come full-circle to, more or less, Brett's approach. The slight difference is that I use the current worksheet rather than creating a new one. I'm also initially assuming that there are no errors in the worksheet. (Code could be added similar to Brett's to account for these.)
I wanted to flood the UsedRange with "#N/A", ignore errors, and use Application.Undo to quickly get back. Unfortunately, I couldn't use Undo (unlike in Word). So I resorted to using a Variant to grab the whole area's data, and then re-insert it.
Sub GetUnlockedCells()
Dim ws As Worksheet
Dim rngUsed As Range
Dim varKeep As Variant
Application.ScreenUpdating = False
Set ws = Worksheets(1)
ws.Protect
Set rngUsed = ws.UsedRange
varKeep = rngUsed.Value
On Error Resume Next
rngUsed.Value = "#N/A"
On Error GoTo 0
ws.Unprotect
MsgBox "Unlocked cells are " & _
rngUsed.SpecialCells(xlCellTypeConstants, xlErrors).Address
rngUsed.Value = varKeep
Application.ScreenUpdating = True
End Sub
So, unfortunately, I haven't advanced much beyond Brett's cool code. Maybe it will inspire someone else, or someone might discover a way to use Undo ;)
I'm also losing formulas as well (converted to values) so some work required!
If there are lots of formulas, general approach is
For each row in ...
lockedR = row.locked
for each cell in row
if isnull(lockedR) then ' inconsistent in row
locked = cell.locked
else
locked = lockedR ' consistent from row, no need to get it.
This pattern works fine for many properties such as HasArray. But just for Locked it is grossly (100 times) slower. Don't know why so inefficient.
Goto Special would be a cute trick, but there isn't one for locked cells.
A good solution would be wonderful but I suspect impossible.