PC Review


Reply
Thread Tools Rate Thread

code to remove rows containing a character string

 
 
kevin
Guest
Posts: n/a
 
      20th Nov 2008
I want to create a macro to remove all the rows of a table in which the cell
in column C contain "FM" within the text string. For some reason, when I set
up a macro to do a filter of "contains FM" then try to delete just those
rows, the macro deletes all rows.

After that ... I need to do the opposite. Need to delete all rows that does
NOT contain "FM".

Any help is appreciated.
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      20th Nov 2008
Hi,

Right click your sheet tab, view code and paste this in and run it and all
rows with FM in the string in Col C will be deleted.

If you then to go on the delete all rows that don't contain FM all yoiur
data will be gone so I think you need to re-visit the second part of your
question

Sub delete_Me()
Dim copyrange As Range
Lastrow = Cells(Cells.Rows.Count, "C").End(xlUp).Row
Set MyRange = Range("C1:C" & Lastrow)
For Each c In MyRange
If InStr(c, "FM") Then 'Red change to suit
If copyrange Is Nothing Then
Set copyrange = c.EntireRow
Else
Set copyrange = Union(copyrange, c.EntireRow)
End If
End If
Next
If Not copyrange Is Nothing Then
copyrange.Delete
End If
End Sub

Mike


"kevin" wrote:

> I want to create a macro to remove all the rows of a table in which the cell
> in column C contain "FM" within the text string. For some reason, when I set
> up a macro to do a filter of "contains FM" then try to delete just those
> rows, the macro deletes all rows.
>
> After that ... I need to do the opposite. Need to delete all rows that does
> NOT contain "FM".
>
> Any help is appreciated.

 
Reply With Quote
 
kevin
Guest
Posts: n/a
 
      20th Nov 2008
Worked great. I have two people working off this table. One of them works
with the ones containing "FM". The other works on those items not containing
"FM". My intent was to set up different macros for each to seperate the
lists out for them.

What changes would I make to delete those not containing "FM"?

Thanks again!

"Mike H" wrote:

> Hi,
>
> Right click your sheet tab, view code and paste this in and run it and all
> rows with FM in the string in Col C will be deleted.
>
> If you then to go on the delete all rows that don't contain FM all yoiur
> data will be gone so I think you need to re-visit the second part of your
> question
>
> Sub delete_Me()
> Dim copyrange As Range
> Lastrow = Cells(Cells.Rows.Count, "C").End(xlUp).Row
> Set MyRange = Range("C1:C" & Lastrow)
> For Each c In MyRange
> If InStr(c, "FM") Then 'Red change to suit
> If copyrange Is Nothing Then
> Set copyrange = c.EntireRow
> Else
> Set copyrange = Union(copyrange, c.EntireRow)
> End If
> End If
> Next
> If Not copyrange Is Nothing Then
> copyrange.Delete
> End If
> End Sub
>
> Mike
>
>
> "kevin" wrote:
>
> > I want to create a macro to remove all the rows of a table in which the cell
> > in column C contain "FM" within the text string. For some reason, when I set
> > up a macro to do a filter of "contains FM" then try to delete just those
> > rows, the macro deletes all rows.
> >
> > After that ... I need to do the opposite. Need to delete all rows that does
> > NOT contain "FM".
> >
> > Any help is appreciated.

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      20th Nov 2008
Kevin,

change this line

If InStr(c, "FM") Then

to This

If InStr(c, "FM") < 1 Then

Mike

"kevin" wrote:

> Worked great. I have two people working off this table. One of them works
> with the ones containing "FM". The other works on those items not containing
> "FM". My intent was to set up different macros for each to seperate the
> lists out for them.
>
> What changes would I make to delete those not containing "FM"?
>
> Thanks again!
>
> "Mike H" wrote:
>
> > Hi,
> >
> > Right click your sheet tab, view code and paste this in and run it and all
> > rows with FM in the string in Col C will be deleted.
> >
> > If you then to go on the delete all rows that don't contain FM all yoiur
> > data will be gone so I think you need to re-visit the second part of your
> > question
> >
> > Sub delete_Me()
> > Dim copyrange As Range
> > Lastrow = Cells(Cells.Rows.Count, "C").End(xlUp).Row
> > Set MyRange = Range("C1:C" & Lastrow)
> > For Each c In MyRange
> > If InStr(c, "FM") Then 'Red change to suit
> > If copyrange Is Nothing Then
> > Set copyrange = c.EntireRow
> > Else
> > Set copyrange = Union(copyrange, c.EntireRow)
> > End If
> > End If
> > Next
> > If Not copyrange Is Nothing Then
> > copyrange.Delete
> > End If
> > End Sub
> >
> > Mike
> >
> >
> > "kevin" wrote:
> >
> > > I want to create a macro to remove all the rows of a table in which the cell
> > > in column C contain "FM" within the text string. For some reason, when I set
> > > up a macro to do a filter of "contains FM" then try to delete just those
> > > rows, the macro deletes all rows.
> > >
> > > After that ... I need to do the opposite. Need to delete all rows that does
> > > NOT contain "FM".
> > >
> > > Any help is appreciated.

 
Reply With Quote
 
kevin
Guest
Posts: n/a
 
      20th Nov 2008
Thanks a lot ... this worked perfect.

"Mike H" wrote:

> Kevin,
>
> change this line
>
> If InStr(c, "FM") Then
>
> to This
>
> If InStr(c, "FM") < 1 Then
>
> Mike
>
> "kevin" wrote:
>
> > Worked great. I have two people working off this table. One of them works
> > with the ones containing "FM". The other works on those items not containing
> > "FM". My intent was to set up different macros for each to seperate the
> > lists out for them.
> >
> > What changes would I make to delete those not containing "FM"?
> >
> > Thanks again!
> >
> > "Mike H" wrote:
> >
> > > Hi,
> > >
> > > Right click your sheet tab, view code and paste this in and run it and all
> > > rows with FM in the string in Col C will be deleted.
> > >
> > > If you then to go on the delete all rows that don't contain FM all yoiur
> > > data will be gone so I think you need to re-visit the second part of your
> > > question
> > >
> > > Sub delete_Me()
> > > Dim copyrange As Range
> > > Lastrow = Cells(Cells.Rows.Count, "C").End(xlUp).Row
> > > Set MyRange = Range("C1:C" & Lastrow)
> > > For Each c In MyRange
> > > If InStr(c, "FM") Then 'Red change to suit
> > > If copyrange Is Nothing Then
> > > Set copyrange = c.EntireRow
> > > Else
> > > Set copyrange = Union(copyrange, c.EntireRow)
> > > End If
> > > End If
> > > Next
> > > If Not copyrange Is Nothing Then
> > > copyrange.Delete
> > > End If
> > > End Sub
> > >
> > > Mike
> > >
> > >
> > > "kevin" wrote:
> > >
> > > > I want to create a macro to remove all the rows of a table in which the cell
> > > > in column C contain "FM" within the text string. For some reason, when I set
> > > > up a macro to do a filter of "contains FM" then try to delete just those
> > > > rows, the macro deletes all rows.
> > > >
> > > > After that ... I need to do the opposite. Need to delete all rows that does
> > > > NOT contain "FM".
> > > >
> > > > Any help is appreciated.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to remove " character from string in c#? Milsnips Microsoft C# .NET 6 8th Apr 2010 04:41 AM
"Add/Remove Rows Code" adds rows on grouped sheets, but won't remove rows. Conan Kelly Microsoft Excel Programming 1 16th Nov 2007 10:41 PM
remove character from string =?Utf-8?B?TmlnZWw=?= Microsoft Access Queries 1 24th May 2007 09:23 PM
getting the character code of a character in a string Velvet Microsoft ASP .NET 9 19th Jan 2006 09:27 PM
Code to Remove Specific Character at End of String =?Utf-8?B?SkhL?= Microsoft Access VBA Modules 5 2nd Jun 2005 12:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:20 AM.