Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Delete rows that don't contain...

Hi,

I'm trying to write a macro the deletes all rows that don't contain the
numbers 10 in row A. The numbers will range from 102000 to 1099999. I want to
delete all rows that don't have any of these numbers in the first column.

Thanks for any help
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default Delete rows that don't contain...

Hi,

I think your saying that if the number in column A doesn't begin with 10
then delete the entire row. If that's correct then right click your sheet
tab, view code and paste this in and run it. If that's wrong then post back.

Sub Standard()
mycolumn = "A" 'Change to suit
Dim MyRange, MyRange1 As Range
LastRow = Cells(Rows.Count, mycolumn).End(xlUp).Row
Set MyRange = Range(mycolumn & "1:" & mycolumn & LastRow)
For Each c In MyRange
If InStr(CStr((c.Value)), "10") < 1 Then
If MyRange1 Is Nothing Then
Set MyRange1 = c.EntireRow
Else
Set MyRange1 = Union(MyRange1, c.EntireRow)
End If
End If
Next
If Not MyRange1 Is Nothing Then
MyRange1.Delete
End If
End Sub

Mike

"nrfinsa" wrote:

Hi,

I'm trying to write a macro the deletes all rows that don't contain the
numbers 10 in row A. The numbers will range from 102000 to 1099999. I want to
delete all rows that don't have any of these numbers in the first column.

Thanks for any help

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default Delete rows that don't contain...

Hi,

Reading your post again you do say 'Don't contain' so change

If InStr(CStr((c.Value)), "10") < 1 Then

to

If InStr(CStr(c.Value), "10") = 0 Then

and it will dlete every row that doesn't 'contain' a 10. Note i've knocked
out a set of parenthesis that weren't necessary.

Mike

"Mike H" wrote:

Hi,

I think your saying that if the number in column A doesn't begin with 10
then delete the entire row. If that's correct then right click your sheet
tab, view code and paste this in and run it. If that's wrong then post back.

Sub Standard()
mycolumn = "A" 'Change to suit
Dim MyRange, MyRange1 As Range
LastRow = Cells(Rows.Count, mycolumn).End(xlUp).Row
Set MyRange = Range(mycolumn & "1:" & mycolumn & LastRow)
For Each c In MyRange
If InStr(CStr((c.Value)), "10") < 1 Then
If MyRange1 Is Nothing Then
Set MyRange1 = c.EntireRow
Else
Set MyRange1 = Union(MyRange1, c.EntireRow)
End If
End If
Next
If Not MyRange1 Is Nothing Then
MyRange1.Delete
End If
End Sub

Mike

"nrfinsa" wrote:

Hi,

I'm trying to write a macro the deletes all rows that don't contain the
numbers 10 in row A. The numbers will range from 102000 to 1099999. I want to
delete all rows that don't have any of these numbers in the first column.

Thanks for any help

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Delete rows that don't contain...


Thanks Mike,

That worked perfectly :-)

Neville

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 126
Default Delete rows that don't contain...

Mike

Is it possible to add other choices to keep rows, This works fine for my
number range, but I would like to keep 17 and 2 types of text SHS and TFG and
then delete all other rows

Regards

Johnny

"Mike H" wrote:

Hi,

I think your saying that if the number in column A doesn't begin with 10
then delete the entire row. If that's correct then right click your sheet
tab, view code and paste this in and run it. If that's wrong then post back.

Sub Standard()
mycolumn = "A" 'Change to suit
Dim MyRange, MyRange1 As Range
LastRow = Cells(Rows.Count, mycolumn).End(xlUp).Row
Set MyRange = Range(mycolumn & "1:" & mycolumn & LastRow)
For Each c In MyRange
If InStr(CStr((c.Value)), "10") < 1 Then
If MyRange1 Is Nothing Then
Set MyRange1 = c.EntireRow
Else
Set MyRange1 = Union(MyRange1, c.EntireRow)
End If
End If
Next
If Not MyRange1 Is Nothing Then
MyRange1.Delete
End If
End Sub

Mike

"nrfinsa" wrote:

Hi,

I'm trying to write a macro the deletes all rows that don't contain the
numbers 10 in row A. The numbers will range from 102000 to 1099999. I want to
delete all rows that don't have any of these numbers in the first column.

Thanks for any help



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
I want to delete all even rows? burch Excel Discussion (Misc queries) 1 May 14th 08 02:30 AM
How to Delete empty rows in excel in b/w rows with values Dennis Excel Worksheet Functions 3 August 28th 07 04:15 PM
Cut filtered rows, paste into next empty row of new sheet, and delete cut rows Scott Excel Worksheet Functions 0 December 13th 06 01:25 AM
How to delete rows when List toolbar's "delete" isnt highlighted? Linda Excel Worksheet Functions 1 May 26th 05 08:39 PM
delete empty rows between rows with text Paulo Baptista Excel Discussion (Misc queries) 2 February 28th 05 03:41 PM


All times are GMT +1. The time now is 05:29 PM.

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"