Excel
(1)
Macro
(1)
XlTopToBottom
(1)
XlSortNormal
(1)
ActiveSheet
(1)
SalesAid
(1)
XlGuess
(1)
TctedThank
(1)

select a range by cell names; vba

Asked By cate
21-Nov-09 08:33 AM
I am trying to select X rows for a sort.  The first and last rows have
a defined name, RSTART and RLAST.  They look
like this in the Define Name dialog:  ='MySheet'!$15:$15 ....

How do you select the rows using these names.  Here is the macro output
of my best shot.  I cannot figure out how to incorporate the second
name.  Thank you.

Sub trythis()

Application.Goto Reference:="RSTART"
Rows("15:59").Select    '<---- Held down shift key and selected
RLAST in cell address combo
Range("I15").Activate    ' what is this?
Selection.Sort Key1:=Range("B15"), Order1:=xlDescending,
Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub

Most things that you do in excel do not need to have the objects (like ranges

Dave Peterson replied to cate
21-Nov-09 08:42 AM
Most things that you do in excel do not need to have the objects (like ranges or
worksheets) selected first.

You could use code like:

Option Explicit
Sub trythis()

Dim wks As Worksheet
Dim myRng As Range

Set wks = ActiveSheet

With wks
'I like this first line--I find it more self-documenting
Set myRng = .Range(.Range("RStart"), .Range("RLast"))
'but you could use either of these, too.
'Set myRng = .Range("Rstart", "rlast")
'Set myRng = .Range("Rstart:Rlast")
End With

With myRng
.Sort Key1:=.Columns(2), Order1:=xlDescending, _
Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With

End Sub


--

Dave Peterson

ges or_tctedThank you. I always have problems with ranges.

cate replied to Dave Peterson
21-Nov-09 09:57 AM
ges or
_
t
cted

Thank you. I always have problems with ranges.  Sometimes I have
numbers, sometimes letters and, this time, names. Ahhhh.
Thanks again.

Sub sortnamedrange()'NOT needed unless a different sheet'Application.

Don Guillett replied to cate
21-Nov-09 09:02 AM
Sub sortnamedrange()
'NOT needed unless a different sheet
'Application.Goto Reference:="RSTART"

mc = 2 ' col B
fr = Range("rstart").Row
lr = Range("rlast").Row
Range(Cells(fr, mc), Cells(lr, mc)) _
.Sort Key1:=Cells(fr, mc), Order1:=xlDescending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
dguillett1@austin.rr.com
Post Question To EggHeadCafe