+ Reply to Thread
Results 1 to 3 of 3

Worksheet SelectionChange Event

  1. #1
    Registered User
    Join Date
    01-20-2004
    Posts
    33

    Worksheet SelectionChange Event

    Hi,

    I have this code here for a selectionchange event on one of my worksheets.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range(Cells(Target.Row, 4), _
    Cells(Target.Row + Target.Rows.Count - 1, 30)).Select
    End Sub

    This works great as long as the range I want is contiguous. How would I go about changing this code to only keep selected rows if the user holds the CTRL key and selects random rows? I don't know if this is even possible but I've played with the intersect method and this doesn't seem to be a solution.
    Any input is appreciated.

    Thanks,
    Mjack

  2. #2
    Ardus Petus
    Guest

    Re: Worksheet SelectionChange Event

    Try following code.

    BTW: you forgot to disable events while you're changing selection in your
    code!

    HTH
    --
    AP

    '----------------------------
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rArea As Range
    Dim r1 As Range
    Dim rSelect As Range
    Application.EnableEvents = False
    For Each rArea In Target.Areas
    Set r1 = Range( _
    Cells(rArea.Row, "D"), _
    Cells(rArea.Row + rArea.Rows.Count - 1, "AD") _
    )
    If rSelect Is Nothing Then
    Set rSelect = r1
    Else
    Set rSelect = Union(rSelect, r1)
    End If
    Next rArea
    rSelect.Select
    Application.EnableEvents = True
    End Sub
    '----------------------------------
    "mjack003" <[email protected]> a écrit
    dans le message de news:
    [email protected]...
    >
    > Hi,
    >
    > I have this code here for a selectionchange event on one of my
    > worksheets.
    >
    > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    > Range(Cells(Target.Row, 4), _
    > Cells(Target.Row + Target.Rows.Count - 1, 30)).Select
    > End Sub
    >
    > This works great as long as the range I want is contiguous. How would
    > I go about changing this code to only keep selected rows if the user
    > holds the CTRL key and selects random rows? I don't know if this is
    > even possible but I've played with the intersect method and this
    > doesn't seem to be a solution.
    > Any input is appreciated.
    >
    > Thanks,
    > Mjack
    >
    >
    > --
    > mjack003
    > ------------------------------------------------------------------------
    > mjack003's Profile:
    > http://www.excelforum.com/member.php...fo&userid=5141
    > View this thread: http://www.excelforum.com/showthread...hreadid=539927
    >




  3. #3
    Registered User
    Join Date
    01-20-2004
    Posts
    33
    Thanks Ardus. Didn't even think about that...ingenious! One problem though...the user is selecting from a very long list. Haven't really put any thought into this yet but is there anyway to keep the screen from centering to the intial selection...which distracts the user and causes them to lose how far down the list they had scrolled. Thank you for the quick response.

    Mjack

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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