Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Delete rows with specific text


Hi,

Can someone please provide the code that will delete all rows in column
B that contain a cell with the word 'Total'?

Thanks


--
MAYDAY
------------------------------------------------------------------------
MAYDAY's Profile: http://www.excelforum.com/member.php...o&userid=13548
View this thread: http://www.excelforum.com/showthread...hreadid=376890

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Delete rows with specific text


Sub deltotal()

Range("b1").Activate
Set Var = ActiveCell
For x = 1 To 65536
On Error GoTo skp:
g = Application.WorksheetFunction.Search("total", Var)
Set Var = ActiveCell.Offset(1, 0)
ActiveCell.EntireRow.Select
Selection.Delete
GoTo newvar
skp:
Var.Offset(1, 0).Activate
Set Var = ActiveCell
Resume
newvar:
Var.Activate
Next
End Sub


--
barrfly

Excel User - Energy markets
------------------------------------------------------------------------
barrfly's Profile: http://www.excelforum.com/member.php...fo&userid=4141
View this thread: http://www.excelforum.com/showthread...hreadid=376890

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Delete rows with specific text

One way:

Public Sub DeleteRows()
Const sLOOK As String = "Total"
Dim rFound As Range
Dim rDelete As Range
Dim sFoundAddr As String
Set rFound = Columns(2).Find( _
What:=sLOOK, _
LookIn:=xlValues, _
LookAt:=xlPart, _
MatchCase:=False)
If Not rFound Is Nothing Then
Set rDelete = rFound
sFoundAddr = rFound.Address
Do
Set rFound = Columns(2).FindNext(After:=rFound)
Set rDelete = Union(rDelete, rFound)
Loop Until rFound.Address = sFoundAddr
rDelete.EntireRow.Delete
End If
End Sub





In article ,
MAYDAY wrote:

Can someone please provide the code that will delete all rows in column
B that contain a cell with the word 'Total'?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Delete rows with specific text

Here is some code that is a little faster since it does not loop through each
cell...

Public Sub RemoveTotals()
Dim rngToSearch As Range
Dim wks As Worksheet
Dim rngFound As Range

Set wks = ActiveSheet
Set rngToSearch = wks.Columns(2)

Set rngFound = rngToSearch.Find("Total")
If rngFound Is Nothing Then
MsgBox "No Totals Found"
Else
Do
rngFound.EntireRow.Delete
Set rngFound = rngToSearch.FindNext
Loop Until rngFound Is Nothing
End If
End Sub
--
HTH...

Jim Thomlinson


"MAYDAY" wrote:


Hi,

Can someone please provide the code that will delete all rows in column
B that contain a cell with the word 'Total'?

Thanks


--
MAYDAY
------------------------------------------------------------------------
MAYDAY's Profile: http://www.excelforum.com/member.php...o&userid=13548
View this thread: http://www.excelforum.com/showthread...hreadid=376890


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Delete rows with specific text

How much faster is it to Union everthing into one big range and delete that
at the end as you have done as opposed to deleteing as you go as my code has
done. Have you ever checked? (Note to MayDay... Use this code it should be
the most efficient)
--
HTH...

Jim Thomlinson


"JE McGimpsey" wrote:

One way:

Public Sub DeleteRows()
Const sLOOK As String = "Total"
Dim rFound As Range
Dim rDelete As Range
Dim sFoundAddr As String
Set rFound = Columns(2).Find( _
What:=sLOOK, _
LookIn:=xlValues, _
LookAt:=xlPart, _
MatchCase:=False)
If Not rFound Is Nothing Then
Set rDelete = rFound
sFoundAddr = rFound.Address
Do
Set rFound = Columns(2).FindNext(After:=rFound)
Set rDelete = Union(rDelete, rFound)
Loop Until rFound.Address = sFoundAddr
rDelete.EntireRow.Delete
End If
End Sub





In article ,
MAYDAY wrote:

Can someone please provide the code that will delete all rows in column
B that contain a cell with the word 'Total'?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Delete rows with specific text

Since deleting every row requires reindexing all the remaining rows, and
deleting all at once apparently doesn't, it's variable. The gain in
efficiency should increase in proportion to both hits and total rows. In
a (very) informal test with 16000 rows and 1000 hits, deleting each row
took about 6 times as long as deleting all at once.


In article ,
"Jim Thomlinson" wrote:

How much faster is it to Union everthing into one big range and delete that
at the end as you have done as opposed to deleteing as you go as my code has
done. Have you ever checked? (Note to MayDay... Use this code it should be
the most efficient)

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Delete rows with specific text

I knew it would be more efficient but that is a lot. I will go the extra
distance in the future to create one big range to delete at the end...
Thanks... It never ceases to amaze me how much info you can pick up from this
forum!
--
HTH...

Jim Thomlinson


"JE McGimpsey" wrote:

Since deleting every row requires reindexing all the remaining rows, and
deleting all at once apparently doesn't, it's variable. The gain in
efficiency should increase in proportion to both hits and total rows. In
a (very) informal test with 16000 rows and 1000 hits, deleting each row
took about 6 times as long as deleting all at once.


In article ,
"Jim Thomlinson" wrote:

How much faster is it to Union everthing into one big range and delete that
at the end as you have done as opposed to deleteing as you go as my code has
done. Have you ever checked? (Note to MayDay... Use this code it should be
the most efficient)


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Delete rows with specific text


Thank you all

--
MAYDA
-----------------------------------------------------------------------
MAYDAY's Profile: http://www.excelforum.com/member.php...fo&userid=1354
View this thread: http://www.excelforum.com/showthread.php?threadid=37689

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Delete rows with specific text

Here is some code that is a little faster since it does not loop through
each
cell...



I tested that on 10,000 rows where every other one said "Total". You're
right, it's a lot faster than the other.

the way I do that is by passing a formula into a routine... (acknowledged,
my method would not work if there were not 1 free column at the far right of
the spreadsheet).

the del_rows subroutine accepts any formula... if it meets the condition, it
evaluates to "", if it doesn't, it evaluates to 1.

the rountine puts the formula in, copies it to the end of the data range,
sorts by the result, and deletes the ones that met the delete condition.

In the same test, that method took about half a second.
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 rows with specific text David New Users to Excel 5 April 3rd 23 07:29 PM
Delete Rows Without Specific Text waggett Excel Worksheet Functions 6 October 6th 09 11:34 AM
select and delete specific rows Paulg Excel Discussion (Misc queries) 1 August 22nd 06 04:12 PM
Delete rows with specific leftmost value CLR Excel Programming 2 November 20th 04 02:02 AM
Macro to delete specific rows Steve Excel Programming 12 October 1st 04 11:50 PM


All times are GMT +1. The time now is 02:59 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"