Selecting Rows

nehpets12

Active Member
Joined
Feb 22, 2002
Messages
453
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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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>

caffeine_sample.gif
</gif>
This message was edited by Richie(UK) on 2002-09-12 04:19
 
Upvote 0
Thanks nearly there but this only selects the 2 rows how can I select all the rows between them?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,027
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top