Consulting

Results 1 to 17 of 17

Thread: Solved: deleting rows beneath active cell one by one till value is found in column

  1. #1

    Solved: deleting rows beneath active cell one by one till value is found in column

    Hi There

    Please see the following code. I want the macro to delete 5 rows if the user clicks on a sub task (this part works). The tricky part is if a user clicks on a main task, the macro should delete rows one by one beneath it till it finds a main task (where the value in Column B=1), and then STOP. Currently, it finds ALL rows in the spreadsheet where Column B=1 and deletes them!

    Thanks!
    S

    [VBA]Sub DeleteTask01()
    '
    '
    Dim SelectedRow As Long

    SelectedRow = ActiveCell.row

    If Cells(SelectedRow, 2).Value <> 1 Then

    Range("B" & SelectedRow & ":B" & SelectedRow + 5).EntireRow.Delete
    Else

    Dim i As Long
    For i = SelectedRow To 999

    If Cells(i, 2).Value <> 1 Then

    Cells(i,2).EntireRow.Delete

    End If

    Next i
    End If

    End Sub[/VBA]

  2. #2
    can u attach the file for better understanding.
    Regards
    Senthil

  3. #3

    deleting rows beneath active cell one by one till value is found in column....

    hi there

    please see attached simplified file for a better idea.....

    thanks
    Sunil

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That workbook has no code that looks remotely like the code that you posted, and calls a procedure that doesn't exist. Not much for us to work with.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5

    deleting rows beneath active cell one by one till value is found in column....

    Hi there
    Sorry - I deleted pretty much everything to simply the file!
    I've removed unnecessary sheets now, but left most of the code....
    Thanks
    S

  6. #6
    can anyone help with this please?????
    Thanks in advance
    Sunil

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Sunil -
    By example, if I were to select cell D22, and pressed the <Delete Task> button, do you want it to delete rows 22 thru (inclusive) 35?

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I don't think that you are doing much to help us.

    The macro behind the delete button doesn't exist.

    The sheets are protected.

    I can't believe that the tasks are eally called Main Task, Sub Task, so how are they identified.

    And so on.

    You have got to do your bit or we will walk!
    Last edited by Bob Phillips; 10-13-2008 at 04:33 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9

    deleting rows beneath active cell one by one till value is found in column....

    Hi
    I'm really sorry about that - it's not intentional! I'm trying to post a simplified file so it's easy to understand my challenge, and I'm not used to dealing with all these macros/passwords, etc.

    hopefully the attached file will work. I've unprotected the sheets.

    The situation will be that the sheets will be protected and all the gray rows hidden. The user will create Main/Sub Tasks as required using other Macros not shown. What I need is if the user clicks on any main task (you'll note they have a "1" in column B), then the macro should delete rows below it till it finds another Main task with a "1" in Column B and then stop.

    Thanks (and sorry for the hassle)
    Sunil

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub DeleteTask01()
    Dim SelectedRow As Long
    Dim i As Long

    SelectedRow = ActiveCell.Row

    If SelectedRow > 20 Then

    If Cells(SelectedRow, 2).Value <> 1 Then

    Range("B" & SelectedRow & ":B" & SelectedRow + 5).EntireRow.Delete
    Else

    i = SelectedRow + 1
    Do Until Cells(i, 2).Value = 1

    i = i + 1
    Loop

    Rows(SelectedRow).Resize(i - SelectedRow).Delete
    End If
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just a thought. If you try to delete the last main task, it will fail. You need some sort of marking identification so that the code can test for the end of the data as well as the next main task.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    thanks so much for the code (and your patience). That worked really well. And I added a blank line with the number 1 in Column B to provide the "marker".

    One last thing on this macro: i'm trying to extend the macro so it performs the exact same function on the next Sheet. (Sheet B). I did a similar thing for another Macro to Insert rows and that worked well, but when I run the following code, it deletes pretty much all the rows below.... What am I doing wrong?

    Thanks again - Sunil.

    [VBA]Sub DeleteTask01temp()
    Dim SelectedRow As Long
    Dim i As Long
    Dim SheetB As Worksheet

    Set SheetB = Sheets("Stage01-Tracking")

    SelectedRow = ActiveCell.row

    If SelectedRow > 20 Then

    If Cells(SelectedRow, 2).Value <> 1 Then

    Range("B" & SelectedRow & ":B" & SelectedRow + 5).EntireRow.Delete

    With SheetB
    Range("B" & SelectedRow & ":B" & SelectedRow + 5).EntireRow.Delete
    End With


    Else

    i = SelectedRow + 1
    Do Until Cells(i, 2).Value = 1

    i = i + 1
    Loop

    Rows(SelectedRow).Resize(i - SelectedRow).Delete

    With SheetB
    i = SelectedRow + 1
    Do Until Cells(i, 2).Value = 1

    i = i + 1
    Loop

    Rows(SelectedRow).Resize(i - SelectedRow).Delete
    End With


    End If
    End If
    Set SheetB = Nothing
    End Sub[/VBA]

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not tested, but try this

    [vba]

    Sub DeleteTask01temp()
    Dim SelectedRow As Long
    Dim i As Long
    Dim SheetB As Worksheet

    Set SheetB = Sheets("Stage01-Tracking")

    SelectedRow = ActiveCell.Row

    If SelectedRow > 20 Then

    If Cells(SelectedRow, 2).Value <> 1 Then

    Range("B" & SelectedRow & ":B" & SelectedRow + 5).EntireRow.Delete

    SheetB.Range("B" & SelectedRow & ":B" & SelectedRow + 5).EntireRow.Delete

    Else

    i = SelectedRow + 1
    Do Until Cells(i, 2).Value = 1

    i = i + 1
    Loop

    Rows(SelectedRow).Resize(i - SelectedRow).Delete

    With SheetB
    i = SelectedRow + 1
    Do Until .Cells(i, 2).Value = 1

    i = i + 1
    Loop

    .Rows(SelectedRow).Resize(i - SelectedRow).Delete
    End With
    End If
    End If
    Set SheetB = Nothing
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    that works for sub tasks, but fails when run on a main task. It fails on the line: [Do Until .Cells(i, 2).Value = 1] under With SheetB Section...
    I'll try and play around with it when I'm home this evening, unless you have any ideas.....
    Thanks
    S

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post a workbook that we can work on?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16

    deleting rows beneath active cell one by one till value is found in column....

    Hi
    Attached is the workbook. The first sheet is the one with the buttons to Insert and Delete rows which should execute the same command on the other two sheets.
    In addition to the above query, I also have a query regarding the Insert Task macro. You'll see the rows it copies to insert are deliberately hidden. How do I get the macro to unhide the first of the rows that it inserts?
    Thanks a bunch.
    S

  17. #17
    ....I hope my explanation was clear enough....
    please let me know if you need a better explanation!
    Thanks
    SM

Posting Permissions

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