PC Review


Reply
Thread Tools Rate Thread

Delete value if found in cell.

 
 
Sinner
Guest
Posts: n/a
 
      25th Mar 2008
Hi,


I want to delete work total if found in the sheet.
The problem with below is that it is only deleting case sensitive
values.

ElseIf c.Value Like "*TOTAL*" Then ' Case sensitive
c.EntireRow.Delete

Any fix would be appreciated.
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      25th Mar 2008
Maybe

UCase(c.Value) Like "*TOTAL*" Then

Mike

"Sinner" wrote:

> Hi,
>
>
> I want to delete work total if found in the sheet.
> The problem with below is that it is only deleting case sensitive
> values.
>
> ElseIf c.Value Like "*TOTAL*" Then ' Case sensitive
> c.EntireRow.Delete
>
> Any fix would be appreciated.
>

 
Reply With Quote
 
Sinner
Guest
Posts: n/a
 
      25th Mar 2008
On Mar 25, 5:41*pm, Mike H <Mi...@discussions.microsoft.com> wrote:
> Maybe
>
> UCase(c.Value) Like "*TOTAL*" Then
>
> Mike
>
>
>
> "Sinner" wrote:
> > Hi,

>
> > I want to delete work total if found in the sheet.
> > The problem with below is that it is only deleting case sensitive
> > values.

>
> > ElseIf c.Value Like "*TOTAL*" Then ' Case sensitive
> > * * * * c.EntireRow.Delete

>
> > Any fix would be appreciated.- Hide quoted text -

>
> - Show quoted text -


Mike,
It is not deleting total payables, Grand Total etc.
 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      25th Mar 2008
On Tue, 25 Mar 2008 05:30:57 -0700 (PDT), Sinner <(E-Mail Removed)> wrote:

>Hi,
>
>
>I want to delete work total if found in the sheet.
>The problem with below is that it is only deleting case sensitive
>values.
>
>ElseIf c.Value Like "*TOTAL*" Then ' Case sensitive
> c.EntireRow.Delete
>
>Any fix would be appreciated.


You could precede your module with

Option Compare Text

e.g.:

Option Explicit
Option Compare Text
Sub foo()
Const s1 As String = "TOTAL"
Const s2 As String = "total"

Debug.Print IIf(s1 Like "TOTAL", True, False)
Debug.Print IIf(s2 Like "TOTAL", True, False)
End Sub
--ron
 
Reply With Quote
 
Sinner
Guest
Posts: n/a
 
      25th Mar 2008
On Mar 25, 6:10*pm, Ron Rosenfeld <ronrosenf...@nospam.org> wrote:
> On Tue, 25 Mar 2008 05:30:57 -0700 (PDT), Sinner <ims...@gmail.com> wrote:
> >Hi,

>
> >I want to delete work total if found in the sheet.
> >The problem with below is that it is only deleting case sensitive
> >values.

>
> >ElseIf c.Value Like "*TOTAL*" Then ' Case sensitive
> > * * * *c.EntireRow.Delete

>
> >Any fix would be appreciated.

>
> You could precede your module with
>
> Option Compare Text
>
> e.g.:
>
> Option Explicit
> Option Compare Text
> Sub foo()
> Const s1 As String = "TOTAL"
> Const s2 As String = "total"
>
> Debug.Print IIf(s1 Like "TOTAL", True, False)
> Debug.Print IIf(s2 Like "TOTAL", True, False)
> End Sub
> --ron


This is where i need a fix

For Each c In Range("A1").CurrentRegion
If c.Value Like "----*" Or _
c.Value Like "====*" Or _
UCase(c.Value) Like "*TOTAL*" Then ' Case sensitive

c.EntireRow.Delete

End If
Next
 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      25th Mar 2008
On Tue, 25 Mar 2008 06:34:18 -0700 (PDT), Sinner <(E-Mail Removed)> wrote:

>This is where i need a fix
>
>For Each c In Range("A1").CurrentRegion
> If c.Value Like "----*" Or _
> c.Value Like "====*" Or _
> UCase(c.Value) Like "*TOTAL*" Then ' Case sensitive
>
> c.EntireRow.Delete
>
> End If


Using UCASE will work to make your TOTAL case insensitive.

What I gave was another method:

"You could precede your module with
Option Compare Text"

So, as I showed in my example, prior to your SUB statement, you place the line:

Option Compare Text


--ron
 
Reply With Quote
 
Sinner
Guest
Posts: n/a
 
      25th Mar 2008
On Mar 25, 6:51*pm, Ron Rosenfeld <ronrosenf...@nospam.org> wrote:
> On Tue, 25 Mar 2008 06:34:18 -0700 (PDT), Sinner <ims...@gmail.com> wrote:
> >This is where i need a fix

>
> >For Each c In Range("A1").CurrentRegion
> > * *If c.Value Like "----*" Or _
> > * *c.Value Like "====*" Or _
> > * *UCase(c.Value) Like "*TOTAL*" Then ' Case sensitive

>
> > * * * *c.EntireRow.Delete

>
> > * *End If

>
> Using UCASE will work to make your TOTAL case insensitive.
>
> What I gave was another method:
>
> "You could precede your module with
> Option Compare Text"
>
> So, as I showed in my example, prior to your SUB statement, you place the line:
>
> Option Compare Text
>
> --ron


When I use "*TOTAL*", it is deleting only word TOTAL.
When I use "*TOTAL *", it is deleting TOTAL PAYABLE, GRAND TOTAL etc.
and not TOTAL only.

Any ideas?
 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      25th Mar 2008
On Tue, 25 Mar 2008 07:20:05 -0700 (PDT), Sinner <(E-Mail Removed)> wrote:

>On Mar 25, 6:51*pm, Ron Rosenfeld <ronrosenf...@nospam.org> wrote:
>> On Tue, 25 Mar 2008 06:34:18 -0700 (PDT), Sinner <ims...@gmail.com> wrote:
>> >This is where i need a fix

>>
>> >For Each c In Range("A1").CurrentRegion
>> > * *If c.Value Like "----*" Or _
>> > * *c.Value Like "====*" Or _
>> > * *UCase(c.Value) Like "*TOTAL*" Then ' Case sensitive

>>
>> > * * * *c.EntireRow.Delete

>>
>> > * *End If

>>
>> Using UCASE will work to make your TOTAL case insensitive.
>>
>> What I gave was another method:
>>
>> "You could precede your module with
>> Option Compare Text"
>>
>> So, as I showed in my example, prior to your SUB statement, you place the line:
>>
>> Option Compare Text
>>
>> --ron

>
>When I use "*TOTAL*", it is deleting only word TOTAL.
>When I use "*TOTAL *", it is deleting TOTAL PAYABLE, GRAND TOTAL etc.
>and not TOTAL only.
>
>Any ideas?


I'm not sure I understand what you want to do.

In your first post, and subsequent posts, it appears as if you want to delete
the entire row if "TOTAL" is found in 'Range("A1").CurrentRegion' but were
having a problem because of your code being case-sensitive.

You were given two different solutions for that.

If you want to delete the entire row if you find ONLY the word TOTAL in some
cell in 'Range("A1").CurrentRegion', then omit the "*"'s.

If you just want to delete the word "TOTAL" from the cell, take a look at the
Replace method.

If you want to do something else, you will need to be more specific.
--ron
 
Reply With Quote
 
Sinner
Guest
Posts: n/a
 
      25th Mar 2008
On Mar 25, 8:43*pm, Ron Rosenfeld <ronrosenf...@nospam.org> wrote:
> On Tue, 25 Mar 2008 07:20:05 -0700 (PDT), Sinner <ims...@gmail.com> wrote:
> >On Mar 25, 6:51*pm, Ron Rosenfeld <ronrosenf...@nospam.org> wrote:
> >> On Tue, 25 Mar 2008 06:34:18 -0700 (PDT), Sinner <ims...@gmail.com> wrote:
> >> >This is where i need a fix

>
> >> >For Each c In Range("A1").CurrentRegion
> >> > * *If c.Value Like "----*" Or _
> >> > * *c.Value Like "====*" Or _
> >> > * *UCase(c.Value) Like "*TOTAL*" Then ' Case sensitive

>
> >> > * * * *c.EntireRow.Delete

>
> >> > * *End If

>
> >> Using UCASE will work to make your TOTAL case insensitive.

>
> >> What I gave was another method:

>
> >> "You could precede your module with
> >> Option Compare Text"

>
> >> So, as I showed in my example, prior to your SUB statement, you place the line:

>
> >> Option Compare Text

>
> >> --ron

>
> >When I use "*TOTAL*", it is deleting only word TOTAL.
> >When I use "*TOTAL *", it is deleting TOTAL PAYABLE, GRAND TOTAL etc.
> >and not TOTAL only.

>
> >Any ideas?

>
> I'm not sure I understand what you want to do.
>
> In your first post, and subsequent posts, it appears as if you want to delete
> the entire row if "TOTAL" is found in 'Range("A1").CurrentRegion' *but were
> having a problem because of your code being case-sensitive.
>
> You were given two different solutions for that.
>
> If you want to delete the entire row if you find ONLY the word TOTAL in some
> cell in 'Range("A1").CurrentRegion', then omit the "*"'s.
>
> If you just want to delete the word "TOTAL" from the cell, take a look at the
> Replace method.
>
> If you want to do something else, you will need to be more specific.
> --ron- Hide quoted text -
>
> - Show quoted text -


Ronsenfeld I'm importing a sheet with some data.
After the import I want to delete rows if a cell contains word total
in it. it can be only total, total payable, grand total, total ABC,
TOTAL etc.


Now I have used total wihtout "*"s

For Each c In Range("A1").CurrentRegion
If c.Value Like "----*" Or _
c.Value Like "====*" Or _
UCase(c.Value) Like "TOTAL" Then ' Case sensitive

c.EntireRow.Delete

End If
Next

Still I can see total payable in columnE.
 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      25th Mar 2008
On Tue, 25 Mar 2008 09:28:36 -0700 (PDT), Sinner <(E-Mail Removed)> wrote:

>On Mar 25, 8:43*pm, Ron Rosenfeld <ronrosenf...@nospam.org> wrote:
>> On Tue, 25 Mar 2008 07:20:05 -0700 (PDT), Sinner <ims...@gmail.com> wrote:
>> >On Mar 25, 6:51*pm, Ron Rosenfeld <ronrosenf...@nospam.org> wrote:
>> >> On Tue, 25 Mar 2008 06:34:18 -0700 (PDT), Sinner <ims...@gmail.com> wrote:
>> >> >This is where i need a fix

>>
>> >> >For Each c In Range("A1").CurrentRegion
>> >> > * *If c.Value Like "----*" Or _
>> >> > * *c.Value Like "====*" Or _
>> >> > * *UCase(c.Value) Like "*TOTAL*" Then ' Case sensitive

>>
>> >> > * * * *c.EntireRow.Delete

>>
>> >> > * *End If

>>
>> >> Using UCASE will work to make your TOTAL case insensitive.

>>
>> >> What I gave was another method:

>>
>> >> "You could precede your module with
>> >> Option Compare Text"

>>
>> >> So, as I showed in my example, prior to your SUB statement, you place the line:

>>
>> >> Option Compare Text

>>
>> >> --ron

>>
>> >When I use "*TOTAL*", it is deleting only word TOTAL.
>> >When I use "*TOTAL *", it is deleting TOTAL PAYABLE, GRAND TOTAL etc.
>> >and not TOTAL only.

>>
>> >Any ideas?

>>
>> I'm not sure I understand what you want to do.
>>
>> In your first post, and subsequent posts, it appears as if you want to delete
>> the entire row if "TOTAL" is found in 'Range("A1").CurrentRegion' *but were
>> having a problem because of your code being case-sensitive.
>>
>> You were given two different solutions for that.
>>
>> If you want to delete the entire row if you find ONLY the word TOTAL in some
>> cell in 'Range("A1").CurrentRegion', then omit the "*"'s.
>>
>> If you just want to delete the word "TOTAL" from the cell, take a look at the
>> Replace method.
>>
>> If you want to do something else, you will need to be more specific.
>> --ron- Hide quoted text -
>>
>> - Show quoted text -

>
>Ronsenfeld I'm importing a sheet with some data.
>After the import I want to delete rows if a cell contains word total
>in it. it can be only total, total payable, grand total, total ABC,
>TOTAL etc.
>
>
>Now I have used total wihtout "*"s
>
>For Each c In Range("A1").CurrentRegion
> If c.Value Like "----*" Or _
> c.Value Like "====*" Or _
> UCase(c.Value) Like "TOTAL" Then ' Case sensitive
>
> c.EntireRow.Delete
>
> End If
>Next
>
>Still I can see total payable in columnE.


OK, I see what is happening now.

You MUST have the "*" if you want to delete lines where the cells contain TOTAL

In other words ... Like "*TOTAL*" is correct.

The reason you are seeing certain lines where a cell contains TOTAL being left
behind is because your

For each c in ...

next c

loop is not processing all the lines.

For example, let us say you have

A1: Type
A2: Total
A3: Total Payable
A4: Something Else


Row 1 gets retained
Row 2 gets deleted

Once row 2 gets deleted, row 3 moves up to row 2. BUT "next c" no longer
refers to row 2 -- rather it refers to row 3. So the row that originally
contained TOTAL Payable never got processed.


In order to avoid this, you must process your lines from the bottom up, rather
than from the top down.

Assuming your Total might be in column A, then something like:

=================
Option Explicit
Option Compare Text
Sub foo()
Dim c As Range
Dim lLastRow As Long
Dim rw As Long

Set c = Range("A1")
lLastRow = c.End(xlDown).Row

For rw = lLastRow To 1 Step -1
If Cells(rw, 1).Value Like "*TOTAL*" Then
Cells(rw, 1).EntireRow.Delete
End If
Next rw
End Sub
=======================

If not in column A, change the columnindex argument for the Cells property to
reflect the column to check.

If TOTAL might be in more than one column, then set up a sub loop to check each
column as you go up the rows.
--ron
 
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
Find and Delete a specified value IF found in cell in ANY Sheets in a WorkBook Gary Keramidas Microsoft Excel Programming 2 1st Apr 2008 08:54 AM
Delete a cell when found =?Utf-8?B?cmlja3NnbWE=?= Microsoft Excel Programming 4 10th Aug 2007 02:08 PM
How to delete values of a cell if it is found in another coloumn karty Microsoft Excel Worksheet Functions 3 22nd Oct 2005 04:29 PM
Cascading Delete...#Delete found in table...what the heck is going on! Andre Laplume via AccessMonster.com Microsoft Access Queries 6 2nd Jun 2005 06:26 PM
is it safe to delete Found.000,Found.001,Found.002,... (newbie) Sting Microsoft Windows 2000 4 16th Jan 2004 03:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:12 PM.