Originally Posted by
Megatronixs
Can you please maybe explain shortly how it works?
Not sure how much you already know about macros so I'll start from the beginning.
In Excel, certain actions that a user takes trigger an event. If Excel sees one one of these events occurring, it will look for a Sub with a name matching the event. In this case, the event is the user changing the contents of a cell in a particular worksheet. Worksheet_Change is called when this happens, and Target is the range of cells that was changed.
Intersect is a function that returns the intersection of multiple ranges. If it returns a special value called Nothing, then there is no intersection. So first if the intersection of the updated cell and the cell we are watching is not Nothing (note the double negative here, especially non-native English speakers), then the cell we are concerned about has changed.
Then we are going to use the Environ function to get the Windows username for the person currently logged in. If it matches the person we are looking for, we continue with our code.
Next we disable automatic event handling. That is because we are about to change the value of the cell that the user just changed. If we fail to disable event handling, Excel will try to call this Sub again as soon as we change a cell, and then we change it again, and it gets called again, and we are in an infinite loop. We change the cell back to blank, re-enable events, and give the user a little message that he tried something he's not allowed to do.
To add a second or third person, how to add it to this macro?
can I add a range and specify different cells that are not in a range?
See code below for answer to the first question.
Not sure what you mean by the second question. You can have whatever cells you want to monitor in your Range, but the code has to change if there are more than one. If that's what you mean let me know. The code would have to add a loop that blanked out every cell that the user tried to change. They could do this for more than one cell at a time by doing a paste, for example.
Bookmarks