Auto Protecting cells & auto filling date

C

ccarmock

I wonder if anyone can tell me if it is possible to auto proect cells
after data has been entered?

Basically I have a spreadsheet that has two levels of protection - some
cells are password protected and others available for all to enter data
into.

What I would like to achieve is once data has been entered into a row
in the editable area that row becomes part of the password protected
area automatically.

Also is it possible to use the NOW (or similar) function to populate
one cell with a date when another is filled?

Ie a log entry is made in column B. When some data is entered into
column B cthe corresponding entry in column A gets filled with todays
date.

Any help much appreciated.

Regards
Clive
 
G

Guest

I can't answer the first part, but the second part is easy
just enter into a1 =IF(b1=0,0,NOW())
format a1 as a date
in tools/options uncheck show zeros
 
G

Guest

The only problem with NOW is that it always will reset to the day it is
opened or printed
 
C

ccarmock

Yes I see what you mean - so every time I open the sheet the dates ar
updated.....

If the first time it triggers it coudl replace the formula with th
actual value that woudl achieve what I need...
 
G

Gord Dibben

ccarmock

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'when entering data in a cell in Col B
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 2 Then
n = Target.Row
If Excel.Range("B" & n).Value <> "" Then
Excel.Range("A" & n).Value = Now
End If
End If
enditall:
Application.EnableEvents = True
End Sub

This is sheet event code.

Right-click on the sheet tab and "View Code".

Copy/paste the code into that module. As you enter data in column B, the date
will be placed in A on same row.


Gord Dibben Excel MVP
 
C

ccarmock

Hi - Many thanks that works perfectly!

I wonder if something similar could be used to modify the ranges used
to protect data in the worksheet?

Ie once data is entered into a cell in column B I woudl then like to
change that cell from being in a range that allows anyone to edit into
a range that is password protected.

Regards
Clive
 
C

ccarmock

One further question on this - when I lock the spreadsheet (as I don
want the date to be modified by the user, the system can no longe
update column A with the date of the change.

To achieve this I have column B set to allow Everyone to make change
while the rest of the sheet requires a password to edit ranges.

I guess I can get the Macro to turn off protection and turn it o
again? However would this mean storing the password in the Macro?
Again not an issue, but I would then need to be able to prevent th
user from looking at the Macro code to discover the password - is tha
possible?

Or alternatively is there the concept of allowing the 'SYSTEM
permission to update a range of cells, this might be a cleane
approach
 
G

Gord Dibben

ccarmock

Depending upon how sophisticated your users are and whether or not they enable
macros upon opening the workbook you can achieve most of what you want.

First, unlock columns A and B then protect the sheet with a password("justme")
is example only.

The Sub following will enter a date in column A and lock that cell in Column A
but leaving rest of column A unlocked.

B must remain unprotected in order for users to enter data.

Add the code below to the sheet module.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'when entering data in a cell in Col B
'if a date is in A, it won't change when B is updated
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 2 Then
SHEETUNPROTECT
N = Target.Row
If Excel.Range("B" & N).Value <> "" _
And Excel.Range("A" & N).Value = "" Then
Excel.Range("A" & N).Value = Now
Excel.Range("A" & N).Locked = True
End If
End If
enditall:
Application.EnableEvents = True
SHEETPROTECT
End Sub

Now copy the following macros to a new general module in the workbook.

Sub SHEETPROTECT()
ActiveSheet.Protect Password:="justme", DrawingObjects:=True, _
Contents:=True, Scenarios:=True
End Sub

Sub SHEETUNPROTECT()
ActiveSheet.Unprotect Password:="justme"
End Sub

Right-click on the workbook/project and select VBAProject Properties then
"Lock project for viewing"

You can password protect this module so prying eyes can't see it and obtain
the password("justme").

You now save the workbook and close Excel....the VBAProject locking won't come
into effect until Excel is closed out fully.

If the users enable macros and if no one of them knows how to crack a
VBAProject password, you should be good to go.


Gord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top