Autodelete a row from protected sheet and move to another protected sheet based on date

SaraWitch

Active Member
Joined
Sep 29, 2015
Messages
322
Office Version
  1. 365
Platform
  1. Windows
Hello peeps,

In a protected sheet ("Placements"), I have a formula in column U that calculates a date from a range of other cells. When a date is populated, I want the row to automatically delete and paste into another protected sheet ("Leavers"). I think I'm almost there with the formula below but know I have to change the <> in line 4 (I've tried some date text but it doesn't work).

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect ("password")
    Dim NextRow As Long
    If Target.Column <> 21 Then Exit Sub
    Application.EnableEvents = False
    With Sheets("Leavers")
        NextRow = .Cells(Rows.Count, 8).End(xlUp).Row + 1
        Target.EntireRow.Copy Destination:=.Cells(NextRow, 1)
        Target.EntireRow.Delete
    End With
    Application.EnableEvents = True
ActiveSheet.Protect ("password")
End Sub

Any help would be gratefully received :)
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
And is it further possible to re-protect the sheets with the same password the row/s have moved?
If you are just using standard protection (i.e. you haven't changed any of the options when you protected the sheets then you just do it the same way as you have done in the original post.

VBA Code:
Worksheets("EBC EA Leavers").Unprotect "YourPassword1"
Worksheets("EBC EA Placements").Unprotect "YourPassword2"
 
' Rest of your code

Worksheets("EBC EA Leavers").Protect "YourPassword1"
Worksheets("EBC EA Placements").Protect "YourPassword2"

If you have changed any of the protection options then the easiest thing to do is use the macro recorder when setting the protection manually and it will produce the code you need... something like

VBA Code:
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFiltering:=True
    ActiveSheet.EnableSelection = xlUnlockedCells

then just replace ActiveSheet with the relevant sheet like the below (you'll have to add the Password:="myPassword", as it won't record your password)

VBA Code:
   Worksheets("EBC EA Leavers").Protect Password:="myPassword", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFiltering:=True
   Worksheets("EBC EA Leavers").EnableSelection = xlUnlockedCells
 
Upvote 1
Amazing! I can't tell you what a difference this will make to the management of the workbook!!

Thank you so much, @MARK858 :biggrin:

(I wonder when you get a moment, you could remove the link to the workbook in your post #37... ...thank you again!)
 
Upvote 0
(I wonder when you get a moment, you could remove the link to the workbook in your post #37... ...thank you again!)
I will on this occasion but in future please desensitize any sensitive data before posting it on a public forum

From post 31

Before uploading
1) make sure you sanitize any sensitive data
 
Upvote 0
Thank you (there was no sensitive data - I was just being cautionary).

Again, I appreciate all your help :)
 
Upvote 0
So sorry, @MARK858 - is there a way that when a row is moved it matches the conditional formatting in the new sheet (which is slightly different to the sheet from which the row is being moved)? (Every time I think I've thought of everything! :rolleyes: 😝)
 
Upvote 0
Just copy the data across as values

VBA Code:
Sub Move_Row_to_Leavers_Sheet()
Dim lr As Long, i As Long
 
Application.ScreenUpdating = False
 
  
Worksheets("EBC EA Leavers").Unprotect "YourPassword1"
Worksheets("EBC EA Placements").Unprotect "YourPassword2"
 
    For i = Sheets("EBC EA Placements").Columns(3).Find("*", , xlValues, , xlByRows, xlPrevious).Row To 8 Step -1
     
        If Sheets("EBC EA Placements").Cells(i, 22).Value = "Left" Then
         
         
            With Worksheets("EBC EA Leavers")
                lr = .Columns(3).Find("*", , xlValues, , xlByRows, xlPrevious).Row
             
                If lr < 8 Then
                    Sheets("EBC EA Placements").Cells(i, 22).EntireRow.Copy
                    .Cells(8, 1).PasteSpecial xlValues
                    Sheets("EBC EA Placements").Cells(i, 22).EntireRow.Delete
                Else
                    Sheets("EBC EA Placements").Cells(i, 22).EntireRow.Copy
                    .Cells(lr + 1, 1).PasteSpecial xlValues
                    Sheets("EBC EA Placements").Cells(i, 22).EntireRow.Delete
                End If
            
               Application.CutCopyMode = False
             
            End With
    
       End If
 
    Next

 
Worksheets("EBC EA Leavers").Protect "YourPassword1"
Worksheets("EBC EA Placements").Protect "YourPassword2"
 
Application.ScreenUpdating = True

End Sub
 
Upvote 1
Hi @MARK858.

Your wonderful code moves a row to another sheet beautifully. I was just wondering if there's a way to move it to the bottom of the list, rather than row 8, which would be at the top every time. There are many empty rows (but with formulas) in the Leaver's sheet but I would like the moved row to go in the next available row (say in the next blank row in column D) - is this possible at all?
 
Upvote 0
There are many empty rows (but with formulas)
They are either empty rows or they have formulas, they can't be both

I was just wondering if there's a way to move it to the bottom of the list, rather than row 8

The code I posted looks for the last cell with data in column C, if that row is less than row 8 it pastes in row 8. If the last row with values is row is greater than or equal to row 8 then it pastes in the next row after the last row with values in column C, if you want Column D then change the 3 to 4
Rich (BB code):
lr = .Columns(3).Find

If you want something else then you need to make your description clearer (especially about the formulas and where you want the last row in relation to the formulas and the results they return)
 
Last edited:
Upvote 1

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top