Voted up because you're curious enough to ask. However, there is nothing confusing here. The logic of the macro above is simple.
- If you're modifying
A1
, it won't continue firing. - If it's not in Column A, or the highlighted range is more than 1 column, it won't continue firing.
- If the cell you're on, the active cell is empty, then it won't continue firing, allowing you to put in an initial value to the cell.
If all the above weren't satisfied (meaning FALSE
), the macro continues running. The next few checks are simple.
- Okay, so the active cell wasn't empty. But did user input a number? If yes, continue running. Otherwise, delete his input and inform user to only use numbers.
- If user provided a valid number, do the following:
- Get user's input, store it as
newVal
. - Undo the input, get the original cell's value and store it as
oldVal
. - Add the two together and put it back into the cell.
- Get user's input, store it as
Don't confuse yourself with the Delete
button. What the author meant was simply this:
Worksheet_Change
event tracks almost every single change you do on the sheet. This includes typing in, editing, or deleting values from cells. When you press the Delete
button while highlighting a cell, you are causing the macro above to happen.
Now, if the above line is NOT commented out, the macro will eventually get to the third check, which is to check if the target cell is empty. Since you've just deleted the content, of course it's empty. The macro will exit and nothing will happen.
If you comment it out, this check will never happen. When you delete the cell's content, you'll continue running and the macro will undo your job again and again.
The way to check it is simple. See my screenshot below:
Put the same breakpoints as I have on the three lines above. Notice that I have the IsEmpty
line commented out. Now enter a value into A2
. Try deleting it. You'll see it get deleted but the first red line in the macro will get highlighted. This means it's checking your cell's content now. The IsNumeric
check is going to evaluate to TRUE
which will not exit your code. Basically, it will continue to the bottom part.
Press F5 to continue running the code.
Now you'll arrive at Application.Undo
. Before arriving here, note that newVal
is going to store your new value (0
). Now, your delete will be undone by the Application.Undo
line, and 0
will be added to it. Basically, you're just adding 0
repeatedly to the cell's value.
Do not get yourself confused by the terms or by the help file. They're, well, helpful. But consider first the logic of the code before looking up what each thing does. Isolate the ifs and the thens and the elses. Have a paper handy and prepare to write or draw the logic flow of this by hand. That is much better than reading the help file when you don't know what to look for.
Let us know if this clears up the confusion. :)