I used the code from here.
Sub lock_sheets
lock_nonempty_cells(0)
lock_nonempty_cells(1)
End Sub
Sub lock_nonempty_cells(shindex As Integer)
Dim sh, oFilled, oBlanks As Object
Dim iWhat As Integer
Const cPWD = ""
dim cp_locked as new com.sun.star.util.CellProtection
cp_locked.islocked = true
dim cp_unlocked as new com.sun.star.util.CellProtection
cp_unlocked.islocked = false
sh=ThisComponent.Sheets.getByIndex(shindex)
sh.unProtect(cPWD)
With com.sun.star.sheet.CellFlags
iWhat= .VALUE + .DATETIME + .STRING + .FORMULA
End With
oFilled = sh.queryContentCells(iWhat)
oBlanks = sh.queryEmptyCells()
' isLocked is read only isLocked=true will not work. New object needs to be assigned to CellProtection
oBlanks.CellProtection = cp_unlocked
oFilled.CellProtection = cp_locked
sh.protect(cPWD)
End Sub
The results:
Sheet(0) is perfect, all blank cells can be modified as required.
All cells with “Hello !” are locked as required.
But in Sheet(1), why can’t some blank cells ( C2:I5, A26:E35, J4:J35 and K2:XXXX35) be modified ?