+ Reply to Thread
Results 1 to 9 of 9

Assistance with code Please

  1. #1
    Greg B...
    Guest

    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. #2
    Trevor Shuttleworth
    Guest

    Re: Assistance with code Please

    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..." <[email protected]> wrote in message
    news:[email protected]...
    >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. #3
    Greg B...
    Guest

    Re: Assistance with code Please

    Thank you Trevor


    Greg



  4. #4
    Dave Peterson
    Guest

    Re: Assistance with code Please

    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. #5
    Greg B
    Guest

    Re: Assistance with code Please

    Thanks for all your help

    Greg



  6. #6
    Myrna Larson
    Guest

    Re: Assistance with code Please

    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" <[email protected]> wrote:

    >Thanks for all your help
    >
    >Greg
    >



  7. #7
    Trevor Shuttleworth
    Guest

    Re: Assistance with code Please

    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" <[email protected]> wrote in message
    news:[email protected]...
    > 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. #8
    Dave Peterson
    Guest

    Re: Assistance with code Please

    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" <[email protected]> wrote in message
    > news:[email protected]...
    > > 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. #9
    Greg B
    Guest

    Re: Assistance with code Please

    Ok thanks never thought of it that way

    Will keep it in mind from now on

    Thanks for the advice

    Greg



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1