Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2006

Tip: Looking for answers? Try searching our database.

For each Next Loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vanna - 24 Mar 2006 18:09 GMT
Hi,
this is my first time trying out the ...for..each..next loop
I'm trying to delete the entire row if the cell in the E column contains the
word "USD".  I tried the codes below and get "run time error 424, Object
require" error.  Any help would be very much appreciated.

Sub deleteUSD()
Dim rng As Range

myrange = Range("E8:E500")

For Each rng In myrange
   If rng.Value = "USD" Then
   rng.EntireRow.Delete
   End If
Next rng

End Sub
Chip Pearson - 24 Mar 2006 18:13 GMT
Try the following:

Sub deleteUSD()
   Dim RowNdx As Long

   For RowNdx = 500 To 8 Step -1
       If Cells(RowNdx, "E").Value = "USD" Then
           Rows(RowNdx).Delete
       End If
   Next RowNdx

End Sub

Signature

Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com

> Hi,
> this is my first time trying out the ...for..each..next loop
[quoted text clipped - 16 lines]
>
> End Sub
Gary''s Student - 24 Mar 2006 18:21 GMT
Try:

Sub Macrox()
Dim i As Integer
For i = 500 To 8 Step -1
If Cells(i, 5).Value = "USD" Then
Rows(i).EntireRow.Delete
End If
Next
End Sub

The important thing is to go backwards
Signature

Gary''s Student

> Hi,
> this is my first time trying out the ...for..each..next loop
[quoted text clipped - 14 lines]
>
> End Sub
Jim Thomlinson - 24 Mar 2006 18:21 GMT
The issue you are running into has to do with the delete. The code is moving
through each cell in the range, but as the code deletes rows the range keeps
on changing. In this instance (if you want to use a for each) you need to be
a little bit sneeky. Create a second range object to accumulate the instances
of USD...

Sub deleteUSD()
Dim rng As Range
dim myRange as range
dim rngAll as range

set myrange = Range("E8:E500")

For Each rng In myrange
   If rng.Value = "USD" Then
    if rngall is nothing then
        set rngall = rng
    else
        set rngall = union(rng, rngall)
    end if
   End If
Next rng

if not (rngall is nothing) then rngall.entirerow.delete
End Sub

This can also be done using Find and Findnext which is a bit more efficient
but since you are only looking at a few hundred cells it is not a big
difference in this case...
Signature

HTH...

Jim Thomlinson

> Hi,
> this is my first time trying out the ...for..each..next loop
[quoted text clipped - 14 lines]
>
> End Sub
JMB - 27 Mar 2006 00:06 GMT
it is not a big difference in this case...

Especially since you are only deleting one range object, not several
hundred.  Correct?

> The issue you are running into has to do with the delete. The code is moving
> through each cell in the range, but as the code deletes rows the range keeps
[quoted text clipped - 44 lines]
> >
> > End Sub
Tom Ogilvy - 24 Mar 2006 20:45 GMT
by the way.  the reason you are getting the error is because

myrange = Range("E8:E500")

should be

Dim myRange as Range

set myRange = range("E8:E500")

You haven't yet gotten to the problems described by the others, so you need
to change your approach as well.  This was just for information - not a
suggestion that that change will give the final result you desire.

Signature

Regards,
Tom Ogilvy

> Hi,
> this is my first time trying out the ...for..each..next loop
[quoted text clipped - 14 lines]
>
> End Sub
Vanna - 27 Mar 2006 17:41 GMT
Thanks everyone for your responses.  There are certainly many ways to do the
same thing.  I've learn the short cut this time.

> by the way.  the reason you are getting the error is because
>
[quoted text clipped - 28 lines]
> >
> > End Sub
Tom Ogilvy - 24 Mar 2006 20:45 GMT
Here is one more
Sub DeleteUSD()
 Dim rng as Range
 set rng = columns(5).Find("USD")
 if not rng is nothing then
    do
        rng.EntireRow.Delete
        set rng = columns(5).Find("USD")
   loop while not rng is nothing
 End if
end Sub

Signature

Regards,
Tom Ogilvy

> Hi,
> this is my first time trying out the ...for..each..next loop
[quoted text clipped - 14 lines]
>
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.