Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Sam Sam is offline
external usenet poster
 
Posts: 11
Default Delete Entire Row.

Hi ...

We only want to mail advertising to individuals, not businesses. Here is
a short macro I wrote to eliminate obvious businesses from the list:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
End If
Next I
End Sub

It worked ... Kind of. When it came to a record with `Company' in the
name it deleted it. However, if the next record also had `Company' in the
name, it was not deleted. I guess that when it eliminated record seven,
record eight became record seven, and it was done with seven. It went on to
eight, which was nine until a second ago.

I fixed it, though. I added one line:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
I = I - 1
End If
Next I
End Sub

Now I is not incremented - Program checks record seven again. Works fine.
but there are two things I don't like.

1) Instead of `For I = 2 To 500,' I would the loop to just automatically run
until every record is checked:
`For I = 2 To EndOfColumn.'
There must be way to do that, but I don't know what it is.

2) I don't like screwing with I. I'm just an amateur, but I bet `real'
programmers never alter the value of a loop counter inside the loop itself.
(Do they??)

Sam
--
A man who had lately declared
That property ought to be shared,
Thought it going too far
When they called for his car,
And a list of exceptions prepared.

Thomas Thorneley,
From The Penguin
Book Of Limericks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Delete Entire Row.

Sam,

When deleting rows it is better to one of two things
- delete from the bottom up
- build up a range of rows to delete and delete them all at the end

Here is your code modified for the first, with automatic detection of the
end

Sub Eliminate()
Dim I As Long
Dim FullName As String
Dim oWS AsWorksheet

Set oWS = Worksheets(1)
For I = Cells(Rows.Count,"A").End(xlUp).Row To 2 Step -1
FullName = oWS.Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
oWS.Cells(I, 1).EntireRow.Delete
End If
Next I

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Sam" wrote in message
ink.net...
Hi ...

We only want to mail advertising to individuals, not businesses. Here

is
a short macro I wrote to eliminate obvious businesses from the list:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
End If
Next I
End Sub

It worked ... Kind of. When it came to a record with `Company' in the
name it deleted it. However, if the next record also had `Company' in the
name, it was not deleted. I guess that when it eliminated record seven,
record eight became record seven, and it was done with seven. It went on

to
eight, which was nine until a second ago.

I fixed it, though. I added one line:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
I = I - 1
End If
Next I
End Sub

Now I is not incremented - Program checks record seven again. Works

fine.
but there are two things I don't like.

1) Instead of `For I = 2 To 500,' I would the loop to just automatically

run
until every record is checked:
`For I = 2 To EndOfColumn.'
There must be way to do that, but I don't know what it is.

2) I don't like screwing with I. I'm just an amateur, but I bet `real'
programmers never alter the value of a loop counter inside the loop

itself.
(Do they??)

Sam
--
A man who had lately declared
That property ought to be shared,
Thought it going too far
When they called for his car,
And a list of exceptions prepared.

Thomas Thorneley,
From The Penguin
Book Of Limericks




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Delete Entire Row.

Sam

Try this one.

Notice it finds the last row in the column you choose then
deletes from the bottom up(For i = iLastrow To 1 Step -1)

Option Compare Text
Sub Delete_By_Criteria()
Dim i As Integer
Dim iLastrow As Integer
Dim Collet As String
Set Wks = ActiveSheet
Application.ScreenUpdating = False
Collet = InputBox("Enter Your Column Letter")
whatwant = InputBox("Choose Criteria" & Chr(13) _
& "Wildcards such as *PY* can be used")
iLastrow = Wks.Cells(Rows.Count, Collet).End(xlUp).Row
For i = iLastrow To 1 Step -1
If Wks.Cells(i, Collet).Value Like whatwant Then
Wks.Rows(i).Delete
End If
Next i
Application.ScreenUpdating = True
End Sub

Gord Dibben Excel MVP

On Sat, 20 Dec 2003 20:56:25 GMT, "Sam" wrote:

Hi ...

We only want to mail advertising to individuals, not businesses. Here is
a short macro I wrote to eliminate obvious businesses from the list:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
End If
Next I
End Sub

It worked ... Kind of. When it came to a record with `Company' in the
name it deleted it. However, if the next record also had `Company' in the
name, it was not deleted. I guess that when it eliminated record seven,
record eight became record seven, and it was done with seven. It went on to
eight, which was nine until a second ago.

I fixed it, though. I added one line:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
I = I - 1
End If
Next I
End Sub

Now I is not incremented - Program checks record seven again. Works fine.
but there are two things I don't like.

1) Instead of `For I = 2 To 500,' I would the loop to just automatically run
until every record is checked:
`For I = 2 To EndOfColumn.'
There must be way to do that, but I don't know what it is.

2) I don't like screwing with I. I'm just an amateur, but I bet `real'
programmers never alter the value of a loop counter inside the loop itself.
(Do they??)

Sam


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Delete Entire Row.

Best to work from bottom to up so you don't have to deal with these types of
issues.

If you must go from top to bottom, then this code should work:

Sub Eliminate()
Const cFullName = 4
Dim i As Long, j As Long

With Worksheets(1)
i = 2: j = .Cells(.Rows.Count, cFullName).End(xlUp).Row
Do Until i j
If InStr(1, .Cells(i, cFullName).Value, " COMPANY",
vbTextCompare) < 0 Then
.Rows(i).EntireRow.Delete
j = j - 1
Else
i = i + 1
End If
Loop
End With
End Sub



"Sam" wrote in message
ink.net...
Hi ...

We only want to mail advertising to individuals, not businesses. Here

is
a short macro I wrote to eliminate obvious businesses from the list:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
End If
Next I
End Sub

It worked ... Kind of. When it came to a record with `Company' in the
name it deleted it. However, if the next record also had `Company' in the
name, it was not deleted. I guess that when it eliminated record seven,
record eight became record seven, and it was done with seven. It went on

to
eight, which was nine until a second ago.

I fixed it, though. I added one line:

Sub Eliminate()
Dim I As Long
Dim FullName As String

For I = 2 To 500
FullName = Worksheets(1).Cells(I, 4).Value
If InStr(2, FullName, " COMPANY", 1) 0 Then
Worksheets(1).Cells(I, 1).Select
Selection.EntireRow.Delete
I = I - 1
End If
Next I
End Sub

Now I is not incremented - Program checks record seven again. Works

fine.
but there are two things I don't like.

1) Instead of `For I = 2 To 500,' I would the loop to just automatically

run
until every record is checked:
`For I = 2 To EndOfColumn.'
There must be way to do that, but I don't know what it is.

2) I don't like screwing with I. I'm just an amateur, but I bet `real'
programmers never alter the value of a loop counter inside the loop

itself.
(Do they??)

Sam
--
A man who had lately declared
That property ought to be shared,
Thought it going too far
When they called for his car,
And a list of exceptions prepared.

Thomas Thorneley,
From The Penguin
Book Of Limericks




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Delete Entire Row.

Even better (at least in terms of speed) is eliminating all the
incremental deletions and deleting all the rows at once:

Public Sub Eliminate()
Const cFullName = 4
Dim rCell As Range
Dim rDelete As Range
With Worksheets(1)
For Each rCell In .Range(.Cells(1, cFullName), _
.Cells(.Rows.Count, cFullName).End(xlUp))
With rCell
If Instr(.Text, " COMPANY", vbTextCompare) < 0 Then
If rDelete Is Nothing Then
Set rDelete = .Cells
Else
Set rDelete = Union(rDelete, .Cells)
End If
End If
End With
Next rCell
End With
If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
End Sub

or, if " COMPANY" is expected to occur rarely, do a .Find() loop.




In article ,
"Rob van Gelder" wrote:

Best to work from bottom to up so you don't have to deal with these types of
issues.

If you must go from top to bottom, then this code should work:

Sub Eliminate()
Const cFullName = 4
Dim i As Long, j As Long

With Worksheets(1)
i = 2: j = .Cells(.Rows.Count, cFullName).End(xlUp).Row
Do Until i j
If InStr(1, .Cells(i, cFullName).Value, " COMPANY",
vbTextCompare) < 0 Then
.Rows(i).EntireRow.Delete
j = j - 1
Else
i = i + 1
End If
Loop
End With
End Sub



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Delete Entire Row.

An excellent point worth noting.

My advise was more to demonstrate alternate looping, which the poster seemed
to struggle with.


"J.E. McGimpsey" wrote in message
...
Even better (at least in terms of speed) is eliminating all the
incremental deletions and deleting all the rows at once:

Public Sub Eliminate()
Const cFullName = 4
Dim rCell As Range
Dim rDelete As Range
With Worksheets(1)
For Each rCell In .Range(.Cells(1, cFullName), _
.Cells(.Rows.Count, cFullName).End(xlUp))
With rCell
If Instr(.Text, " COMPANY", vbTextCompare) < 0 Then
If rDelete Is Nothing Then
Set rDelete = .Cells
Else
Set rDelete = Union(rDelete, .Cells)
End If
End If
End With
Next rCell
End With
If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
End Sub

or, if " COMPANY" is expected to occur rarely, do a .Find() loop.




In article ,
"Rob van Gelder" wrote:

Best to work from bottom to up so you don't have to deal with these

types of
issues.

If you must go from top to bottom, then this code should work:

Sub Eliminate()
Const cFullName = 4
Dim i As Long, j As Long

With Worksheets(1)
i = 2: j = .Cells(.Rows.Count, cFullName).End(xlUp).Row
Do Until i j
If InStr(1, .Cells(i, cFullName).Value, " COMPANY",
vbTextCompare) < 0 Then
.Rows(i).EntireRow.Delete
j = j - 1
Else
i = i + 1
End If
Loop
End With
End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete an entire row One-Leg Excel Discussion (Misc queries) 13 November 11th 08 08:27 PM
Delete entire row if David T Excel Discussion (Misc queries) 2 December 6th 06 10:14 PM
CANNOT DELETE AN ENTIRE COLUMN ibeetb Excel Discussion (Misc queries) 4 June 23rd 06 02:55 AM
Can I delete an entire row if condition is not met? Christine Excel Worksheet Functions 8 May 4th 06 09:47 AM
Delete Entire Rows Wally Steadman[_3_] Excel Programming 2 November 20th 03 08:01 AM


All times are GMT +1. The time now is 07:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"