+ Reply to Thread
Results 1 to 5 of 5

delete row based on value of cell in row

  1. #1
    AD108
    Guest

    delete row based on value of cell in row

    I am attempting to loop through each row of the used range of a sheet, and
    then delete it based on its contents. My code is failing. Any help would
    be appreciated. Thanks in advance.

    For i = 8 To ActiveSheet.UsedRange.Rows.Count
    Set x = Cells(i, 6).value
    If Not IsNumeric(x) Then
    ActiveSheet.Rows(i).Delete
    End If
    Next i



  2. #2
    Jim Cone
    Guest

    Re: delete row based on value of cell in row

    You create a range object by setting the variable equal to the range,
    not to the range value... Set x = Cells(i, 6)

    It is good practice to use the default property of objects instead of
    letting Excel assume it... If not IsNumeric(x.Value) Then

    Delete rows from the bottom up...
    For i =ActiveSheet.UsedRange.Rows.Count to 8 Step -1
    --
    Jim Cone
    San Francisco, USA
    http://www.realezsites.com/bus/primitivesoftware


    "AD108" <[email protected]> wrote in message news:[email protected]...
    I am attempting to loop through each row of the used range of a sheet, and
    then delete it based on its contents. My code is failing. Any help would
    be appreciated. Thanks in advance.

    For i = 8 To ActiveSheet.UsedRange.Rows.Count
    Set x = Cells(i, 6).value
    If Not IsNumeric(x) Then
    ActiveSheet.Rows(i).Delete
    End If
    Next i



  3. #3
    Die_Another_Day
    Guest

    Re: delete row based on value of cell in row

    When deleting rows you really should work in reverse, as follows.
    Dim lRow as Long
    lRow = Cells(Rows.Count,6).End(xlUp).Row
    For i = lRow to 8 Step - 1
    If Not IsNumeric(Cells(i,6).Value) Then Rows(i).Delete
    Next

    HTH
    Die_Another_Day

    AD108 wrote:
    > I am attempting to loop through each row of the used range of a sheet, and
    > then delete it based on its contents. My code is failing. Any help would
    > be appreciated. Thanks in advance.
    >
    > For i = 8 To ActiveSheet.UsedRange.Rows.Count
    > Set x = Cells(i, 6).value
    > If Not IsNumeric(x) Then
    > ActiveSheet.Rows(i).Delete
    > End If
    > Next i



  4. #4
    Forum Contributor
    Join Date
    12-12-2005
    Posts
    667

    delete row based on value of cell in row

    Your code is OK except for "Set". The line should be:
    x = Cells(i, 6).value
    "Set" assigns an object reference to a variable or property. In other words it is used to give a name to an object.
    Best regards,

    Ray

  5. #5
    AD108
    Guest

    Re: delete row based on value of cell in row

    Thanks very much. Right, if I start deleting them from the top, the counter
    won't work.


    "Jim Cone" <[email protected]> wrote in message
    news:[email protected]...
    > You create a range object by setting the variable equal to the range,
    > not to the range value... Set x = Cells(i, 6)
    >
    > It is good practice to use the default property of objects instead of
    > letting Excel assume it... If not IsNumeric(x.Value) Then
    >
    > Delete rows from the bottom up...
    > For i =ActiveSheet.UsedRange.Rows.Count to 8 Step -1
    > --
    > Jim Cone
    > San Francisco, USA
    > http://www.realezsites.com/bus/primitivesoftware
    >
    >
    > "AD108" <[email protected]> wrote in message

    news:[email protected]...
    > I am attempting to loop through each row of the used range of a sheet, and
    > then delete it based on its contents. My code is failing. Any help would
    > be appreciated. Thanks in advance.
    >
    > For i = 8 To ActiveSheet.UsedRange.Rows.Count
    > Set x = Cells(i, 6).value
    > If Not IsNumeric(x) Then
    > ActiveSheet.Rows(i).Delete
    > End If
    > Next i
    >
    >




+ 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