PC Review


Reply
Thread Tools Rate Thread

Delete rows if value matches

 
 
Rob
Guest
Posts: n/a
 
      12th Mar 2008
Having some success but stuck with checking values in several columns. The
below was taken from Ron de Bruin's web site (which is great I must add) but
I can't get the code to delete rows where the criteria matches. Also, once
sorted, is there an option to do the same but us OR instead of AND eg. if
contents of cell column A = ron, delete or if contents of cell in column B =
dave, delete and so on.

Thanks, Rob

http://www.rondebruin.nl/delete.htm#Loop

'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1

'We check the values in the A column in this example
With .Cells(Lrow, "A")

If Not IsError(.Value) Then

If .Cells(Lrow, "A").Value = "ron" And _
.Cells(Lrow, "B").Value = "dave" And _
.Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete
'If .Value = "ron" Then .EntireRow.Delete
'This will delete each row with the Value "ron"
'in Column A, case sensitive.

End If

End With

Next Lrow


 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      12th Mar 2008
Maybe you have a case problem. Try looking for any spelling Dave, DAVE,
dave,dAvE

If ucase(.Cells(Lrow, "A"))= "RON" And _
> ucase(.Cells(Lrow, "B"))= "DAVE" And _
> .Cells(Lrow, "C").Value > 10 Then .Rows


--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Rob" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Having some success but stuck with checking values in several columns.
> The below was taken from Ron de Bruin's web site (which is great I must
> add) but I can't get the code to delete rows where the criteria matches.
> Also, once sorted, is there an option to do the same but us OR instead of
> AND eg. if contents of cell column A = ron, delete or if contents of cell
> in column B = dave, delete and so on.
>
> Thanks, Rob
>
> http://www.rondebruin.nl/delete.htm#Loop
>
> 'Set the first and last row to loop through
> Firstrow = .UsedRange.Cells(1).Row
> Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
>
> 'We loop from Lastrow to Firstrow (bottom to top)
> For Lrow = Lastrow To Firstrow Step -1
>
> 'We check the values in the A column in this example
> With .Cells(Lrow, "A")
>
> If Not IsError(.Value) Then
>
> If .Cells(Lrow, "A").Value = "ron" And _
> .Cells(Lrow, "B").Value = "dave" And _
> .Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete
> 'If .Value = "ron" Then .EntireRow.Delete
> 'This will delete each row with the Value "ron"
> 'in Column A, case sensitive.
>
> End If
>
> End With
>
> Next Lrow
>
>


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      12th Mar 2008
Remember that VBA's comparisons are case sensitive: Dave <> dave <> DaVe

And if you want or's

If lcase(.Cells(Lrow, "A").Value) = lcase("ron") _
or lcase(.Cells(Lrow, "B").Value) = lcase("dave") _
or .Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete

if you want a mixture of and's and or's, it'll make your code easier to read if
you surround the stuff that goes together with ()'s.

If (lcase(.Cells(Lrow, "A").Value) = lcase("ron") _
and lcase(.Cells(Lrow, "B").Value) = lcase("dave")) _
or .Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete

Column A has to be Ron and at the same time column B has to be Dave.
Or
column C has to be > 10.

If either are true, then delete the row.

Rob wrote:
>
> Having some success but stuck with checking values in several columns. The
> below was taken from Ron de Bruin's web site (which is great I must add) but
> I can't get the code to delete rows where the criteria matches. Also, once
> sorted, is there an option to do the same but us OR instead of AND eg. if
> contents of cell column A = ron, delete or if contents of cell in column B =
> dave, delete and so on.
>
> Thanks, Rob
>
> http://www.rondebruin.nl/delete.htm#Loop
>
> 'Set the first and last row to loop through
> Firstrow = .UsedRange.Cells(1).Row
> Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
>
> 'We loop from Lastrow to Firstrow (bottom to top)
> For Lrow = Lastrow To Firstrow Step -1
>
> 'We check the values in the A column in this example
> With .Cells(Lrow, "A")
>
> If Not IsError(.Value) Then
>
> If .Cells(Lrow, "A").Value = "ron" And _
> .Cells(Lrow, "B").Value = "dave" And _
> .Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete
> 'If .Value = "ron" Then .EntireRow.Delete
> 'This will delete each row with the Value "ron"
> 'in Column A, case sensitive.
>
> End If
>
> End With
>
> Next Lrow


--

Dave Peterson
 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      12th Mar 2008
See the examples below the macro Rob about case
http://www.rondebruin.nl/delete.htm

--

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


"Rob" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Having some success but stuck with checking values in several columns. The
> below was taken from Ron de Bruin's web site (which is great I must add) but
> I can't get the code to delete rows where the criteria matches. Also, once
> sorted, is there an option to do the same but us OR instead of AND eg. if
> contents of cell column A = ron, delete or if contents of cell in column B =
> dave, delete and so on.
>
> Thanks, Rob
>
> http://www.rondebruin.nl/delete.htm#Loop
>
> 'Set the first and last row to loop through
> Firstrow = .UsedRange.Cells(1).Row
> Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
>
> 'We loop from Lastrow to Firstrow (bottom to top)
> For Lrow = Lastrow To Firstrow Step -1
>
> 'We check the values in the A column in this example
> With .Cells(Lrow, "A")
>
> If Not IsError(.Value) Then
>
> If .Cells(Lrow, "A").Value = "ron" And _
> .Cells(Lrow, "B").Value = "dave" And _
> .Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete
> 'If .Value = "ron" Then .EntireRow.Delete
> 'This will delete each row with the Value "ron"
> 'in Column A, case sensitive.
>
> End If
>
> End With
>
> Next Lrow
>
>

 
Reply With Quote
 
Rob
Guest
Posts: n/a
 
      13th Mar 2008
Thanks, Case was my issue and not replacing all the code, thought you had to
retain If Not IsError(.Value) Then....

Regards, Rob

"Ron de Bruin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> See the examples below the macro Rob about case
> http://www.rondebruin.nl/delete.htm
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Rob" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Having some success but stuck with checking values in several columns.
>> The below was taken from Ron de Bruin's web site (which is great I must
>> add) but I can't get the code to delete rows where the criteria matches.
>> Also, once sorted, is there an option to do the same but us OR instead of
>> AND eg. if contents of cell column A = ron, delete or if contents of cell
>> in column B = dave, delete and so on.
>>
>> Thanks, Rob
>>
>> http://www.rondebruin.nl/delete.htm#Loop
>>
>> 'Set the first and last row to loop through
>> Firstrow = .UsedRange.Cells(1).Row
>> Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
>>
>> 'We loop from Lastrow to Firstrow (bottom to top)
>> For Lrow = Lastrow To Firstrow Step -1
>>
>> 'We check the values in the A column in this example
>> With .Cells(Lrow, "A")
>>
>> If Not IsError(.Value) Then
>>
>> If .Cells(Lrow, "A").Value = "ron" And _
>> .Cells(Lrow, "B").Value = "dave" And _
>> .Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete
>> 'If .Value = "ron" Then .EntireRow.Delete
>> 'This will delete each row with the Value "ron"
>> 'in Column A, case sensitive.
>>
>> End If
>>
>> End With
>>
>> Next Lrow
>>


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th Mar 2008
If you always want to use a text comparison (ignore case) for code in that
module, you can add:

Option Compare Text

at the top of the module.


Rob wrote:
>
> Thanks, Case was my issue and not replacing all the code, thought you had to
> retain If Not IsError(.Value) Then....
>
> Regards, Rob
>
> "Ron de Bruin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > See the examples below the macro Rob about case
> > http://www.rondebruin.nl/delete.htm
> >
> > --
> >
> > Regards Ron de Bruin
> > http://www.rondebruin.nl/tips.htm
> >
> >
> > "Rob" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> Having some success but stuck with checking values in several columns.
> >> The below was taken from Ron de Bruin's web site (which is great I must
> >> add) but I can't get the code to delete rows where the criteria matches.
> >> Also, once sorted, is there an option to do the same but us OR instead of
> >> AND eg. if contents of cell column A = ron, delete or if contents of cell
> >> in column B = dave, delete and so on.
> >>
> >> Thanks, Rob
> >>
> >> http://www.rondebruin.nl/delete.htm#Loop
> >>
> >> 'Set the first and last row to loop through
> >> Firstrow = .UsedRange.Cells(1).Row
> >> Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
> >>
> >> 'We loop from Lastrow to Firstrow (bottom to top)
> >> For Lrow = Lastrow To Firstrow Step -1
> >>
> >> 'We check the values in the A column in this example
> >> With .Cells(Lrow, "A")
> >>
> >> If Not IsError(.Value) Then
> >>
> >> If .Cells(Lrow, "A").Value = "ron" And _
> >> .Cells(Lrow, "B").Value = "dave" And _
> >> .Cells(Lrow, "C").Value > 10 Then .Rows(Lrow).Delete
> >> 'If .Value = "ron" Then .EntireRow.Delete
> >> 'This will delete each row with the Value "ron"
> >> 'in Column A, case sensitive.
> >>
> >> End If
> >>
> >> End With
> >>
> >> Next Lrow
> >>


--

Dave Peterson
 
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
compare rows for a match and sum the matches Bert Microsoft Excel Worksheet Functions 1 30th Apr 2009 09:27 PM
Copy rows that matches cell value Kashyap Microsoft Excel Programming 2 3rd Mar 2009 08:36 AM
Returning matches from mutiple rows Sunshine Microsoft Excel Worksheet Functions 4 30th Mar 2008 10:10 PM
Set a Default for rows with no matches =?Utf-8?B?c3RpY2thbmRyb2Nr?= Microsoft Access Queries 2 7th Mar 2005 02:51 PM
Comparing Two Rows and counting matches nurice Microsoft Excel Misc 2 19th May 2004 03:01 PM


Features
 

Advertising
 

Newsgroups
 


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