LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   VB Scripting Worklsheet and Key events (https://www.linuxquestions.org/questions/programming-9/vb-scripting-worklsheet-and-key-events-856998/)

delta function 01-18-2011 05:03 AM

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 ??

Snark1994 01-18-2011 11:26 AM

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"
 * Search for a cell in worksheet 2, column H whose value is "CBSC01CA" followed by
      the contents of cell A
 * If you've found a cell which matches the above criteria, then copy cells D and I
      in the row of the cell into the cells B and C in the row of the highlighted
      cell.

Is this right? And the problem is that when you enter text, depending on how you finish editing the text (pressing enter/escape/an arrow key) the position of the highlighted cell is different when you call the function?

In this case, how does the sub get called? Is this done by another one of your subs?

delta function 01-19-2011 01:25 AM

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.

Snark1994 01-20-2011 09:53 AM

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.