Using Application.OnKey <key>, <procedure>, I can cause Excel to run <procedure> whenever <key> is pressed.
What I’d like is for it to do whatever pressing <key> would have done by default, and then run <procedure>. In other words, I don’t want to use OnKey to override what a key does, I want to use it to add to what a key does.
Background Info:
I’m using a barcode scanner that acts just like a keyboard when it scans something, typing in the characters of the barcode, followed by a carriage return (char 13). This carriage return does trigger Excel’s OnKey "~" event.
Right now, I can scan a barcode, and it puts the barcode’s value in the cell, then moves down to the next cell. This is a good start. What I want to do is have Excel run some code along with this, so that it puts the value in the cell, moves down a cell, then runs a procedure.
Why not just “rethrow” the carriage return using
SendKeys, and let Excel deal with it as it sees fit? Admittedly, this is a kludge, but perhaps no more so than the rest of your code as you describe it! It would be the simplest and most readable solution — if it works.Just put
PlainOldCarriageReturnat the beginning of the procedure called byOnKey.My cursory testing indicates that this does works, but you’ll want to test it yourself.