Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Greg B...
 
Posts: n/a
Default Assistance with code Please

I have copied the code below to see if someone can help me with my problem.
I work great unfortunately it needs to check from cells d2 to d101. I have
to hit run about 5 times for the whole range to be cleared. I sthere anyway
to loop it until it is done or have it complete in one shot.

Can someone tell me what I am doing wrong

Sub CopyData()
Dim rng As Range, cell As Range, col As Long
Dim rw As Long, rng2 As Range
col = 4
rw = 1

With Worksheets("SCHEDULE")
Set rng = .Range(.Cells(col), .Cells(Rows.Count, col).End(xlUp))
End With
For Each cell In rng
If LCase(cell.Value) = "yes" Then
cell.EntireRow.Delete

End If
Next

End Sub
Thanks in advance

Greg



  #2   Report Post  
Trevor Shuttleworth
 
Posts: n/a
Default

Greg

try it this way:

Sub CopyData()
Dim rng As Range, cell As Range, RngToDelete As Range, col As Long
Dim rw As Long, rng2 As Range
col = 4
rw = 1

With Worksheets("SCHEDULE")
Set rng = .Range(.Cells(col), .Cells(Rows.Count, col).End(xlUp))
End With
For Each cell In rng
If LCase(cell.Value) = "yes" Then
If RngToDelete Is Nothing Then
Set RngToDelete = cell
Else
Set RngToDelete = Union(RngToDelete, cell)
End If
End If
Next
If Not RngToDelete Is Nothing Then
RngToDelete.EntireRow.Delete
End If
End Sub

Regards

Trevor


"Greg B..." wrote in message
...
I have copied the code below to see if someone can help me with my problem.
I work great unfortunately it needs to check from cells d2 to d101. I
have
to hit run about 5 times for the whole range to be cleared. I sthere
anyway
to loop it until it is done or have it complete in one shot.

Can someone tell me what I am doing wrong

Sub CopyData()
Dim rng As Range, cell As Range, col As Long
Dim rw As Long, rng2 As Range
col = 4
rw = 1

With Worksheets("SCHEDULE")
Set rng = .Range(.Cells(col), .Cells(Rows.Count, col).End(xlUp))
End With
For Each cell In rng
If LCase(cell.Value) = "yes" Then
cell.EntireRow.Delete

End If
Next

End Sub
Thanks in advance

Greg





  #3   Report Post  
Greg B...
 
Posts: n/a
Default

Thank you Trevor


Greg


  #4   Report Post  
Dave Peterson
 
Posts: n/a
Default

This link looks funny to me:

Set rng = .Range(.Cells(col), .Cells(Rows.Count, col).End(xlUp))

Did you mean:

Set rng = .Range(.Cells(rw, col), .Cells(Rows.Count, col).End(xlUp))

(added "rw" in the first .cells() portion.)

"Greg B..." wrote:

Thank you Trevor

Greg


--

Dave Peterson
  #5   Report Post  
Greg B
 
Posts: n/a
Default

Thanks for all your help

Greg




  #6   Report Post  
Myrna Larson
 
Posts: n/a
Default

Just so you understand why your original code failed, when using For Each, the
rows are processed in ascending order. Let's say you delete row 2. The code
then goes on to row 3. Problem is, the original row 3 has moved up to row 2
because of the deletion, so you really want to check row 2 again.

The solution for this is to process the rows from the bottom up. That means
you can't use For Each loop, but a "standard" For/Next loop with the row
number:

RLast = Cells(65536,1).End(xlUp).Row
For R = RLast To 1 Step - 1
If Cells(R, 1).Value = "X" Then Rows(R).EntireRow.Delete
Next R


On Thu, 3 Mar 2005 01:17:03 +1030, "Greg B" wrote:

Thanks for all your help

Greg


  #7   Report Post  
Trevor Shuttleworth
 
Posts: n/a
Default

Dave

I didn't notice that and you are right. However, in my testing it works
fine ... and I'm not sure why.

?rng.Address
$D$1:$D$17

Any thoughts ?

Regards

Trevor

"Dave Peterson" wrote in message
...
This link looks funny to me:

Set rng = .Range(.Cells(col), .Cells(Rows.Count, col).End(xlUp))

Did you mean:

Set rng = .Range(.Cells(rw, col), .Cells(Rows.Count, col).End(xlUp))

(added "rw" in the first .cells() portion.)

"Greg B..." wrote:

Thank you Trevor

Greg


--

Dave Peterson



  #8   Report Post  
Dave Peterson
 
Posts: n/a
Default

Try it with a workbook with two worksheets.

And make sure sheets(1) isn't the activesheet.

Those unqualified range objects usually refer to the activesheet. If the
activesheet is not the correct sheet, then you get an error.



Trevor Shuttleworth wrote:

Dave

I didn't notice that and you are right. However, in my testing it works
fine ... and I'm not sure why.

?rng.Address
$D$1:$D$17

Any thoughts ?

Regards

Trevor

"Dave Peterson" wrote in message
...
This link looks funny to me:

Set rng = .Range(.Cells(col), .Cells(Rows.Count, col).End(xlUp))

Did you mean:

Set rng = .Range(.Cells(rw, col), .Cells(Rows.Count, col).End(xlUp))

(added "rw" in the first .cells() portion.)

"Greg B..." wrote:

Thank you Trevor

Greg


--

Dave Peterson


--

Dave Peterson
  #9   Report Post  
Greg B
 
Posts: n/a
Default

Ok thanks never thought of it that way

Will keep it in mind from now on

Thanks for the advice

Greg


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
Error trapped only while stepping through the code - Not triggered when run Jeff Excel Discussion (Misc queries) 7 March 7th 05 06:29 PM
Error trapped only while stepping through the code - Not triggered when run Jeff Excel Discussion (Misc queries) 0 February 28th 05 06:26 PM
Formula assistance required!!! Don Excel Discussion (Misc queries) 4 February 22nd 05 08:34 PM
Help with Amending this Code Please [email protected] Excel Worksheet Functions 4 February 1st 05 07:04 PM
Command Button VBA code Dave Peterson Excel Discussion (Misc queries) 2 January 25th 05 11:28 PM


All times are GMT +1. The time now is 08:51 AM.

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

About Us

"It's about Microsoft Excel"