PC Review


Reply
Thread Tools Rate Thread

Deleting rows based on data NOT meeting criteria --working macro here, just need help with tweaking

 
 
Zarlot531
Guest
Posts: n/a
 
      28th Apr 2007
Here is the model of the code I'm using -- this come from
http://www.rondebruin.nl/delete.htm

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

ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView

Firstrow = ActiveSheet.UsedRange.Cells(1).Row
Lastrow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1

With ActiveSheet
.DisplayPageBreaks = False
For Lrow = Lastrow To Firstrow Step -1

If IsError(.Cells(Lrow, "A").Value) Then
'Do nothing, This avoid a error if there is a error in
the cell

ElseIf .Cells(Lrow, "A").Value = "ron"
Then .Rows(Lrow).Delete
'This will delete each row with the Value "ron" in
Column A, case sensitive.

End If
Next
End With

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

End Sub

____________________________________________________

Here is my situation and my questions:

In Column A on my spreadsheet, there is data like this:


AR3303
AR4055
Subtotal
Blank Cell (and row)
Blank Cell (and row)
AR9999
DP3838
DP3923
DP3932


What I want to do is delete all rows that do NOT contain the DP in the
first cell (Column A cell). I think I could do this somehow using a
LEFT function and obviously an IF NOT function somehow, but I'm having
problems tweaking it to do it this way.

Can anybody help?

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      28th Apr 2007
Sub DelRw()
lstRw = Cells(Rows.Count, 1).End(xlUp).Row
For i = lstRw To 2 Step -1 'Assumes Header Row
x = Cells(i, 1).Value
If Left(x, 2) <> "DP" Then
Cells(i, 1).EntireRow.Delete
End If
Next

"Zarlot531" wrote:

> Here is the model of the code I'm using -- this come from
> http://www.rondebruin.nl/delete.htm
>
> 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
>
> ViewMode = ActiveWindow.View
> ActiveWindow.View = xlNormalView
>
> Firstrow = ActiveSheet.UsedRange.Cells(1).Row
> Lastrow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1
>
> With ActiveSheet
> .DisplayPageBreaks = False
> For Lrow = Lastrow To Firstrow Step -1
>
> If IsError(.Cells(Lrow, "A").Value) Then
> 'Do nothing, This avoid a error if there is a error in
> the cell
>
> ElseIf .Cells(Lrow, "A").Value = "ron"
> Then .Rows(Lrow).Delete
> 'This will delete each row with the Value "ron" in
> Column A, case sensitive.
>
> End If
> Next
> End With
>
> ActiveWindow.View = ViewMode
> With Application
> .ScreenUpdating = True
> .Calculation = CalcMode
> End With
>
> End Sub
>
> ____________________________________________________
>
> Here is my situation and my questions:
>
> In Column A on my spreadsheet, there is data like this:
>
>
> AR3303
> AR4055
> Subtotal
> Blank Cell (and row)
> Blank Cell (and row)
> AR9999
> DP3838
> DP3923
> DP3932
>
>
> What I want to do is delete all rows that do NOT contain the DP in the
> first cell (Column A cell). I think I could do this somehow using a
> LEFT function and obviously an IF NOT function somehow, but I'm having
> problems tweaking it to do it this way.
>
> Can anybody help?
>
>

 
Reply With Quote
 
Zarlot531
Guest
Posts: n/a
 
      28th Apr 2007
Wow you're a genius ... thanks a lot!


On Apr 28, 3:16 pm, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> Sub DelRw()
> lstRw = Cells(Rows.Count, 1).End(xlUp).Row
> For i = lstRw To 2 Step -1 'Assumes Header Row
> x = Cells(i, 1).Value
> If Left(x, 2) <> "DP" Then
> Cells(i, 1).EntireRow.Delete
> End If
> Next
>
>
>
> "Zarlot531" wrote:
> > Here is the model of the code I'm using -- this come from
> >http://www.rondebruin.nl/delete.htm

>
> > 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

>
> > ViewMode = ActiveWindow.View
> > ActiveWindow.View = xlNormalView

>
> > Firstrow = ActiveSheet.UsedRange.Cells(1).Row
> > Lastrow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1

>
> > With ActiveSheet
> > .DisplayPageBreaks = False
> > For Lrow = Lastrow To Firstrow Step -1

>
> > If IsError(.Cells(Lrow, "A").Value) Then
> > 'Do nothing, This avoid a error if there is a error in
> > the cell

>
> > ElseIf .Cells(Lrow, "A").Value = "ron"
> > Then .Rows(Lrow).Delete
> > 'This will delete each row with the Value "ron" in
> > Column A, case sensitive.

>
> > End If
> > Next
> > End With

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

>
> > End Sub

>
> > ____________________________________________________

>
> > Here is my situation and my questions:

>
> > In Column A on my spreadsheet, there is data like this:

>
> > AR3303
> > AR4055
> > Subtotal
> > Blank Cell (and row)
> > Blank Cell (and row)
> > AR9999
> > DP3838
> > DP3923
> > DP3932

>
> > What I want to do is delete all rows that do NOT contain the DP in the
> > first cell (Column A cell). I think I could do this somehow using a
> > LEFT function and obviously an IF NOT function somehow, but I'm having
> > problems tweaking it to do it this way.

>
> > Can anybody help?- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
Zarlot531
Guest
Posts: n/a
 
      28th Apr 2007
Here is my preliminary code. I've changed it around a little from
what you've written, but could you explain what the "To 1 Step -1"
really means? I changed it from 2 to 1 because I have no header, and
I asusme the -1 is referring to the footer.

But one problem I'm having is that I run the code and it works
perfectly except for the fact that it stops reading too soon. For
example, there will be a few more lines left (rows who don't have data
starting for five or six columns over) at the very end. If I move the
lines at the end over to the very first column, then it reads it and
is fine, or if there happens to be a row that is over far left enough
at the end, then everything is fine. Which part of this code controls
how far over the program reads to tell when the rows have stopped?

Thanks...

Sub DelRw()
Dim lstRw
Dim i
Dim x
Dim CalcMode
With Application
CalcMode = .Calculation
..Calculation = xlCalculationManual
..ScreenUpdating = False
End With
lstRw = Cells(Rows.Count, 1).End(xlUp).Row
For i = lstRw To 1 Step -1
x = Cells(i, 3).Value
If Left(x, 4) <> "2745" Then
Cells(i, 3).EntireRow.Delete
End If

Next
With Application
..ScreenUpdating = True
..Calculation = CalcMode
End With

End Sub















On Apr 28, 3:16 pm, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> Sub DelRw()
> lstRw = Cells(Rows.Count, 1).End(xlUp).Row
> For i = lstRw To 2 Step -1 'Assumes Header Row
> x = Cells(i, 1).Value
> If Left(x, 2) <> "DP" Then
> Cells(i, 1).EntireRow.Delete
> End If
> Next
>
>
>
> "Zarlot531" wrote:
> > Here is the model of the code I'm using -- this come from
> >http://www.rondebruin.nl/delete.htm

>
> > 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

>
> > ViewMode = ActiveWindow.View
> > ActiveWindow.View = xlNormalView

>
> > Firstrow = ActiveSheet.UsedRange.Cells(1).Row
> > Lastrow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1

>
> > With ActiveSheet
> > .DisplayPageBreaks = False
> > For Lrow = Lastrow To Firstrow Step -1

>
> > If IsError(.Cells(Lrow, "A").Value) Then
> > 'Do nothing, This avoid a error if there is a error in
> > the cell

>
> > ElseIf .Cells(Lrow, "A").Value = "ron"
> > Then .Rows(Lrow).Delete
> > 'This will delete each row with the Value "ron" in
> > Column A, case sensitive.

>
> > End If
> > Next
> > End With

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

>
> > End Sub

>
> > ____________________________________________________

>
> > Here is my situation and my questions:

>
> > In Column A on my spreadsheet, there is data like this:

>
> > AR3303
> > AR4055
> > Subtotal
> > Blank Cell (and row)
> > Blank Cell (and row)
> > AR9999
> > DP3838
> > DP3923
> > DP3932

>
> > What I want to do is delete all rows that do NOT contain the DP in the
> > first cell (Column A cell). I think I could do this somehow using a
> > LEFT function and obviously an IF NOT function somehow, but I'm having
> > problems tweaking it to do it this way.

>
> > Can anybody help?- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      29th Apr 2007
The step -1 is to walk from the bottom of the column to the top.
Since you are deleting rows, and the default is to shift up, it is
better to start at the bottom and work toward the top. Otherwise
it would skip rows as it worked its way down.

The last row (lstRw) is calculated in the code you are using based
on column "A". You can do one of two things:

1- Change lstRw = Cells(Rows.Count, ?).End(xlUp).Row to replace
the question mark with whichever column you have the most data in or

2- Chage to lstRw = Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell).Row

Either of the two methods should then cover all of the rows with data in them.

Since you are only searching a single column, this particular code does not
take the length of the row into consideration except for the delete command
where the entire row is deleted.

"Zarlot531" wrote:

> Here is my preliminary code. I've changed it around a little from
> what you've written, but could you explain what the "To 1 Step -1"
> really means? I changed it from 2 to 1 because I have no header, and
> I asusme the -1 is referring to the footer.
>
> But one problem I'm having is that I run the code and it works
> perfectly except for the fact that it stops reading too soon. For
> example, there will be a few more lines left (rows who don't have data
> starting for five or six columns over) at the very end. If I move the
> lines at the end over to the very first column, then it reads it and
> is fine, or if there happens to be a row that is over far left enough
> at the end, then everything is fine. Which part of this code controls
> how far over the program reads to tell when the rows have stopped?
>
> Thanks...
>
> Sub DelRw()
> Dim lstRw
> Dim i
> Dim x
> Dim CalcMode
> With Application
> CalcMode = .Calculation
> ..Calculation = xlCalculationManual
> ..ScreenUpdating = False
> End With
> lstRw = Cells(Rows.Count, 1).End(xlUp).Row
> For i = lstRw To 1 Step -1
> x = Cells(i, 3).Value
> If Left(x, 4) <> "2745" Then
> Cells(i, 3).EntireRow.Delete
> End If
>
> Next
> With Application
> ..ScreenUpdating = True
> ..Calculation = CalcMode
> End With
>
> End Sub
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> On Apr 28, 3:16 pm, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> > Sub DelRw()
> > lstRw = Cells(Rows.Count, 1).End(xlUp).Row
> > For i = lstRw To 2 Step -1 'Assumes Header Row
> > x = Cells(i, 1).Value
> > If Left(x, 2) <> "DP" Then
> > Cells(i, 1).EntireRow.Delete
> > End If
> > Next
> >
> >
> >
> > "Zarlot531" wrote:
> > > Here is the model of the code I'm using -- this come from
> > >http://www.rondebruin.nl/delete.htm

> >
> > > 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

> >
> > > ViewMode = ActiveWindow.View
> > > ActiveWindow.View = xlNormalView

> >
> > > Firstrow = ActiveSheet.UsedRange.Cells(1).Row
> > > Lastrow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1

> >
> > > With ActiveSheet
> > > .DisplayPageBreaks = False
> > > For Lrow = Lastrow To Firstrow Step -1

> >
> > > If IsError(.Cells(Lrow, "A").Value) Then
> > > 'Do nothing, This avoid a error if there is a error in
> > > the cell

> >
> > > ElseIf .Cells(Lrow, "A").Value = "ron"
> > > Then .Rows(Lrow).Delete
> > > 'This will delete each row with the Value "ron" in
> > > Column A, case sensitive.

> >
> > > End If
> > > Next
> > > End With

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

> >
> > > End Sub

> >
> > > ____________________________________________________

> >
> > > Here is my situation and my questions:

> >
> > > In Column A on my spreadsheet, there is data like this:

> >
> > > AR3303
> > > AR4055
> > > Subtotal
> > > Blank Cell (and row)
> > > Blank Cell (and row)
> > > AR9999
> > > DP3838
> > > DP3923
> > > DP3932

> >
> > > What I want to do is delete all rows that do NOT contain the DP in the
> > > first cell (Column A cell). I think I could do this somehow using a
> > > LEFT function and obviously an IF NOT function somehow, but I'm having
> > > problems tweaking it to do it this way.

> >
> > > Can anybody help?- Hide quoted text -

> >
> > - Show quoted text -

>
>
>

 
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
Deleting rows meeting certain criteria in a particular column tsraj Microsoft Excel Misc 1 1st Apr 2010 07:58 PM
deleting rows based on criteria gbpg Microsoft Excel Programming 7 16th Aug 2008 05:23 AM
Deleting Rows Based on Criteria bgoode Microsoft Access Macros 1 24th Apr 2008 04:33 PM
Deleting rows meeting criteria =?Utf-8?B?Vmlja2llIEJlbnRvbg==?= Microsoft Excel Misc 3 16th Feb 2004 03:21 PM
Deleting rows based on criteria John Walker Microsoft Excel Programming 2 12th Dec 2003 08:37 PM


Features
 

Advertising
 

Newsgroups
 


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