Consulting

Results 1 to 18 of 18

Thread: Dialog box, select rows>5, delete in multiple worksheets

  1. #1
    VBAX Regular
    Joined
    Jan 2016
    Posts
    10
    Location

    Dialog box, select rows>5, delete in multiple worksheets

    Code below works good for deleting rows on the active worksheet, but I would love to have a couple of changes. I am not good at VBA, this code is a sample from another place. I would like two different codes, one for the current worksheet and one for multiple worksheets.


    1. I don't want to be able to delete rows 1-5 for this code. Some kind of dialog box that says: "You can only delete row "6" or below".


    Option Explicit
    
    
    Sub DeleteMe()
        Dim Ret As Range, Cl As Range
    
    
        On Error Resume Next
        Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
        On Error GoTo 0
    
    
        ActiveSheet.Unprotect Password:="password"
    
    
        If Not Ret Is Nothing Then Ret.EntireRow.Delete
    
    
        ActiveSheet.Protect Password:="password"
    End Sub


    2. Another similar code as above (delete row 6 or higher) with dialog box and selection, but when I select rows in worksheet A, the same rows in worksheet B and C are also deleted.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    for 1 sheet

    Sub vbax_55475_delete_rows_via_inputbox_single_ws()
    
        With Worksheets("Sheet1") 'change sheet name to suit
            .Select
            On Error Resume Next
            Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
            On Error GoTo 0
            .Unprotect Password:="password"
            If Not Ret Is Nothing Then Ret.EntireRow.Delete
            .Protect Password:="password"
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    for multiple sheets, same password

    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_same_pass()
    
        Dim ws As Worksheet
        
        For Each ws In Worksheets(Array("Sheet3", "Sheet4", "Sheet6")) 'change sheet names to suit
            With ws
                .Select
                On Error Resume Next
                Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
                On Error GoTo 0
                .Unprotect Password:="password"
                If Not Ret Is Nothing Then Ret.EntireRow.Delete
                .Protect Password:="password"
            End With
        Next ws
        
    End Sub
    Last edited by mancubus; 03-18-2016 at 05:08 AM. Reason: typo
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    for multiple sheets, different passwords
    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_diff_pass()
    
        Dim wsList, passList
        Dim i As Long
        
        wsList = Array("Sheet3", "Sheet4", "Sheet6") 'change sheet names to suit
        passList = Array("PassWordForSheet3", "PassWordForSheet4", "PassWordForSheet6") 'change passwords to suit
        
        For i = LBound(wsList) To UBound(wsList)
            With Worksheets(wsList(i))
                .Select
                On Error Resume Next
                Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
                On Error GoTo 0
                .Unprotect Password:=passList(i)
                If Not Ret Is Nothing Then Ret.EntireRow.Delete
                .Protect Password:=passList(i)
            End With
        Next ws
        
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Regular
    Joined
    Jan 2016
    Posts
    10
    Location
    Thank you mancubus!
    Is it possible to make a dialog box appear on each code when people try to delete row 1-5?

    Quote Originally Posted by mancubus View Post
    for 1 sheet

    Sub vbax_55475_delete_rows_via_inputbox_single_ws()
    
        With Worksheets("Sheet1") 'change sheet name to suit
            .Select
            On Error Resume Next
            Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
            On Error GoTo 0
            .Unprotect Password:="password"
            If Not Ret Is Nothing Then Ret.EntireRow.Delete
            .Protect Password:="password"
        End With
    
    End Sub

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    ah, i missed that bit.

    do you mean 'dont let the user select rows for deletion other than rows 1 to 5?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Regular
    Joined
    Jan 2016
    Posts
    10
    Location
    Quote Originally Posted by mancubus View Post
    ah, i missed that bit.

    do you mean 'dont let the user select rows for deletion other than rows 1 to 5?
    I have important information on row 1-5 so I want them to stay intact
    But I would like to be able to delete row 6, 7, 8 and so on.

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    try

    Sub vbax_55475_delete_rows_via_inputbox_single_ws()
         
        Dim Ret As Range
        
        With Worksheets("Sheet1") 'change sheet name to suit
            .Select
            On Error Resume Next
            Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
            On Error GoTo 0
            If Ret.Cells(1).Row < 6 Then
                MsgBox "Rows 1-5 must not be deleted. Quitting...", vbOKOnly, "Warning!"
                Exit Sub
            End If
            .Unprotect Password:="password"
            If Not Ret Is Nothing Then Ret.EntireRow.Delete
            .Protect Password:="password"
        End With
         
    End Sub
    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_same_pass()
        
        Dim ws As Worksheet
        Dim Ret As Range
         
        For Each ws In Worksheets(Array("Sheet3", "Sheet4", "Sheet6")) 'change sheet names to suit
            With ws
                .Select
                On Error Resume Next
                Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
                On Error GoTo 0
                If Ret.Cells(1).Row < 6 Then
                    MsgBox "Rows 1-5 must not be deleted. Quitting...", vbOKOnly, "Warning!"
                    Exit Sub
                End If
                .Unprotect Password:="password"
                If Not Ret Is Nothing Then Ret.EntireRow.Delete
                .Protect Password:="password"
            End With
        Next ws
         
    End Sub
    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_diff_pass()
         
        Dim wsList, passList
        Dim i As Long
        Dim Ret As Range
         
        wsList = Array("Sheet3", "Sheet4", "Sheet6") 'change sheet names to suit
        passList = Array("PassWordForSheet3", "PassWordForSheet4", "PassWordForSheet6") 'change passwords to suit
         
        For i = LBound(wsList) To UBound(wsList)
            With Worksheets(wsList(i))
                .Select
                On Error Resume Next
                Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
                On Error GoTo 0
                If Ret.Cells(1).Row < 6 Then
                    MsgBox "Rows 1-5 must not be deleted. Quitting...", vbOKOnly, "Warning!"
                    Exit Sub
                End If
                .Unprotect Password:=passList(i)
                If Not Ret Is Nothing Then Ret.EntireRow.Delete
                .Protect Password:=passList(i)
            End With
        Next ws
         
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    VBAX Regular
    Joined
    Jan 2016
    Posts
    10
    Location
    When I delete on multiple pages (same password), the code asks me to select rows first on Sheet3, then jumps to Sheet4 and asks me again, finally asking me the same on Sheet6.

    Is it possible to make it delete on all three pages without jumping to Sheet 4 and 6 and asking me the same question?
    So that if I delete row 8 on Sheet3, row 8 on Sheet4 and Sheet6 will also be deleted.
    Also making me end up on Sheet3 again when the procedure is finished.

    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_same_pass()
        
        Dim ws As Worksheet
        Dim Ret As Range
         
        For Each ws In Worksheets(Array("Sheet3", "Sheet4", "Sheet6")) 'change sheet names to suit
            With ws
                .Select
                On Error Resume Next
                Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
                On Error GoTo 0
                If Ret.Cells(1).Row < 6 Then
                    MsgBox "Rows 1-5 must not be deleted. Quitting...", vbOKOnly, "Warning!"
                    Exit Sub
                End If
                .Unprotect Password:="password"
                If Not Ret Is Nothing Then Ret.EntireRow.Delete
                .Protect Password:="password"
            End With
        Next ws
         
    End Sub

  10. #10
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_same_pass()
         
        Dim ws As Worksheet
        Dim Ret As Range
         
        On Error Resume Next
        Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
        On Error GoTo 0
         
        If Not Ret Is Nothing Then
            If Ret.Cells(1).Row < 6 Then
                MsgBox "Rows 1-5 must not be deleted. Quitting...", vbOKOnly, "Warning!"
                Exit Sub
            End If
        
            For Each ws In Worksheets(Array("Sheet3", "Sheet4", "Sheet6")) 'change sheet names to suit
                With ws
                    .Unprotect Password:="password"
                    Ret.EntireRow.Delete
                    .Protect Password:="password"
                End With
            Next ws
        End If
         
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  11. #11
    VBAX Regular
    Joined
    Jan 2016
    Posts
    10
    Location
    The code deletes the selected rows at Sheet3, but when it comes to Sheet4 I receive Run-time Error '424': Object required, and the code stops working

    Quote Originally Posted by mancubus View Post
    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_same_pass()
         
        Dim ws As Worksheet
        Dim Ret As Range
         
        On Error Resume Next
        Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
        On Error GoTo 0
         
        If Not Ret Is Nothing Then
            If Ret.Cells(1).Row < 6 Then
                MsgBox "Rows 1-5 must not be deleted. Quitting...", vbOKOnly, "Warning!"
                Exit Sub
            End If
        
            For Each ws In Worksheets(Array("Sheet3", "Sheet4", "Sheet6")) 'change sheet names to suit
                With ws
                    .Unprotect Password:="password"
                    Ret.EntireRow.Delete
                    .Protect Password:="password"
                End With
            Next ws
        End If
         
    End Sub

  12. #12
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    dont quote previous messages or codes pls.

    as i almost never use select statement, sometimes i hardly remember its use and limitations.

    use below. it includes favorite 'selecting' as well.

    Sub vbax_55475_delete_rows_via_inputbox_multiple_wss_same_pass()
         
        Dim ws As Worksheet
        Dim RowsToDel As Range
        Dim RowNums
        Dim i As Long
        
        On Error Resume Next
        Set RowsToDel = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
        On Error GoTo 0
         
        If Not RowsToDel Is Nothing Then
            If RowsToDel.Cells(1).Row < 6 Then
                MsgBox "Rows 1-5 must not be deleted. Quitting...", vbOKOnly, "Warning!"
                Exit Sub
            End If
            
            RowNums = Split(RowsToDel.Address(0, 0), ",")
    
            For Each ws In Worksheets(Array("Sheet3", "Sheet4", "Sheet6")) 'change sheet names to suit
                With ws
                    .Unprotect Password:="password"
                    For i = UBound(RowNums) To LBound(RowNums) Step -1
                        .Rows(RowNums(i)).Delete
                    Next
                    .Protect Password:="password"
                End With
            Next ws
        End If
         
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  13. #13
    VBAX Regular
    Joined
    Jan 2016
    Posts
    10
    Location
    I tried your new code but received runtime error 1004 at

    .Rows(RowNums(i)).Delete

  14. #14
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    worked for me. check the attachment.
    Attached Files Attached Files
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  15. #15
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Why is there the "cannot delete row 6 or higher" restriction?

    If I wanted row 6 deleted, I'd just delete row 1 and then delete the (new) row 5.

  16. #16
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    See post #7
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  17. #17
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    if the deletion process will depend on the user's selection, it means rows to be deleted may/will change each time you run the code.
    and that means deletion will be based on some conditions.
    try to analyze those conditions and adopt the code above to these conditions.

    googling "delete rows based on condition" will give you thousands of examples.

    in my projects, rows to be deleted are filtered based on column values and visible rows under header row are deleted.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  18. #18
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Why not ?

    Sub M_snb()
        Set rtbd = Intersect(Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8), Rows(6).Resize(Rows.Count - 6))
        
        If Not rtbd Is Nothing Then rtbd.Delete
    End Sub

Posting Permissions

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