Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-18-2011, 05:03 AM   #1
delta function
Registered: Jul 2004
Posts: 51

Rep: Reputation: 20
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)


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 ??
Old 01-18-2011, 11:26 AM   #2
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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):

 * 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 
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?
Old 01-19-2011, 01:25 AM   #3
delta function
Registered: Jul 2004
Posts: 51

Original Poster
Rep: Reputation: 20
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.
Old 01-20-2011, 09:53 AM   #4
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter key events MTK358 Programming 5 03-01-2010 12:12 PM
Laptop key press/release events question flsomerv Linux - Laptop and Netbook 2 10-08-2007 10:59 AM
Getting Key and Create events from all window children with Xt LonelyStar Programming 4 09-27-2007 07:27 AM
How to get key press and release events on kernel 2.6 for a application in C? abhijeet Programming 2 04-24-2007 12:37 AM
Java, Fedora Core 6, AltGr, and key events King V Linux - Software 2 04-12-2007 11:11 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration