Selecting Rows

nehpets12

Active Member
Joined
Feb 22, 2002
Messages
452
I am trying to find 2 bits of text in a file and deletin rows inbetween them. This is what I have so far but the last line

rows(b:C).Select

Does not work Please Help




Selection.Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Columns("C:C").Select
Selection.Find(What:="Run", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

b = ActiveCell.Row


Columns("D:D").Select
Selection.Find(What:="Free", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

ActiveCell.Offset(-1, 0).Range("A1").Select

c = ActiveCell.Row

rows(b:C).Select
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
Hi nehpets12,

I think you need to use the Union method. See if you can work with the example below.<pre>

Sub rowselect()
Dim b, c, d As Range

'Selection.Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlGuess, _
'OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Columns("C:C").Select
Selection.Find(What:="Run", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

b = ActiveCell.Row

Columns("D:D").Select
Selection.Find(What:="Free", After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

ActiveCell.Offset(-1, 0).Range("A1").Select

c = ActiveCell.Row

Set d = Union(Rows(b), Rows(c))
d.Select

End Sub</pre>

HTH

PS. Your code selects the row containing "Run" rather than the first one below it - be careful when implementing the deletion code! :wink:

_________________<font color="blue"> «««<font color="red">¤<font color="blue"><font size=+1>Richie</font><font color="red">¤<font color="blue"> »»»</font>

</gif>
This message was edited by Richie(UK) on 2002-09-12 04:19
 

nehpets12

Active Member
Joined
Feb 22, 2002
Messages
452
Thanks nearly there but this only selects the 2 rows how can I select all the rows between them?
 

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
Sorry,

Still learning this VBA stuff myself!

Changed my mind - change Union to Range.
 

New Poster

New Member
Joined
Aug 22, 2002
Messages
22
The syntax should be :-

Rows(b & ":" & c).Select

However, if you want to delete the rows between the rows containing "Run" and "Free", you first need to establish what row is the lower number and also that there is at least one row between them.
So, delete the line that reads "ActiveCell.Offset(-1, 0).Range("A1").Select" and also, replace "rows(b:C).Select" with :-

If b< c - 1 Then
Rows(b + 1 & ":" & c - 1).Delete
ElseIf c< b - 1 Then
Rows(c + 1 & ":" & b - 1).DeleteElse
MsgBox "There are no rows to delete between 'Run' and 'Free'."
End If

In fact, I would suggest that you change the whole thing to :-

Dim b%, c%
Selection.Sort Key1:=[D1], Order1:=xlAscending, Header:=xlYes
b = [C:C].Find(What:="Run").Row
c = [D:D].Find(What:="Free").Row
If b< c - 1 Then
Rows(b + 1 & ":" & c - 1).Delete
ElseIf c< b - 1 Then
Rows(c + 1 & ":" & b - 1).Delete
Else
MsgBox "There are no rows to delete between 'Run' and 'Free'."
End If
This message was edited by New Poster on 2002-09-12 05:30
 

Watch MrExcel Video

Forum statistics

Threads
1,118,113
Messages
5,570,266
Members
412,314
Latest member
yazanwael
Top