Thread: check selection
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones[_2_] Norman Jones[_2_] is offline
external usenet poster
 
Posts: 421
Default check selection

Hi Yngve,

It is usually unnecessary, and inefficient, to
make selections. More normally, a range
variable would be declared and the variable
would be used for subsequent manipulation.

So, for example, your sort code might
resemble:

'==========
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range

Set WB = Workbooks("myBook.xls")
Set SH = WB.Sheets("Sheet2")
Set Rng = SH.Range("A1:C20")

With Rng
.Sort Key1:=.Cells(1, 1), _
Order1:=xlAscending, _
Key2:=.Cells(1, 2), _
Order2:=xlAscending, _
Key3:=.Cells(1, 2), _
Order3:=xlAscending, _
Header:=xlGuess, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom
End With

End Sub
'<<===========

If, however, you do have an imperative
for the user to select a range, Gary's
Student has shown you how to return the
requisite range using the Application.InputBox
method.

If you are experiencing probleme in
implementing either suggestion, post back
with the specific problems encountered.



---
Regards.
Norman


"Yngve" wrote in message
...
On 14 Mai, 12:01, "Norman Jones"
wrote:
Hi Yngve,

============ Is ther a way to check selection before I sort a sheet, I
need to do
it on the fly


============

I am not sure that I have understood your
question, but

Activecell

returns the currently selected cell and

Selection

returns the selected range.

If the selected range is a single cell, Activecell
and will return the same cell.

T|herefore, adapt something like:

'==========
Public Sub Tester()
Dim Rng As Range
Dim rCell As Range

Set Rng = Selection
Set rCell = ActiveCell

MsgBox Prompt:="Selected range = " _
& Selection.Address(External:=True) _
& vbNewLine & "Active cell = " _
& ActiveCell.Address(External:=True)
End Sub
'<<==========

However, a more interesting quesion
might be *why* you need to check the
selection; armed with that information,
it might well be possible to provide a
more useful response.

---
Regards.
Norman

thanks for replaying
Norman
I need to do the cheek to bee sure that the user has select proper
before sorting.
The range I have to select is Range("A5:H"&....)

Regards Yngve