Hi Si
Sorry about the delay getting back to you. It involved plumbers - let's just leave it at that!
Here's a bit of code I use for detecting when someone changes a particular cell on a sheet. The code needs to be placed not in a module, but in the sheet's own code area ie Sheet 1 (Name). These appear below the modules in the code window.
Code:
Private Sub Worksheet_Change(ByVal ChangedCell As Range)
If ChangedCell.Address = Sheets("Macros").[NamedCell].Address Then
Call Initialise
End If
End Sub
So the code is an 'event' rather than a normal Sub. It executes when something happens (Worksheet... Change) rather than being called.
So 'ChangedCell' is a cell that just been changed. I compare it with a particular named range (a cell) that I am monitoring called "NamedCell".
Some things that might help you:
You can say:
x=ChangedCell.Column
x=ChangedCell.Row
x=ChangedCell.Address
x=ChangedCell.Count etc.
So your code might be:
Code:
Private Sub Worksheet_Change(ByVal ChangedCell As Range)
With Cells(ChangedCell.Row,1)
If .Value = "" Then
.Value = Application.WorksheetFunction.Max(Columns(1))+1
End If
End With
End Sub
This is basically saying:
When you change a row, if there is no value in column A, make the value in column A on that row be the maximum number that already exists in Column A + 1.
That will work (I didn't test the code - might be the odd typo), but your users will need to be disciplined (and users never are).
A few more if statements might help you:
- If the cell above the one we're about to write to is blank then don't update with a number (obviously don't check this if Row 1 or you'll error). This stops new IDs being assigned to accidental clicks not in the general body of data.
- If the column of the cell clicked (ChangedCell.Column) is greater than than the number of columns you have in use, ignore. This stops IDs being assigned to things which are definitely not your data.
- If UserName="President Sarkozy of France" then Msgbox("You are not authorised to use this database as punishment for being rude to the Prime Minister of Great Britain and Northern Ireland.")
But I'll leave you to play with those!
Hope that's the sort of thing you're after. Have a good one - Happy Christmas.
Tom