VBA to return to original cell

51DegreesNorth

New Member
Joined
Mar 29, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi,

In my project, the Worksheet_Change event performs some actions. When all is done, I'd like to return the user to the cell where it all started, without firing the on change event again.

VBA Code:
Dim Src_PO_Nr As Long
Dim DestRow As Long
Dim SrcRow As Long
Dim RetAdr As String


    'active cell determined by user (new entry or edit cell)
    Src_PO_Nr = Cells(ActiveCell.Row, 1)
    SrcRow = ActiveCell.Row
    RetAdr = ActiveCell.Address

    '
    Worksheets("Some Sheet").Activate
    
    DestRow = Application.WorksheetFunction.Match(Src_PO_Nr, Range("A:A"), 0)
    Sheets("Some Sheet").Rows(DestRow).Value = Sheets("Some other Sheet").Rows(SrcRow).Value
    
    Worksheets("Some other Sheet").Range("RetAdr").Offset(1).Select

The last line of code errors out. If the orginal active cell was A1, then RetAdr contains $A$1. Declaring RetAdr as range does not fix it.

I"d like to get rid of the error. Also I'd like a a more efficient way to locate Src_PO_Nr - searching in an entire column seems overkill.

Your help much appreciated.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
In a Worksheet_Change event procedure, the default "Target" cell is the cell that is originally changed that sets the whole code in motion.
So you should be able to just use:
VBA Code:
Target.Select
to return to it.

Since this is a "Change" event, and not a "SelectionChange" event, simply selecting this cell should not trigger any code to run again, unless you also have "SelectionChange" code, in which case ou would use:
VBA Code:
Application.EnableEvents = False
Target.Select
Application.EnableEvents = True
 
Upvote 0
Thank you.
No luck, unfortunately. A
VBA Code:
 MsgBox Target
remains empty ..... I can not declare a public variable in event code so I'm basically
back to square one.
 
Upvote 0
Thank you.
No luck, unfortunately. A
VBA Code:
 MsgBox Target
remains empty ..... I can not declare a public variable in event code so I'm basically
back to square one.
What are you trying to accomplish with this?
VBA Code:
MsgBox Target
and where are you putting it?

I think a more helpful thing to look at would be this:
VBA Code:
MsgBox Target.Address
What does that return?

Could please post the entire procedure, including the header row?
And what sheet module are you putting this procedure in?
 
Upvote 0
My apologies for the late reply. The problem no longer exists; management has put this project on hold. Thanks for your assistance though, I will certainly do more research , you have made me think !
 
Upvote 0

Forum statistics

Threads
1,215,732
Messages
6,126,540
Members
449,316
Latest member
sravya

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