Consulting

Results 1 to 4 of 4

Thread: Solved: Goto previous cell

  1. #1
    VBAX Regular
    Joined
    Oct 2008
    Posts
    31
    Location

    Solved: Goto previous cell

    I want to make a macro that goes to previous cell that user visited. I found this: http://www.archivum.info/microsoft.p..._previous_cell

    This solution is based on a Worksheet_SelectionChange -sub.
    However, this solution works only for one worksheet. I need to make the macro work in any sheet or in any workbook.

    Is this possible? How?

    Thanks in advance!

  2. #2
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi,

    Put this in the ThisWorkbook Module and kill the sheet module code.

    [VBA]
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    If OldCell Is Nothing Then
    Set OldCell = Target
    Else
    Set OldCell = NewCell
    Set NewCell = Target
    End If

    End Sub

    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  3. #3
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    and if the last cell was on a different worksheet (not well tested):

    in thisworkbook:
    [vba]
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    If OldCell Is Nothing Then
    Set OldCell = Target
    Set oldSht = Sh
    Else
    Set OldCell = NewCell
    Set oldSht = newSht
    Set NewCell = Target
    Set newSht = Sh
    End If
    End Sub
    [/vba]
    module:
    [vba]
    Option Explicit
    Public OldCell As Range
    Public NewCell As Range
    Public oldSht As Worksheet
    Public newSht As Worksheet
    Public Sub GoBack()
    oldSht.Activate
    OldCell.Select
    End Sub
    [/vba]
    installing as an addin would have it available to any workbook.

  4. #4
    VBAX Regular
    Joined
    Oct 2008
    Posts
    31
    Location
    Awesome! Thanks guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •