Help Needed - Keypress??

InfoH

Board Regular
Joined
Nov 1, 2011
Messages
60
Hi all,

I have a spreadsheet whereby anything entered into a cell between B2:B5000 it then would update the relevent row on columns C and D with both Username and Timestamp.

Now that bit works fine, however if they enter something into B2 and press Enter or Down, it will put the username and timestamp into C3 and D3

this is because it uses the activecell to judge where to put the username and timestamp

so my solution to this (which may very well be more complicated than it needs to be) was to do some if statements for the following

if down arrow is pressed then.. select target cell, move to the right, input username, move to the right, input timestamp and then do orignally selected down arrow to put the user on the cell they requested

Please see my code below and advise whats wrong with it

thanks in advance

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
If Not Intersect(Target, Range("B1:B5000")) Is Nothing Then
On Error Resume Next
If KeyAscii = 13 Then
Application.EnableEvents = False
Target.Cells.Select
ActiveCell.Offset(0, 1) = Application.UserName
ActiveCell.Offset(0, 2).Value = Now
Application.EnableEvents = True
ActiveCell.Offset(1, 0).Select
 
ElseIf KeyAscii = 37 Then
Application.EnableEvents = False
Target.Cells.Select
ActiveCell.Offset(0, 1) = Application.UserName
ActiveCell.Offset(0, 2).Value = Now
Application.EnableEvents = True
ActiveCell.Offset(0, -1).Select
 
ElseIf KeyAscii = 38 Then
Application.EnableEvents = False
Target.Cells.Select
ActiveCell.Offset(0, 1) = Application.UserName
ActiveCell.Offset(0, 2).Value = Now
Application.EnableEvents = True
ActiveCell.Offset(-1, 0).Select
 
ElseIf KeyAscii = 39 Then
Application.EnableEvents = False
Target.Cells.Select
ActiveCell.Offset(0, 1) = Application.UserName
ActiveCell.Offset(0, 2).Value = Now
Application.EnableEvents = True
ActiveCell.Offset(0, 1).Select
 
ElseIf KeyAscii = 40 Then
Application.EnableEvents = False
Target.Cells.Select
ActiveCell.Offset(0, 1) = Application.UserName
ActiveCell.Offset(0, 2).Value = Now
Application.EnableEvents = True
ActiveCell.Offset(1, 0).Select
On Error GoTo 0
End If
End If
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to MrExcel.

You need to use Target instead of ActiveCell. You can forget all the KeyAscii stuff - that variable will be empty.
 
Upvote 0
I am not sure whether I have understood your problem correctly

anyhow try this event code(you know wheres to park it )

and see whether it gets what you want

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Then
Target.Offset(0, 1) = Application.UserName
ElseIf Target.Column = 3 Then
'MsgBox Target.Address
Target.Offset(0, 1) = Now
Else
Exit Sub
End If
End Sub



enter anything in any cell in column B and see what happens in C and D in the same row
if it is ok save the file
 
Upvote 0
Do you want to do anything if they delete the cell contents? (your code doesn't as it stands)
 
Upvote 0

Forum statistics

Threads
1,203,171
Messages
6,053,885
Members
444,692
Latest member
Queendom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top