Thread: Range Selection
View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Range Selection

And, as Dave has pointed out, the Sort routine should use xlNo for the
header argument in the Sort statement. Here is the corrected code...

Sub SortDataBelowFREQ()
Dim FREQrow As Long
Dim LastRow As Long
Dim LastColumn As Long
Dim DataRows As Range
With ActiveSheet
FREQrow = .Cells.Find(What:="FREQ", After:=Range("A1"), _
LookAt:=xlWhole, MatchCase:=True).Row
LastRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlRows).Row
Set DataRows = .Range(FREQrow & ":" & LastRow)
LastColumn = DataRows.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
With .Range(.Cells(FREQrow + 1, "A"), .Cells(LastRow, LastColumn))
.Sort key1:=.Columns(1), order1:=xlAscending, _
key2:=.Columns(2), order2:=xlAscending, _
key3:=.Columns(LastCol), order3:=xlAscending, _
header:=xlNo, MatchCase:=False
End With
End With
End Sub

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
I screwed up my With..End With blocking. This is the macro I should have
posted...

Sub SortDataBelowFREQ()
Dim FREQrow As Long
Dim LastRow As Long
Dim LastColumn As Long
Dim DataRows As Range
With ActiveSheet
FREQrow = .Cells.Find(What:="FREQ", After:=Range("A1"), _
LookAt:=xlWhole, MatchCase:=True).Row
LastRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlRows).Row
Set DataRows = .Range(FREQrow & ":" & LastRow)
LastColumn = DataRows.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
With .Range(.Cells(FREQrow + 1, "A"), .Cells(LastRow, LastColumn))
.Sort key1:=.Columns(1), order1:=xlAscending, _
key2:=.Columns(2), order2:=xlAscending, _
key3:=.Columns(LastCol), order3:=xlAscending, _
header:=xlYes, MatchCase:=False
End With
End With
End Sub

I guess I should point out that I used ActiveSheet, but if you want this
code to apply to a specific sheet, just change the ActiveSheet reference
in the first With statement to Worksheets("SheetX") where you would
replace SheetX with the worksheet's actual name.

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
Here is a macro which uses your Sort code, but which automatically
determines the last used row and last used column, *within* the actual
data area (those rows below the FREQ header row), to apply it to...

Sub SelectDataRows()
Dim FREQrow As Long
Dim LastRow As Long
Dim LastColumn As Long
Dim DataRows As Range
With ActiveSheet
FREQrow = .Cells.Find(What:="FREQ", After:=Range("A1"), _
LookAt:=xlWhole, MatchCase:=True).Row
LastRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlRows).Row
Set DataRows = .Range(FREQrow & ":" & LastRow)
LastColumn = DataRows.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
End With
With .Range(.Cells(FREQrow + 1, "A"), .Cells(LastRow, LastColumn))
.Sort key1:=.Columns(1), order1:=xlAscending, _
key2:=.Columns(2), order2:=xlAscending, _
key3:=.Columns(LastCol), order3:=xlAscending, _
header:=xlYes, MatchCase:=False
End With
End Sub

--
Rick (MVP - Excel)


"Dave Peterson" wrote in message
...
You don't have to work with selections in your code for most things.

If you do, I think you'll find the code harder to understand and
maintain.

You didn't say how you wanted the sort done (or did I miss that???).

Option Explicit
Sub testme()

Dim TopLeftCorner As Range
Dim BotRightCorner As Range

Dim LastRow As Long
Dim LastCol As Long

Dim StringToSearchFor As String
Dim wks As Worksheet

Set wks = ActiveSheet 'worksheets("sheetnamehere")

StringToSearchFor = "Freq"

With wks
With .Range("A1").EntireColumn
Set TopLeftCorner = .Cells.Find(What:=StringToSearchFor, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With

If TopLeftCorner Is Nothing Then
MsgBox StringToSearchFor & " wasn't found!"
Exit Sub
End If

'using column A to determined the last row to sort
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

'using the headers in the row with Freq to determine
'the last column to sort
LastCol _
= .Cells(TopLeftCorner.Row,
.Columns.Count).End(xlToLeft).Column

Set BotRightCorner = .Cells(LastRow, LastCol)

With .Range(TopLeftCorner, BotRightCorner)
.Sort key1:=.Columns(1), order1:=xlAscending, _
key2:=.Columns(2), order2:=xlAscending, _
key3:=.Columns(LastCol), order3:=xlAscending, _
header:=xlYes, MatchCase:=False
End With
End With
End Sub

The sort statement includes the row with Freq, but header:=xlyes means
that it
won't be sorted as part of the data.

WLMPilot wrote:

I am trying to select a variable range, beginning in column A and
ending in
the furtherest column to the right being used AND last row used.

There are three factors to remember:
1) The actual beginning point (upper leftmost cell) may change as I
insert/delete
rows above the data to be sorted. The first cell in column A for
the
sort range
is located below the header FREQ.
2) The number of rows down will increase as data is added.
3) The number of columns used may increase if I determine there are
other
items I need to calculate/input.

Currently, I am using columns A-R. The column headers are on row 8,
which
may increase/decrease based on item #1 above.

One other quick question:
I believe I have the sort code I need. My question is this: After the
range is selected, I believe the first part of the sort code is
"Selection:Sort" (without quotes). Is this correct?

Thanks for your help!

Les

--

Dave Peterson