Worksheet SelectionChange Event

M

mjack003

Hi,

I have this code here for a selectionchange event on one of m
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 woul
I go about changing this code to only keep selected rows if the use
holds the CTRL key and selects random rows? I don't know if this i
even possible but I've played with the intersect method and thi
doesn't seem to be a solution.
Any input is appreciated.

Thanks,
Mjac
 
A

Ardus Petus

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 (e-mail address removed)...
 
M

mjack003

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top