PC Review


Reply
Thread Tools Rate Thread

Delete variable # of rows depending on conditions

 
 
=?Utf-8?B?UG1hbg==?=
Guest
Posts: n/a
 
      26th Sep 2007
Hi,

I have a data sheet in which I have written a macro to add up columns upto
row #503. However every month I do not need 503 rows, sometimes its as small
as 20, or sometimes I need all the 503 rows. I was wondering if anyone could
help me figure out as to how to delete rows if more than three consecutive
rows are blank (this is because there is a blank row in between every unique
entity in a cell).
For eg: column "B" has data as follows:
1
1
1

2
2

3

4
4
4








TOTAL

If I use a macro to delete rows, it deletes the rows between the unique
values as well....hence I thought if a macro can be written that can delete
the blank rows till my total.

Thanks in Advance.....and Thanks to everyone who has helped me in the past

P
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2hhcmxlcyBDaGlja2VyaW5n?=
Guest
Posts: n/a
 
      27th Sep 2007
Pman, if you're trying to leave only one blank row in at a time try something
like this:
Dim cnt as Long
Dim MyCell as Range
For cnt = 503 to 2 Step -1
Set MyCell = Range("B" & cnt)
If MyCell = "" and MyCell.Offset(-1) = "" Then MyCell.EntireRow.Delete
Next

--
Charles Chickering

"A good example is twice the value of good advice."


"Pman" wrote:

> Hi,
>
> I have a data sheet in which I have written a macro to add up columns upto
> row #503. However every month I do not need 503 rows, sometimes its as small
> as 20, or sometimes I need all the 503 rows. I was wondering if anyone could
> help me figure out as to how to delete rows if more than three consecutive
> rows are blank (this is because there is a blank row in between every unique
> entity in a cell).
> For eg: column "B" has data as follows:
> 1
> 1
> 1
>
> 2
> 2
>
> 3
>
> 4
> 4
> 4
>
>
>
>
>
>
>
>
> TOTAL
>
> If I use a macro to delete rows, it deletes the rows between the unique
> values as well....hence I thought if a macro can be written that can delete
> the blank rows till my total.
>
> Thanks in Advance.....and Thanks to everyone who has helped me in the past
>
> P

 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      27th Sep 2007
Your problem is finding the last row of data. Use this statement

Lastrow = cells(rows.count,"A").end(xlup).Row

row.count is an excel constant which is 65536.
end(xlup) move from 65536 to the first non empty cell

You can then modify your loop

for i = 2 to LastRow

next i

"Charles Chickering" wrote:

> Pman, if you're trying to leave only one blank row in at a time try something
> like this:
> Dim cnt as Long
> Dim MyCell as Range
> For cnt = 503 to 2 Step -1
> Set MyCell = Range("B" & cnt)
> If MyCell = "" and MyCell.Offset(-1) = "" Then MyCell.EntireRow.Delete
> Next
>
> --
> Charles Chickering
>
> "A good example is twice the value of good advice."
>
>
> "Pman" wrote:
>
> > Hi,
> >
> > I have a data sheet in which I have written a macro to add up columns upto
> > row #503. However every month I do not need 503 rows, sometimes its as small
> > as 20, or sometimes I need all the 503 rows. I was wondering if anyone could
> > help me figure out as to how to delete rows if more than three consecutive
> > rows are blank (this is because there is a blank row in between every unique
> > entity in a cell).
> > For eg: column "B" has data as follows:
> > 1
> > 1
> > 1
> >
> > 2
> > 2
> >
> > 3
> >
> > 4
> > 4
> > 4
> >
> >
> >
> >
> >
> >
> >
> >
> > TOTAL
> >
> > If I use a macro to delete rows, it deletes the rows between the unique
> > values as well....hence I thought if a macro can be written that can delete
> > the blank rows till my total.
> >
> > Thanks in Advance.....and Thanks to everyone who has helped me in the past
> >
> > P

 
Reply With Quote
 
=?Utf-8?B?Q2hhcmxlcyBDaGlja2VyaW5n?=
Guest
Posts: n/a
 
      27th Sep 2007
I'm well aware of the .End(xlUp) function. The OP stated that he had 503 rows
to deal with, therefore no need for the extra processing time of finding the
last used row. Furthermore, .End(xlUp) only works if all rows are visible.
Taking this one step further, we're deleting rows so if we work from row 2 to
the end we will end up skipping rows causing the OP further problems. When
deleting rows you should always work from the bottom up.
--
Charles Chickering

"A good example is twice the value of good advice."


"Joel" wrote:

> Your problem is finding the last row of data. Use this statement
>
> Lastrow = cells(rows.count,"A").end(xlup).Row
>
> row.count is an excel constant which is 65536.
> end(xlup) move from 65536 to the first non empty cell
>
> You can then modify your loop
>
> for i = 2 to LastRow
>
> next i
>
> "Charles Chickering" wrote:
>
> > Pman, if you're trying to leave only one blank row in at a time try something
> > like this:
> > Dim cnt as Long
> > Dim MyCell as Range
> > For cnt = 503 to 2 Step -1
> > Set MyCell = Range("B" & cnt)
> > If MyCell = "" and MyCell.Offset(-1) = "" Then MyCell.EntireRow.Delete
> > Next
> >
> > --
> > Charles Chickering
> >
> > "A good example is twice the value of good advice."
> >
> >
> > "Pman" wrote:
> >
> > > Hi,
> > >
> > > I have a data sheet in which I have written a macro to add up columns upto
> > > row #503. However every month I do not need 503 rows, sometimes its as small
> > > as 20, or sometimes I need all the 503 rows. I was wondering if anyone could
> > > help me figure out as to how to delete rows if more than three consecutive
> > > rows are blank (this is because there is a blank row in between every unique
> > > entity in a cell).
> > > For eg: column "B" has data as follows:
> > > 1
> > > 1
> > > 1
> > >
> > > 2
> > > 2
> > >
> > > 3
> > >
> > > 4
> > > 4
> > > 4
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > TOTAL
> > >
> > > If I use a macro to delete rows, it deletes the rows between the unique
> > > values as well....hence I thought if a macro can be written that can delete
> > > the blank rows till my total.
> > >
> > > Thanks in Advance.....and Thanks to everyone who has helped me in the past
> > >
> > > P

 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      27th Sep 2007
You are fixing the wrong problem. Look a again at the data. He is putting
the row with TOTAL at row 504 even if there is on 20 rows of data. He simply
wants to put the row containing TOTAL at the end of the list. The blank rows
he is trying to eliminate is between the end of the data and the row with
TOTAL.

"Charles Chickering" wrote:

> I'm well aware of the .End(xlUp) function. The OP stated that he had 503 rows
> to deal with, therefore no need for the extra processing time of finding the
> last used row. Furthermore, .End(xlUp) only works if all rows are visible.
> Taking this one step further, we're deleting rows so if we work from row 2 to
> the end we will end up skipping rows causing the OP further problems. When
> deleting rows you should always work from the bottom up.
> --
> Charles Chickering
>
> "A good example is twice the value of good advice."
>
>
> "Joel" wrote:
>
> > Your problem is finding the last row of data. Use this statement
> >
> > Lastrow = cells(rows.count,"A").end(xlup).Row
> >
> > row.count is an excel constant which is 65536.
> > end(xlup) move from 65536 to the first non empty cell
> >
> > You can then modify your loop
> >
> > for i = 2 to LastRow
> >
> > next i
> >
> > "Charles Chickering" wrote:
> >
> > > Pman, if you're trying to leave only one blank row in at a time try something
> > > like this:
> > > Dim cnt as Long
> > > Dim MyCell as Range
> > > For cnt = 503 to 2 Step -1
> > > Set MyCell = Range("B" & cnt)
> > > If MyCell = "" and MyCell.Offset(-1) = "" Then MyCell.EntireRow.Delete
> > > Next
> > >
> > > --
> > > Charles Chickering
> > >
> > > "A good example is twice the value of good advice."
> > >
> > >
> > > "Pman" wrote:
> > >
> > > > Hi,
> > > >
> > > > I have a data sheet in which I have written a macro to add up columns upto
> > > > row #503. However every month I do not need 503 rows, sometimes its as small
> > > > as 20, or sometimes I need all the 503 rows. I was wondering if anyone could
> > > > help me figure out as to how to delete rows if more than three consecutive
> > > > rows are blank (this is because there is a blank row in between every unique
> > > > entity in a cell).
> > > > For eg: column "B" has data as follows:
> > > > 1
> > > > 1
> > > > 1
> > > >
> > > > 2
> > > > 2
> > > >
> > > > 3
> > > >
> > > > 4
> > > > 4
> > > > 4
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > TOTAL
> > > >
> > > > If I use a macro to delete rows, it deletes the rows between the unique
> > > > values as well....hence I thought if a macro can be written that can delete
> > > > the blank rows till my total.
> > > >
> > > > Thanks in Advance.....and Thanks to everyone who has helped me in the past
> > > >
> > > > P

 
Reply With Quote
 
=?Utf-8?B?UG1hbg==?=
Guest
Posts: n/a
 
      27th Sep 2007
Thank you Charles
It worked for me

"Charles Chickering" wrote:

> Pman, if you're trying to leave only one blank row in at a time try something
> like this:
> Dim cnt as Long
> Dim MyCell as Range
> For cnt = 503 to 2 Step -1
> Set MyCell = Range("B" & cnt)
> If MyCell = "" and MyCell.Offset(-1) = "" Then MyCell.EntireRow.Delete
> Next
>
> --
> Charles Chickering
>
> "A good example is twice the value of good advice."
>
>
> "Pman" wrote:
>
> > Hi,
> >
> > I have a data sheet in which I have written a macro to add up columns upto
> > row #503. However every month I do not need 503 rows, sometimes its as small
> > as 20, or sometimes I need all the 503 rows. I was wondering if anyone could
> > help me figure out as to how to delete rows if more than three consecutive
> > rows are blank (this is because there is a blank row in between every unique
> > entity in a cell).
> > For eg: column "B" has data as follows:
> > 1
> > 1
> > 1
> >
> > 2
> > 2
> >
> > 3
> >
> > 4
> > 4
> > 4
> >
> >
> >
> >
> >
> >
> >
> >
> > TOTAL
> >
> > If I use a macro to delete rows, it deletes the rows between the unique
> > values as well....hence I thought if a macro can be written that can delete
> > the blank rows till my total.
> >
> > Thanks in Advance.....and Thanks to everyone who has helped me in the past
> >
> > P

 
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 Rows for given conditions. fpd833 Microsoft Excel Programming 5 2nd Mar 2009 07:37 PM
delete the non blank rows depending on Column Harn88 Microsoft Excel Programming 1 18th Nov 2008 08:37 AM
Delete rows depending on cell Value =?Utf-8?B?VG9tYXMgU3Ryb2Vt?= Microsoft Excel Programming 3 8th Jun 2007 10:36 AM
How to delete a set of rows depending on Value =?Utf-8?B?bXNidXR0b24yNw==?= Microsoft Excel Misc 1 15th Jan 2006 04:57 PM
Insert rows in depending on criteria in variable columns =?Utf-8?B?amVmZmJlcnQ=?= Microsoft Excel Programming 2 4th Oct 2004 06:53 PM


Features
 

Advertising
 

Newsgroups
 


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