Results 1 to 5 of 5

Thread: [RESOLVED] Problems with Worksheet_SelectionChange

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Resolved [RESOLVED] Problems with Worksheet_SelectionChange

    Hello,

    I am trying to accomplish the following:
    I have 2 sheets, and i want to exchange some data between them.
    Each time sheet2 is clicked on, i need a script to run.

    Let's say sheet1 is selected, and i have the cell C4 active.
    Eache time i go to sheet2 i need a script to run automaticly.
    The script should take the value from the cell that was active in sheet1(so C4),
    and copy in in sheet2, when i switch to it, in another cell, let's say D8.

    I figured out i need the Worksheet_SelectionChange(ByVal Target As Range) function.
    I guess that "Target", holds the value of the cell that was active in the previous sheet?
    How can i access that value?
    I tried Target.Value but it didn't work.

    If someone could help me i would appriciate it verry much!

    Thank you!

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Problems with Worksheet_SelectionChange

    I have created a new module and added:
    vb Code:
    1. Dim rLastCell As Range
    2.  
    3.  
    4. Sub ExampleProcedure()
    5.     Run "LastUsedCell", 1
    6.         If Not rLastCell Is Nothing Then
    7.             MsgBox rLastCell.Value
    8.         Else
    9.             MsgBox "The Sheet is empty"
    10.         End If
    11. End Sub
    12.  
    13. Function LastUsedCell(lWsIndex As Long, Optional strColumn As String) As Range
    14.  
    15. '''''''''''''Written by www.ozgrid.com''''''''''''''''''''''
    16. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    17. 'Returns a Range Object of the last used cell on Worksheet or in a Column.
    18. 'Sheet index number (lWsIndex) is mandatory and Column letter (strColumn) is optional.
    19. 'Example call from Procedure in the same Module.
    20. ''''''''''''''''''''' Run "LastUsedCell", 1, "A" '''''''''''''''''''''''''''''''''''''''
    21. 'IMPORTANT. WILL FAIL IF SHEET INDEX USED IS NOT A WORKSHEET.
    22.  
    23.  
    24.     If strColumn = vbNullString Then
    25.         With Sheets(1).UsedRange
    26.             Set rLastCell = .Find(What:="*", After:=.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
    27.             xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
    28.         End With
    29.     Else
    30.         With Sheets(lWsIndex)
    31.             Set rLastCell = .Cells(.Rows.Count, strColumn).End(xlUp)
    32.         End With
    33.     End If
    34. End Function

    But i get the value of the cell in the last row&column, not the las active one, any thoughts?

    Thank you!

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Problems with Worksheet_SelectionChange

    Ok so i declared a global variable for the first sheet, and access it from the 2nd sheet, and now it almost works
    vb Code:
    1. Public LastCell As Range
    2.  
    3. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    4.     Set LastCell = Target.Select( go down 3 and right 3) so a 3x3 block
    5. End Sub

    Can someone please help me on the syntax?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problems with Worksheet_SelectionChange

    the selection change event does not fire when changing sheet, only cells within a sheet

    try
    vb Code:
    1. set lastcell = target.resize(3, 3)
    note this sets a range object, does not assign values to a range
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    6

    Re: Problems with Worksheet_SelectionChange

    that fixed it , thank you!

Posting Permissions

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



Click Here to Expand Forum to Full Width