Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Delete macros

How do you delete a row when it contains a text string within other text
--
DSM
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Delete macros

Look at the VBA help example on the FindNext method.

Just search for the string using find/FindNext, then delete the row when it
is found.

--
Regards,
Tom Ogilvy


"Dave" wrote:

How do you delete a row when it contains a text string within other text
--
DSM

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Delete macros

I'll clarify my problem. I have hundreds of rows of data with the word "logo"
nested within other text in a cell. I need a macro to delete those rows. The
following will delete rows if the ENTIRE text in the cell matches the text in
quotes in this statement:

If Cells(iRow, 1) = "Hours of Operation" Then Rows(iRow).Delete

How can I make this work for my problem?

Thanks
--
DSM


"Tom Ogilvy" wrote:

Look at the VBA help example on the FindNext method.

Just search for the string using find/FindNext, then delete the row when it
is found.

--
Regards,
Tom Ogilvy


"Dave" wrote:

How do you delete a row when it contains a text string within other text
--
DSM

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Delete macros

I'll clarify my problem. I have hundreds of rows of data with the word
"logo"
nested within other text in a cell. I need a macro to delete those rows.
The
following will delete rows if the ENTIRE text in the cell matches the text
in
quotes in this statement:

If Cells(iRow, 1) = "Hours of Operation" Then Rows(iRow).Delete

How can I make this work for my problem?


How about this...

If InStr(Cells(iRow, 1), "logo") 0 Then Rows(iRow).Delete

if you want your match to be case sensitive; or this, if you want a case
insensitive match...

If InStr(1, Cells(iRow, 1), "logo", vbTextCompare) 0 Then
Rows(iRow).Delete

**NOTE**
===============
InStr does not have a built-in way of distinguishing whole words. That means
if you search word were, to make it easy, "the", you would find the whole
word "the", but you would also get a match from the word "other" because
"the" is in the middle of it. So you have to be aware of the potential for
your search word to be inside other words which could give you unintended
matches when using InStr.

Rick

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Delete macros

Good point, Rick! So if just looking for the *word* "logo" then the VBA code
would be:
If InStr(1, Cells(iRow, 1), " logo ", vbTextCompare) 0 Then
Rows(iRow).Delete
End If
Is this correct, putting a space before and after the word?

Toby

"Rick Rothstein (MVP - VB)" wrote:

I'll clarify my problem. I have hundreds of rows of data with the word
"logo"
nested within other text in a cell. I need a macro to delete those rows.
The
following will delete rows if the ENTIRE text in the cell matches the text
in
quotes in this statement:

If Cells(iRow, 1) = "Hours of Operation" Then Rows(iRow).Delete

How can I make this work for my problem?


How about this...

If InStr(Cells(iRow, 1), "logo") 0 Then Rows(iRow).Delete

if you want your match to be case sensitive; or this, if you want a case
insensitive match...

If InStr(1, Cells(iRow, 1), "logo", vbTextCompare) 0 Then
Rows(iRow).Delete

**NOTE**
===============
InStr does not have a built-in way of distinguishing whole words. That means
if you search word were, to make it easy, "the", you would find the whole
word "the", but you would also get a match from the word "other" because
"the" is in the middle of it. So you have to be aware of the potential for
your search word to be inside other words which could give you unintended
matches when using InStr.

Rick




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Delete macros

Good point, Rick! So if just looking for the *word* "logo" then the VBA
code
would be:
If InStr(1, Cells(iRow, 1), " logo ", vbTextCompare) 0 Then
Rows(iRow).Delete
End If
Is this correct, putting a space before and after the word?


Maybe. What if logo is at the end of a sentence, then it is followed by a
period, not a space. Same goes for colons, semi-colon, question marks,
exclamation marks, opening and closing parentheses, brackets (square and
angled), etc., etc. It is not an easy problem to resolve. The key to making
it work is to be looking for very unique words or multiple words with spaces
between them... these kind of things tend to be unique enough to guarantee
no false positives. Although the multiple word ones can run into a problem
if they are split at the end of a forced line feed.

Rick

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Delete macros

Good point, Rick! So if just looking for the *word* "logo" then the VBA
code
would be:
If InStr(1, Cells(iRow, 1), " logo ", vbTextCompare) 0 Then
Rows(iRow).Delete
End If
Is this correct, putting a space before and after the word?


Maybe. What if logo is at the end of a sentence, then it is followed by a
period, not a space. Same goes for colons, semi-colon, question marks,
exclamation marks, opening and closing parentheses, brackets (square and
angled), etc., etc. It is not an easy problem to resolve. The key to
making it work is to be looking for very unique words or multiple words
with spaces between them... these kind of things tend to be unique enough
to guarantee no false positives. Although the multiple word ones can run
into a problem if they are split at the end of a forced line feed.


I should have added... you can overcome these problems by running a loop
using Replace to change all of the word-limiting characters to single
spaces... then you can use the code you posted; however, this will slow
things down because of the multiple string thrashing that will take place
with each execution of a Replace function.

Rick

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Delete macros

I've subtituted your recommended statement in the macro below and it did not
delete the rows. What is missing?

Sub DeleteRowsWithTextString()
' This macro deletes all rows on the active worksheet
' that have have a specific word in column A.
Dim iRow As Long
Dim LastRow As Long
LastRow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1
For iRow = LastRow To 1 Step -1
If InStr(1, Cells(iRow, 1), " logo ", vbTextCompare) 0 Then
Rows(iRow).Delete
Next iRow
End Sub

Thanks
--
DSM


"Rick Rothstein (MVP - VB)" wrote:

Good point, Rick! So if just looking for the *word* "logo" then the VBA
code
would be:
If InStr(1, Cells(iRow, 1), " logo ", vbTextCompare) 0 Then
Rows(iRow).Delete
End If
Is this correct, putting a space before and after the word?


Maybe. What if logo is at the end of a sentence, then it is followed by a
period, not a space. Same goes for colons, semi-colon, question marks,
exclamation marks, opening and closing parentheses, brackets (square and
angled), etc., etc. It is not an easy problem to resolve. The key to
making it work is to be looking for very unique words or multiple words
with spaces between them... these kind of things tend to be unique enough
to guarantee no false positives. Although the multiple word ones can run
into a problem if they are split at the end of a forced line feed.


I should have added... you can overcome these problems by running a loop
using Replace to change all of the word-limiting characters to single
spaces... then you can use the code you posted; however, this will slow
things down because of the multiple string thrashing that will take place
with each execution of a Replace function.

Rick


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Delete macros

Disregard my previous post. I removed the blank after " logo" in the macro
statement and it worked!

BTW, for my macro education, where could I find a comprehensive glossary on
the web for macro terms and their function?

Thanks Rick and Toby!

Dave
--
DSM


"Rick Rothstein (MVP - VB)" wrote:

Good point, Rick! So if just looking for the *word* "logo" then the VBA
code
would be:
If InStr(1, Cells(iRow, 1), " logo ", vbTextCompare) 0 Then
Rows(iRow).Delete
End If
Is this correct, putting a space before and after the word?


Maybe. What if logo is at the end of a sentence, then it is followed by a
period, not a space. Same goes for colons, semi-colon, question marks,
exclamation marks, opening and closing parentheses, brackets (square and
angled), etc., etc. It is not an easy problem to resolve. The key to
making it work is to be looking for very unique words or multiple words
with spaces between them... these kind of things tend to be unique enough
to guarantee no false positives. Although the multiple word ones can run
into a problem if they are split at the end of a forced line feed.


I should have added... you can overcome these problems by running a loop
using Replace to change all of the word-limiting characters to single
spaces... then you can use the code you posted; however, this will slow
things down because of the multiple string thrashing that will take place
with each execution of a Replace function.

Rick


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Delete macros

Disregard my previous post. I removed the blank after " logo"
in the macro statement and it worked!


It won't find it in something like this though...

Company (logo goes here)

or this...

This "logo" represents....

Only you know how your data is constructed and whether other characters can
appear in front of the word. The word "logo" is relatively unique and you
might get away without using any surrounding spaces. Of course, that means
you would have to watch out for words like "logon", "logoff" and "logout".

Rick



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Delete macros

See also this page and read the tips
http://www.rondebruin.nl/delete.htm


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Dave" wrote in message ...
I'll clarify my problem. I have hundreds of rows of data with the word "logo"
nested within other text in a cell. I need a macro to delete those rows. The
following will delete rows if the ENTIRE text in the cell matches the text in
quotes in this statement:

If Cells(iRow, 1) = "Hours of Operation" Then Rows(iRow).Delete

How can I make this work for my problem?

Thanks
--
DSM


"Tom Ogilvy" wrote:

Look at the VBA help example on the FindNext method.

Just search for the string using find/FindNext, then delete the row when it
is found.

--
Regards,
Tom Ogilvy


"Dave" wrote:

How do you delete a row when it contains a text string within other text
--
DSM

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Delete macros

See also this page and read the tips
http://www.rondebruin.nl/delete.htm


Thanks for the link. Can I assume xlWhole makes Find search on whole words?
(I tried looking it up, but Excel's VBA help files wouldn't locate it.)

Rick

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Delete macros

Hi Rick

Yes

You can also use use xlPart


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in message ...
See also this page and read the tips
http://www.rondebruin.nl/delete.htm


Thanks for the link. Can I assume xlWhole makes Find search on whole words?
(I tried looking it up, but Excel's VBA help files wouldn't locate it.)

Rick

  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Delete macros

Yeah, I figured if xlWhole was "whole word", then xlPart would be non-whole
words.<g

Is there a "fuller" documentation on these defined constants? Searching for
xlWhole brought up three links, one of which was "Microsoft Excel
Constants", but xlWhole (nor xlPart) was not in the long list of constants
shown.

Rick


"Ron de Bruin" wrote in message
...
Hi Rick

Yes

You can also use use xlPart

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in
message ...
See also this page and read the tips
http://www.rondebruin.nl/delete.htm


Thanks for the link. Can I assume xlWhole makes Find search on whole
words? (I tried looking it up, but Excel's VBA help files wouldn't locate
it.)

Rick


  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Delete macros

I not think there is more information then that is in the VBA help for "Range.Find Method"


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in message ...
Yeah, I figured if xlWhole was "whole word", then xlPart would be non-whole
words.<g

Is there a "fuller" documentation on these defined constants? Searching for
xlWhole brought up three links, one of which was "Microsoft Excel
Constants", but xlWhole (nor xlPart) was not in the long list of constants
shown.

Rick


"Ron de Bruin" wrote in message
...
Hi Rick

Yes

You can also use use xlPart

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in
message ...
See also this page and read the tips
http://www.rondebruin.nl/delete.htm

Thanks for the link. Can I assume xlWhole makes Find search on whole
words? (I tried looking it up, but Excel's VBA help files wouldn't locate
it.)

Rick




  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Delete macros

Thanks for your link. The "VBA help" --
DSM


"Ron de Bruin" wrote:

I not think there is more information then that is in the VBA help for "Range.Find Method"


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in message ...
Yeah, I figured if xlWhole was "whole word", then xlPart would be non-whole
words.<g

Is there a "fuller" documentation on these defined constants? Searching for
xlWhole brought up three links, one of which was "Microsoft Excel
Constants", but xlWhole (nor xlPart) was not in the long list of constants
shown.

Rick


"Ron de Bruin" wrote in message
...
Hi Rick

Yes

You can also use use xlPart

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in
message ...
See also this page and read the tips
http://www.rondebruin.nl/delete.htm

Thanks for the link. Can I assume xlWhole makes Find search on whole
words? (I tried looking it up, but Excel's VBA help files wouldn't locate
it.)

Rick



  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Delete macros

(Disregard my last post - it was accidental.)

Thanks for the link. Is their a good search term to use in VBA help to
access a list of macro functions?
--
DSM


"Ron de Bruin" wrote:

I not think there is more information then that is in the VBA help for "Range.Find Method"


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in message ...
Yeah, I figured if xlWhole was "whole word", then xlPart would be non-whole
words.<g

Is there a "fuller" documentation on these defined constants? Searching for
xlWhole brought up three links, one of which was "Microsoft Excel
Constants", but xlWhole (nor xlPart) was not in the long list of constants
shown.

Rick


"Ron de Bruin" wrote in message
...
Hi Rick

Yes

You can also use use xlPart

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Rick Rothstein (MVP - VB)" wrote in
message ...
See also this page and read the tips
http://www.rondebruin.nl/delete.htm

Thanks for the link. Can I assume xlWhole makes Find search on whole
words? (I tried looking it up, but Excel's VBA help files wouldn't locate
it.)

Rick



  #18   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Delete macros

I not think there is more information then that is in the VBA help for
"Range.Find Method"


The description seem kind of "skimpy"<g Oh well, maybe I'll try to find a
good reference book at the bookstore.

Rick

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 All Macros Hardeep_kanwar[_2_] Excel Worksheet Functions 1 October 31st 08 11:21 AM
delete macros AhmetDY Excel Discussion (Misc queries) 2 October 18th 07 10:07 AM
delete macros sandy Excel Worksheet Functions 1 July 11th 07 11:01 PM
How to Delete Another WorkBook Macros using Macros.. Possible? ddiicc Excel Programming 7 July 24th 05 01:54 PM
How to delete macros Jaime Stuardo Excel Discussion (Misc queries) 2 February 14th 05 02:29 PM


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