PDA

View Full Version : GOBACK subroutine for Excel 2007



Sgt Rock
10-06-2012, 12:31 PM
Seeking assistance with interpretation of code to retrun cursor to it's previous location.
Code works sometimes and sometimes not. Doesn't seem to work when first opening the workbook its cotained in. Maybe something needs to be cleared first? To run the code I've assigned an icon to it in the ribbon. Cannot recall where I found the code but it's just a few lines lone. Also not sure why one part is inserted into a module while in the VBE, and the other part of the code is inserted on the worksheet, by right clicking worksheet, view code, and pasting code there. Thanks in advance! I'm new at VBA and still learning each day.

Code inserted via "insert module" in VBE

Option Explicit
Public r As Range
Public oldr As Range
Sub GOBACK()
If oldr Is Nothing Then
Else
oldr.Select
End If
End Sub

Code inserted via the worksheet "view code"

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set oldr = r
Set r = Target
End Sub

Aussiebear
10-06-2012, 03:56 PM
General code,and by this I mean code that can be used anywhere within the workbook can be contained within a module. Specific code that relates to a particular worksheet should reside within that worksheets code section.

Its hard to see why this code would work. It doesn't appear to define an address (Cell Location) before an event such as a (Worksheet_Change) occurs.

Paul_Hossler
10-06-2012, 06:11 PM
You didn't say how GOBACK is called, but if a different sheet is active, you probably have to select the original



Sub GOBACK()
If oldr Is Nothing Then
Else
oldr.Parent.Select
oldr.Select
End If
End Sub


Paul