VB Scripting Worklsheet and Key events
Hi, am working an a worksheet where I need to lookup id in an other worksheet.
The script works as long as I am staying on the same row. When pressing key up or enter the corresponmding lookup information is not placed on the very same row. In short terms, I want the lookup data to be enterd on the same row no matter what key I am pressing. The script: Private Sub Worksheet_Change(ByVal Target As Range) Dim cfind As Range On Error Resume Next If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub thisRow = ActiveCell.Row With Worksheets("Blad2") .With Columns("h:h") Set cfind = .Cells.Find(What:="CBSC01CA" & Target.Value, LookAt:=xlWhole) End With ' MsgBox thisRow If Not cfind Is Nothing Then Range("B" & thisRow - 1).Value = Worksheets("Blad2").Range("D" & cfind.Row) Range("C" & thisRow - 1).Value = Worksheets("Blad2").Range("I" & cfind.Row) Else MsgBox "this ID is not avialble" End If End Sub This example is set to Enter key procedure. So when the enter key is pressed, my current row will change by 1 so I have to compensate for that. All I want is that the data will be correctly enterd no matter what key I am pressing. Any ideas ?? |
Am I right in thinking this is what your programme does (sorry, I haven't done much macro-writing, and it's all been with OpenOffice rather than Excel):
Code:
* Given variable "Target" which will be a cell in column "A" In this case, how does the sub get called? Is this done by another one of your subs? |
Thanks for teh reply Snark1994
Well what you think is true. At the moment I am not calling any subs, but as it seems this is the way to do it. I am fiddeling now with subs which act on keypress. When arrow down or return is pressed => thisRow -1, if arrow up key is pressed => thisRow +1 etc ... The challange now is that triggering the sub functions should only occur when entering the IDs in column. Plus that the subs trigger can only be called from within with the above function ( meaning editing other cells should trigger nothing ) I will do some trail & error. In case I am successful, I will post this later. In case someone knows a link for more detailed info on how to do this, I will be very happy. |
If you aren't calling the sub, how does it get run?
And your approach is a good one - if you capture the keyboard input, then you can work out which row it was originally in and check it's in column A, then pass these values to the sub you posted in your first post (obviously, removing the corrections you had in it for pressing ENTER). This way, you have only one sub which does the actual heavy-lifting (copying the data over), and you can correct the cursor moving about the spreadsheet. |
All times are GMT -5. The time now is 06:30 PM. |