I have a piece of Excel VBA code that works OK if the worksheet is unprotected but does not when protected. I have read the previous similar queries but do not think they apply in this case - the suggested fixes were not applicable or required repeated unprotecting/reprotecting the worksheet which I would like to avoid if possible.
My code looks like this:
Dim NonEmptyCells As Range
Dim Cell As Range
On Error Goto Fin
Set NonEmptyCells = Range("D3:H6").SpecialCells(xlCellTypeBlanks)
For Each Cell In NonEmptyCells
' do stuff with cell
Next Cell
Fin:
On Error GoTo 0
The use of 'On Error' is to avoid the loop when there are zero empty cells found.
The Range(D3:H6)
has cell formatting set so the cells are not locked and not hidden.
When the worksheet is not protected the specialcells line correctly returns the range of blank cells, however when protected special cells always errors as if no empty cells are found.
All help appreciated.
SpecialCells
and useIf Not cell.Value = vbNullString Then
at the start of your loop? – cybernetic.nomadRange
you want and inspecting cells'Value
(given there aren't that many cells to iterate)? I have read the previous similar queries but do not think they apply in this case - why is that? Please edit your post. – Mathieu Guindon