+ Reply to Thread
Results 1 to 9 of 9

Record ActiveCell Location prior to cell movement

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Record ActiveCell Location prior to cell movement

    I'm having a problem with the beforeDoubleClick routine below:

    Private Sub worksheet_beforeDoubleClick(ByVal Target As Range, cancel As Boolean)
        Dim doReturn As Boolean
        doReturn = False
        ActiveWorkbook.Names.Add Name:="rms", RefersToR1C1:= _
            ActiveCell
        With Target
            If Not Intersect(.Cells, Me.Range("aq2:aq3")) Is Nothing Then
                doReturn = True
                If LCase(.Value) = False Then
                    .Value = True
                Else
                    .Value = False
                End If
            End If
            If Not Intersect(.Cells, Me.Range("at2:at24")) Is Nothing Then
                doReturn = True
                Call selectRoute
            End If
            If Not Intersect(.Cells, Me.Range("bd5:bd17")) Is Nothing Then
                doReturn = True
                Call selectSchool
            End If
            If doReturn Then
                Application.Goto reference:="rms"
            End If
        End With
        ActiveWorkbook.Names("rms").Delete
    End Sub
    I want it to record where the cursor is and place the range name "rms" in that location. Then it should evaluate the double-click location and take the indicated action - works fine by the way. Then the cursor should return to the "rms" location and delete that range name.

    The "rms" range name is always attached to the cell that is double-clicked. I was under the impression that this type of code would fire prior to any double-click action.

    Can any offer any suggestions as to where I went wrong?

    TIA.

    Bob
    Last edited by bstubbs; 03-19-2009 at 07:22 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,520

    Re: Record ActiveCell Location prior to cell movement

    Hi Bob,

    Try adding the following statement as the last line of your code:
    Cancel = True
    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Record ActiveCell Location prior to cell movement

    Your macros that are called must move the cell. What do these macros do? It is generally unnecessary to slect or activate ranges in VBA so the activecell could remain the same.

    Try attaching the workbook so that we can see what you are dong.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Record ActiveCell Location prior to cell movement

    GregM:

    Nope that didn't do it. It's still like the entire routine is being executed after the double-click not before it.

    Thanks

    Bob

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,520

    Re: Record ActiveCell Location prior to cell movement

    Hi Bob,

    I used dummy "SelectSchool" and "SelectRoute" subroutines when testing your code. With these routines in place your code worked when I included the "Cancel = True" command, but would not work without it. When I say your code "worked", I mean that it successfully named the active cell as "rms" while the code was running, and that this name had been deleted when code execution was finished.

    In view of this, it seems that the main problem may lie within one or both of the subroutines - you could easily test this by temporarily including an "Exit Sub" command as the first line of code in each of the subroutines. If that locates the problem you could post the subroutines here so that we can take a look at them.

    Regards,

    Greg M

  6. #6
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Record ActiveCell Location prior to cell movement

    GregM:

    Thank you, sir:

    The question that I have is what is the relationship of the "rms" label - is it in the activeCell before you double-clicked elsewhere or in the cell that you double-clicked. The routine has always done the later and I want it to do the former (the "rms" should mark the cell that was active prior to the double-click). Where is your proto-typed code putting the "rms" label?

    TIA.

    Bob

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,520

    Re: Record ActiveCell Location prior to cell movement

    Hi Bob,

    Ok - now I see what you're trying to do!

    E.g. the active cell is A1, you doubleclick on cell E5, and you want to return to cell A1 when your code has executed. The main problem is that the first click of the doubleclick selects cell E5 which immediately becomes the active cell. In effect, you're trying to trap a "BeforeCLICK" (not "BeforeDoubleClick") event.

    Unfortunately, as far as I know, Excel does not have a worksheet "BeforeClick" event, so it will be necessary to develop a workaround. The approach I used was to continuously track the previously selected cell - e.g. with cell A1 as the current selection, click on cell E5 and cell A1 is stored as the previously selected cell. Now click on cell B3, and E5 is stored as the previously selected cell, and so on.

    The following is the code I used in the VBA code module for the worksheet:
    
    Option Explicit
    
    
    Dim rPreviousCell   As Range
    Dim rActiveCell     As Range
    
    
    Private Sub Worksheet_Activate()
        Set rActiveCell = ActiveCell
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Set rPreviousCell = rActiveCell
        Set rActiveCell = ActiveCell
    
    End Sub
    
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
        Dim doReturn As Boolean
    
        doReturn = False
    
        With Target
    
            If Not Intersect(.Cells, Me.Range("AQ2:AQ3")) Is Nothing Then
    
                doReturn = True
                If LCase(.Value) = False Then
                      .Value = True
                Else
                      .Value = False
                End If
    
            End If
    
            If Not Intersect(.Cells, Me.Range("AT2:AT24")) Is Nothing Then
                doReturn = True
                Call SelectRoute
            End If
    
            If Not Intersect(.Cells, Me.Range("BD5:BD17")) Is Nothing Then
                doReturn = True
                Call SelectSchool
            End If
    
            If doReturn Then
                If Not rPreviousCell Is Nothing Then
                    rPreviousCell.Select
                End If
            End If
    
        End With
    
        Cancel = True
    
    End Sub
    Plus the following code in a standard VBA code module:
    
    Option Explicit
    
    
    Public Sub SelectRoute()
        MsgBox "Selecting route ..."
    End Sub
    
    
    Public Sub SelectSchool()
        MsgBox "Selecting school ..."
    End Sub
    This allowed me to select any cell (e.g. AP1) on the worksheet, doubleclick on any cell in the ranges you specify (AT2:AT24 etc.), have the BeforeDoubleClick code execute, and return automatically to cell AP1. Using this approach meant that I didn't need the user-defined name "rms".

    I hope the above helps - please let me know how you get on.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1