PC Review


Reply
Thread Tools Rate Thread

Delete Row if value <>

 
 
Aaron
Guest
Posts: n/a
 
      18th Jan 2008
I have the following code that should delete any row that doesn't have the
value of Defective in my range but it is deleting everything. Please help.

lastrow = Cells(Rows.Count, "A").End(xlUp).Row

For Each c In ActiveSheet.Range("C2:C" & lastrow)
If c.Value = "DEFECTIVE" Then
Else
Selection.EntireRow.delete
End If
Next

Thanks in advance, Aaron
 
Reply With Quote
 
 
 
 
Sam Wilson
Guest
Posts: n/a
 
      18th Jan 2008
It's case sensitive, so "DEFECTIVE" is <> "Defective".

use if lcase(c.value)="defective"

Sam

"Aaron" wrote:

> I have the following code that should delete any row that doesn't have the
> value of Defective in my range but it is deleting everything. Please help.
>
> lastrow = Cells(Rows.Count, "A").End(xlUp).Row
>
> For Each c In ActiveSheet.Range("C2:C" & lastrow)
> If c.Value = "DEFECTIVE" Then
> Else
> Selection.EntireRow.delete
> End If
> Next
>
> Thanks in advance, Aaron

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      18th Jan 2008
Hi Aaron

Check out this page for example code
http://www.rondebruin.nl/delete.htm

When you delete rows always start at the bottom and go up

--

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


"Aaron" <(E-Mail Removed)> wrote in message news:0033E17E-CB5D-40F9-ACE4-(E-Mail Removed)...
>I have the following code that should delete any row that doesn't have the
> value of Defective in my range but it is deleting everything. Please help.
>
> lastrow = Cells(Rows.Count, "A").End(xlUp).Row
>
> For Each c In ActiveSheet.Range("C2:C" & lastrow)
> If c.Value = "DEFECTIVE" Then
> Else
> Selection.EntireRow.delete
> End If
> Next
>
> Thanks in advance, Aaron

 
Reply With Quote
 
Aaron
Guest
Posts: n/a
 
      18th Jan 2008
Sorry that is just how I wrote in in the message, I have the "DEFECTIVE" in
the code matching the exact way the cell value is showing.


"Sam Wilson" wrote:

> It's case sensitive, so "DEFECTIVE" is <> "Defective".
>
> use if lcase(c.value)="defective"
>
> Sam
>
> "Aaron" wrote:
>
> > I have the following code that should delete any row that doesn't have the
> > value of Defective in my range but it is deleting everything. Please help.
> >
> > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
> >
> > For Each c In ActiveSheet.Range("C2:C" & lastrow)
> > If c.Value = "DEFECTIVE" Then
> > Else
> > Selection.EntireRow.delete
> > End If
> > Next
> >
> > Thanks in advance, Aaron

 
Reply With Quote
 
Aaron
Guest
Posts: n/a
 
      18th Jan 2008
Worked great except it deleted the header row??
How can I change it?

"Ron de Bruin" wrote:

> Hi Aaron
>
> Check out this page for example code
> http://www.rondebruin.nl/delete.htm
>
> When you delete rows always start at the bottom and go up
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Aaron" <(E-Mail Removed)> wrote in message news:0033E17E-CB5D-40F9-ACE4-(E-Mail Removed)...
> >I have the following code that should delete any row that doesn't have the
> > value of Defective in my range but it is deleting everything. Please help.
> >
> > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
> >
> > For Each c In ActiveSheet.Range("C2:C" & lastrow)
> > If c.Value = "DEFECTIVE" Then
> > Else
> > Selection.EntireRow.delete
> > End If
> > Next
> >
> > Thanks in advance, Aaron

>

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      18th Jan 2008
Show us the macro you use

--

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


"Aaron" <(E-Mail Removed)> wrote in message news:3F0B2020-9EF7-43FD-8A1B-(E-Mail Removed)...
> Worked great except it deleted the header row??
> How can I change it?
>
> "Ron de Bruin" wrote:
>
>> Hi Aaron
>>
>> Check out this page for example code
>> http://www.rondebruin.nl/delete.htm
>>
>> When you delete rows always start at the bottom and go up
>>
>> --
>>
>> Regards Ron de Bruin
>> http://www.rondebruin.nl/tips.htm
>>
>>
>> "Aaron" <(E-Mail Removed)> wrote in message news:0033E17E-CB5D-40F9-ACE4-(E-Mail Removed)...
>> >I have the following code that should delete any row that doesn't have the
>> > value of Defective in my range but it is deleting everything. Please help.
>> >
>> > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
>> >
>> > For Each c In ActiveSheet.Range("C2:C" & lastrow)
>> > If c.Value = "DEFECTIVE" Then
>> > Else
>> > Selection.EntireRow.delete
>> > End If
>> > Next
>> >
>> > Thanks in advance, Aaron

>>

 
Reply With Quote
 
Aaron
Guest
Posts: n/a
 
      18th Jan 2008
Sub Loop_Example()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With ActiveSheet

'We select the sheet so we can change the window view
.Select

'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView

'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False

'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, "C")

If Not IsError(.Value) Then

If .Value <> "HP DEFECTIVE" Then .EntireRow.delete
'This will delete each row with the Value "ron"
'in Column A, case sensitive.

End If

End With

Next Lrow

End With

ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With

End Sub

"Ron de Bruin" wrote:

> Show us the macro you use
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Aaron" <(E-Mail Removed)> wrote in message news:3F0B2020-9EF7-43FD-8A1B-(E-Mail Removed)...
> > Worked great except it deleted the header row??
> > How can I change it?
> >
> > "Ron de Bruin" wrote:
> >
> >> Hi Aaron
> >>
> >> Check out this page for example code
> >> http://www.rondebruin.nl/delete.htm
> >>
> >> When you delete rows always start at the bottom and go up
> >>
> >> --
> >>
> >> Regards Ron de Bruin
> >> http://www.rondebruin.nl/tips.htm
> >>
> >>
> >> "Aaron" <(E-Mail Removed)> wrote in message news:0033E17E-CB5D-40F9-ACE4-(E-Mail Removed)...
> >> >I have the following code that should delete any row that doesn't have the
> >> > value of Defective in my range but it is deleting everything. Please help.
> >> >
> >> > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
> >> >
> >> > For Each c In ActiveSheet.Range("C2:C" & lastrow)
> >> > If c.Value = "DEFECTIVE" Then
> >> > Else
> >> > Selection.EntireRow.delete
> >> > End If
> >> > Next
> >> >
> >> > Thanks in advance, Aaron
> >>

>

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      18th Jan 2008
Hi Aaron

Change

Firstrow = .UsedRange.Cells(1).Row

To

Firstrow = 2



--

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


"Aaron" <(E-Mail Removed)> wrote in message news:437CAC65-086C-495E-94CC-(E-Mail Removed)...
> Sub Loop_Example()
> Dim Firstrow As Long
> Dim Lastrow As Long
> Dim Lrow As Long
> Dim CalcMode As Long
> Dim ViewMode As Long
>
> With Application
> CalcMode = .Calculation
> .Calculation = xlCalculationManual
> .ScreenUpdating = False
> End With
>
> 'We use the ActiveSheet but you can replace this with
> 'Sheets("MySheet")if you want
> With ActiveSheet
>
> 'We select the sheet so we can change the window view
> .Select
>
> 'If you are in Page Break Preview Or Page Layout view go
> 'back to normal view, we do this for speed
> ViewMode = ActiveWindow.View
> ActiveWindow.View = xlNormalView
>
> 'Turn off Page Breaks, we do this for speed
> .DisplayPageBreaks = False
>
> '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, "C")
>
> If Not IsError(.Value) Then
>
> If .Value <> "HP DEFECTIVE" Then .EntireRow.delete
> 'This will delete each row with the Value "ron"
> 'in Column A, case sensitive.
>
> End If
>
> End With
>
> Next Lrow
>
> End With
>
> ActiveWindow.View = ViewMode
> With Application
> .ScreenUpdating = True
> .Calculation = CalcMode
> End With
>
> End Sub
>
> "Ron de Bruin" wrote:
>
>> Show us the macro you use
>>
>> --
>>
>> Regards Ron de Bruin
>> http://www.rondebruin.nl/tips.htm
>>
>>
>> "Aaron" <(E-Mail Removed)> wrote in message news:3F0B2020-9EF7-43FD-8A1B-(E-Mail Removed)...
>> > Worked great except it deleted the header row??
>> > How can I change it?
>> >
>> > "Ron de Bruin" wrote:
>> >
>> >> Hi Aaron
>> >>
>> >> Check out this page for example code
>> >> http://www.rondebruin.nl/delete.htm
>> >>
>> >> When you delete rows always start at the bottom and go up
>> >>
>> >> --
>> >>
>> >> Regards Ron de Bruin
>> >> http://www.rondebruin.nl/tips.htm
>> >>
>> >>
>> >> "Aaron" <(E-Mail Removed)> wrote in message news:0033E17E-CB5D-40F9-ACE4-(E-Mail Removed)...
>> >> >I have the following code that should delete any row that doesn't have the
>> >> > value of Defective in my range but it is deleting everything. Please help.
>> >> >
>> >> > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
>> >> >
>> >> > For Each c In ActiveSheet.Range("C2:C" & lastrow)
>> >> > If c.Value = "DEFECTIVE" Then
>> >> > Else
>> >> > Selection.EntireRow.delete
>> >> > End If
>> >> > Next
>> >> >
>> >> > Thanks in advance, Aaron
>> >>

>>

 
Reply With Quote
 
Aaron
Guest
Posts: n/a
 
      18th Jan 2008
Worked prefect! Thanks!

"Ron de Bruin" wrote:

> Hi Aaron
>
> Change
>
> Firstrow = .UsedRange.Cells(1).Row
>
> To
>
> Firstrow = 2
>
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Aaron" <(E-Mail Removed)> wrote in message news:437CAC65-086C-495E-94CC-(E-Mail Removed)...
> > Sub Loop_Example()
> > Dim Firstrow As Long
> > Dim Lastrow As Long
> > Dim Lrow As Long
> > Dim CalcMode As Long
> > Dim ViewMode As Long
> >
> > With Application
> > CalcMode = .Calculation
> > .Calculation = xlCalculationManual
> > .ScreenUpdating = False
> > End With
> >
> > 'We use the ActiveSheet but you can replace this with
> > 'Sheets("MySheet")if you want
> > With ActiveSheet
> >
> > 'We select the sheet so we can change the window view
> > .Select
> >
> > 'If you are in Page Break Preview Or Page Layout view go
> > 'back to normal view, we do this for speed
> > ViewMode = ActiveWindow.View
> > ActiveWindow.View = xlNormalView
> >
> > 'Turn off Page Breaks, we do this for speed
> > .DisplayPageBreaks = False
> >
> > '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, "C")
> >
> > If Not IsError(.Value) Then
> >
> > If .Value <> "HP DEFECTIVE" Then .EntireRow.delete
> > 'This will delete each row with the Value "ron"
> > 'in Column A, case sensitive.
> >
> > End If
> >
> > End With
> >
> > Next Lrow
> >
> > End With
> >
> > ActiveWindow.View = ViewMode
> > With Application
> > .ScreenUpdating = True
> > .Calculation = CalcMode
> > End With
> >
> > End Sub
> >
> > "Ron de Bruin" wrote:
> >
> >> Show us the macro you use
> >>
> >> --
> >>
> >> Regards Ron de Bruin
> >> http://www.rondebruin.nl/tips.htm
> >>
> >>
> >> "Aaron" <(E-Mail Removed)> wrote in message news:3F0B2020-9EF7-43FD-8A1B-(E-Mail Removed)...
> >> > Worked great except it deleted the header row??
> >> > How can I change it?
> >> >
> >> > "Ron de Bruin" wrote:
> >> >
> >> >> Hi Aaron
> >> >>
> >> >> Check out this page for example code
> >> >> http://www.rondebruin.nl/delete.htm
> >> >>
> >> >> When you delete rows always start at the bottom and go up
> >> >>
> >> >> --
> >> >>
> >> >> Regards Ron de Bruin
> >> >> http://www.rondebruin.nl/tips.htm
> >> >>
> >> >>
> >> >> "Aaron" <(E-Mail Removed)> wrote in message news:0033E17E-CB5D-40F9-ACE4-(E-Mail Removed)...
> >> >> >I have the following code that should delete any row that doesn't have the
> >> >> > value of Defective in my range but it is deleting everything. Please help.
> >> >> >
> >> >> > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
> >> >> >
> >> >> > For Each c In ActiveSheet.Range("C2:C" & lastrow)
> >> >> > If c.Value = "DEFECTIVE" Then
> >> >> > Else
> >> >> > Selection.EntireRow.delete
> >> >> > End If
> >> >> > Next
> >> >> >
> >> >> > Thanks in advance, Aaron
> >> >>
> >>

>

 
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
Delete data in a linked Excel sheet using Access code or seql delete Rocky Microsoft Access External Data 9 26th Jun 2005 12:42 AM
Re: Macro to delete sheets and saves remaining file does not properly delete module gazornenplat Microsoft Excel Programming 0 22nd Jun 2005 01:12 AM
Macro to delete sheets and saves remaining file does not properly delete module pherrero Microsoft Excel Programming 7 21st Jun 2005 05:16 PM
Delete every 3rd row, then delete rows 2-7, move info f/every 2nd row up one to the end and delete the row below Annette Microsoft Excel Programming 2 21st Sep 2004 02:40 PM
Re: When I highlight a sentence to delete, Word won't let me. I have to backspace. How can I deleted selected text with my delete key? Bill Foley Microsoft Word Document Management 1 4th Feb 2004 11:06 PM


Features
 

Advertising
 

Newsgroups
 


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