Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Please take a look at this working code and critque it.

This code is working, but I have the feeling its not the best code.

Basically, I have a sheet in which if column D is equal to zero, I want
to delete the row and shift the rows up. I want to do this for every
row, starting at row three to bypass the headings.

Please let me know your thoughts.

With objWkb.Worksheets("Sorted Rankings")
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

For Row = 3 To TotalRows Step 1
If Row TotalRows Then GoTo EndNow:

If .Cells(Row, "D").Value = 0 Then
.Rows(Row).EntireRow.Delete ' Delete Shift:=xlUp
Row = Row - 1 ' Because we deleted a row and shifted it up, we have
to make sure variable Row does not get increased otherwise the next row
down from a deleted row will not be evaluated!
End If
'Check TotalRows in case a row was deleted
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

Next Row
EndNow:

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 692
Default Please take a look at this working code and critque it.

When deleting rows - you need to work from the bottom up...

For Row = TotalRows to 3 Step -1

--
steveB

Remove "AYN" from email to respond
"BerkshireGuy" wrote in message
oups.com...
This code is working, but I have the feeling its not the best code.

Basically, I have a sheet in which if column D is equal to zero, I want
to delete the row and shift the rows up. I want to do this for every
row, starting at row three to bypass the headings.

Please let me know your thoughts.

With objWkb.Worksheets("Sorted Rankings")
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

For Row = 3 To TotalRows Step 1
If Row TotalRows Then GoTo EndNow:

If .Cells(Row, "D").Value = 0 Then
.Rows(Row).EntireRow.Delete ' Delete Shift:=xlUp
Row = Row - 1 ' Because we deleted a row and shifted it up, we have
to make sure variable Row does not get increased otherwise the next row
down from a deleted row will not be evaluated!
End If
'Check TotalRows in case a row was deleted
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

Next Row
EndNow:



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Please take a look at this working code and critque it.

Couple of comments:

1) It's nearly always a bad idea to use reserved words as variable names
(e.g., "Row"). It can get confusing, especially months down the road.

2) You can avoid all the messy row handling if you go backwards through
the rows:

For nRow = nTotalRows To 3 Step -1
If Cells(nRow, "D").Value = 0 Then _
Rows(nRow).EntireRow.Delete
Next nRow

3) You can gain a lot of efficiency if there are a large number of rows
to delete by identifying them all and deleting them all at once (e.g.,
XL doesn't have to reindex each time). Here's one way I might rewrite
your code:

Dim rCell As Range
Dim rDel As Range
With objWkb.Worksheets("Sorted Rankings")
For Each rCell In .Range("D3:D" & _
.Range("D" & .Rows.Count).End(xlUp).Row)
With rCell
If .Value = 0 Then
If rDel Is Nothing Then
Set rDel = .Cells
Else
Set rDel = Union(rDel, .Cells)
End If
End If
End With
Next rCell
If Not rDel Is Nothing Then rDel.EntireRow.Delete
End With



In article .com,
"BerkshireGuy" wrote:

This code is working, but I have the feeling its not the best code.

Basically, I have a sheet in which if column D is equal to zero, I want
to delete the row and shift the rows up. I want to do this for every
row, starting at row three to bypass the headings.

Please let me know your thoughts.

With objWkb.Worksheets("Sorted Rankings")
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

For Row = 3 To TotalRows Step 1
If Row TotalRows Then GoTo EndNow:

If .Cells(Row, "D").Value = 0 Then
.Rows(Row).EntireRow.Delete ' Delete Shift:=xlUp
Row = Row - 1 ' Because we deleted a row and shifted it up, we have
to make sure variable Row does not get increased otherwise the next row
down from a deleted row will not be evaluated!
End If
'Check TotalRows in case a row was deleted
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

Next Row
EndNow:

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Please take a look at this working code and critque it.



nitpicking... <g

using a union can get slow if the union has many areas.. and
will get impossibly slow around 800 areas and more.

better to "flush" the range if the area count gets 'too high'.

Sub foo()

Dim rCell As Range
Dim rDel As Range
With objWkb.Worksheets("Sorted Rankings")
For Each rCell In .Range("D3:D" & _
.Range("D" & .Rows.Count).End(xlUp).Row)
With rCell
If .Value = 0 Then
If rDel Is Nothing Then
Set rDel = .Cells
Else
Set rDel = Union(rDel, .Cells)
If rDel.Areas.Count 200 Then Call Flush(rDel)
End If
End If
End With
Next rCell
End With
Call Flush(rDel)
End Sub

Private Sub Flush(ByRef rDel As Range)
If Not rDel Is Nothing Then
Application.ScreenUpdating = False
rDel.EntireRow.Delete
Application.ScreenUpdating = True
Set rDel = Nothing
End If
End Sub





--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


JE McGimpsey wrote :

Couple of comments:

1) It's nearly always a bad idea to use reserved words as variable
names (e.g., "Row"). It can get confusing, especially months down the
road.

2) You can avoid all the messy row handling if you go backwards
through the rows:

For nRow = nTotalRows To 3 Step -1
If Cells(nRow, "D").Value = 0 Then _
Rows(nRow).EntireRow.Delete
Next nRow

3) You can gain a lot of efficiency if there are a large number of
rows to delete by identifying them all and deleting them all at once
(e.g., XL doesn't have to reindex each time). Here's one way I might
rewrite your code:

Dim rCell As Range
Dim rDel As Range
With objWkb.Worksheets("Sorted Rankings")
For Each rCell In .Range("D3:D" & _
.Range("D" & .Rows.Count).End(xlUp).Row)
With rCell
If .Value = 0 Then
If rDel Is Nothing Then
Set rDel = .Cells
Else
Set rDel = Union(rDel, .Cells)
End If
End If
End With
Next rCell
If Not rDel Is Nothing Then rDel.EntireRow.Delete
End With



In article .com,
"BerkshireGuy" wrote:

This code is working, but I have the feeling its not the best code.

Basically, I have a sheet in which if column D is equal to zero, I
want to delete the row and shift the rows up. I want to do this
for every row, starting at row three to bypass the headings.

Please let me know your thoughts.

With objWkb.Worksheets("Sorted Rankings")
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

For Row = 3 To TotalRows Step 1
If Row TotalRows Then GoTo EndNow:

If .Cells(Row, "D").Value = 0 Then
.Rows(Row).EntireRow.Delete ' Delete Shift:=xlUp
Row = Row - 1 ' Because we deleted a row and shifted it up, we
have to make sure variable Row does not get increased otherwise the
next row down from a deleted row will not be evaluated!
End If
'Check TotalRows in case a row was deleted
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

Next Row
EndNow:

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Please take a look at this working code and critque it.

Excellent! Thanks everyone.
-Brian



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Please take a look at this working code and critque it.

KeepitCool -

Your code is generating a Method or data member not found error.

Thanks
Brian

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Please take a look at this working code and critque it.

And much more mundane...

If the cell is empty, then its value will be 0, too.

You may want to watch out for that, too.



BerkshireGuy wrote:

This code is working, but I have the feeling its not the best code.

Basically, I have a sheet in which if column D is equal to zero, I want
to delete the row and shift the rows up. I want to do this for every
row, starting at row three to bypass the headings.

Please let me know your thoughts.

With objWkb.Worksheets("Sorted Rankings")
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

For Row = 3 To TotalRows Step 1
If Row TotalRows Then GoTo EndNow:

If .Cells(Row, "D").Value = 0 Then
.Rows(Row).EntireRow.Delete ' Delete Shift:=xlUp
Row = Row - 1 ' Because we deleted a row and shifted it up, we have
to make sure variable Row does not get increased otherwise the next row
down from a deleted row will not be evaluated!
End If
'Check TotalRows in case a row was deleted
TotalRows = objWkb.Worksheets("Sorted Rankings").Range("D" &
objWkb.Worksheets("Sorted Rankings").Rows.Count).End(xlUp).Row

Next Row
EndNow:


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Please take a look at this working code and critque it.


possibly. it was untested and adapted from
JE McGimpsey, just to illustrate the flush
'technique'.

the objwkb is missing..
thats all, rest works for me.

include dave peterson's suggestion

With rCell
If Len(.Value) 0 And .Value = 0 Then

and WHERE does it generate that error?


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


BerkshireGuy wrote :

KeepitCool -

Your code is generating a Method or data member not found error.

Thanks
Brian

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Code Is Not Working Rob Excel Discussion (Misc queries) 2 May 30th 07 05:23 PM
Code not working and can't see why Steve Excel Discussion (Misc queries) 3 December 31st 04 03:12 PM
Wht is this Code not Working ? John Excel Programming 7 December 7th 04 02:09 AM
Code not working Todd Huttenstine Excel Programming 1 June 10th 04 05:06 PM
Code not working Bob Phillips[_5_] Excel Programming 5 August 14th 03 03:12 PM


All times are GMT +1. The time now is 03:09 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"