Run macro after Enter key is pressed

axma55

New Member
Joined
Feb 26, 2010
Messages
2
Hi. I have table with many rows of data, and I'd like to have certain cells copied in the same row when data is entered in a specific column in that row. This is what I have so far:

Private Sub worksheet_change(ByVal target As Range)

If target.Column = 29 Then
Thisrow = target.Row
If target.Value <> "" Then

Cells(ActiveCell.Row, 3).Copy
...(more code that copies cells follows this)

End If
End If

End Sub​

If the user presses the Tab key after entering data in column 29, then it runs just as I want it to run. The problem comes when the user presses the Enter key. Data in the wrong row is copied when that happens. Is there a way to detect if the Enter key is pressed which then would run code that would simply go back up one row and run the code to copy the cells in the active row? Or, is there a better way than what I'm thinking of doing? Hope that made sense. Thanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This is the problem line
Code:
Cells(ActiveCell.Row, 3).Copy
ActiveCell is not the changed cell, it is the cell that is now selected.
If you press Tab, ActiveCell is one to the right of Target
If you press Return, ActiveCell is one below Target.

If you base your activity on Target, rather than ActiveCell, it will be corrected.
Code:
Cells(Target.Row, 3).Copy
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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