Probably too easy...change range to be one row down.

R

RocketMan

I use a cell.find command to find a row col with a certain item. Now
I want to alter that so that its one row below after the find. Here
is what I have so far:

Dim FirstCell As Range
Dim LastCell As Range
Dim SortRange As Range

Set FirstCell = ActiveSheet.Cells.Find(What:="Pills", After:=[A4],
LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False)

' need to set FirstCell to be one row lower to sort the data

Set LastCell = ActiveSheet.Range("E65536").End(xlUp) ' end of data
lines will be in E column
Set SortRange = ActiveSheet.Range(FirstCell, LastCell)
' ActiveSheet.SortRange.Sort ' use A column as key
 
D

Dave Peterson

I'd try:

Set SortRange = ActiveSheet.Range(FirstCell.offset(1,0), LastCell)
I use a cell.find command to find a row col with a certain item. Now
I want to alter that so that its one row below after the find. Here
is what I have so far:

Dim FirstCell As Range
Dim LastCell As Range
Dim SortRange As Range

Set FirstCell = ActiveSheet.Cells.Find(What:="Pills", After:=[A4],
LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False)

' need to set FirstCell to be one row lower to sort the data

Set LastCell = ActiveSheet.Range("E65536").End(xlUp) ' end of data
lines will be in E column
Set SortRange = ActiveSheet.Range(FirstCell, LastCell)
' ActiveSheet.SortRange.Sort ' use A column as key
 
J

JLGWhiz

After the Find statement:

FirstCell = FirstCell.Offset(1, 0).Address

Then to get your sort range:

Set LastCell = ActiveSheet.Range("E65536").End(xlUp).Address

SortRange = Range(Firstcell & ":" & LastCell)
 
J

JLGWhiz

That should be Set SortRange = Range(FirstCell & ":" & LastCell)
And it wouldn't hurt to insert the worksheet name in the Set statement.
 
R

RocketMan

Not quite.
FirstCell = FirstCell.Offset(1, 0).Address
will set the value to the offset address. So "pills" becomes $A$21.

:)


After the Find statement:

FirstCell = FirstCell.Offset(1, 0).Address

Then to get your sort range:

Set LastCell = ActiveSheet.Range("E65536").End(xlUp).Address

SortRange = Range(Firstcell & ":" & LastCell)



RocketMan said:
I use a cell.find command to find a row col with a certain item.  Now
I want to alter that so that its one row below after the find.  Here
is what I have so far:
Dim FirstCell As Range
Dim LastCell As Range
Dim SortRange As Range
Set FirstCell = ActiveSheet.Cells.Find(What:="Pills", After:=[A4],
LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
        False)
' need to set FirstCell to be one row lower to sort the data
Set LastCell = ActiveSheet.Range("E65536").End(xlUp) ' end of data
lines will be in E column
Set SortRange = ActiveSheet.Range(FirstCell, LastCell)
' ActiveSheet.SortRange.Sort ' use A column as key- Hide quoted text -

- Show quoted text -
 
R

RocketMan

final code that works...just for reference:

Sub Alpha_Click()

Dim FirstCell As Range
Dim LastCell As Range
Dim SortRange As Range

Set FirstCell = ActiveSheet.Cells.Find(What:="Pills", After:=[A4],
LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False)

Set LastCell = ActiveSheet.Range("A65536").End(xlUp)
Set SortRange = Range(FirstCell.Offset(1, 0), LastCell.Offset(0, 5))
SortRange.Sort Key1:=FirstCell

End Sub
 
J

JLGWhiz

Did you not say you wanted the sort range to start one row lower than the
find cell?
If so, then Offset(1, 0) is one row lower. If you want "pills" included in
the sort range, the just use the FirstRow.Address as the front end of the
range. Or give a better explanation of what you mean by one row down.

RocketMan said:
Not quite.
FirstCell = FirstCell.Offset(1, 0).Address
will set the value to the offset address. So "pills" becomes $A$21.

:)


After the Find statement:

FirstCell = FirstCell.Offset(1, 0).Address

Then to get your sort range:

Set LastCell = ActiveSheet.Range("E65536").End(xlUp).Address

SortRange = Range(Firstcell & ":" & LastCell)



RocketMan said:
I use a cell.find command to find a row col with a certain item. Now
I want to alter that so that its one row below after the find. Here
is what I have so far:
Dim FirstCell As Range
Dim LastCell As Range
Dim SortRange As Range
Set FirstCell = ActiveSheet.Cells.Find(What:="Pills", After:=[A4],
LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False)
' need to set FirstCell to be one row lower to sort the data
Set LastCell = ActiveSheet.Range("E65536").End(xlUp) ' end of data
lines will be in E column
Set SortRange = ActiveSheet.Range(FirstCell, LastCell)
' ActiveSheet.SortRange.Sort ' use A column as key- Hide quoted text -

- Show quoted text -
 

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