Excel Macro to Cut, Paste and then delete row

colinh69

New Member
Joined
Feb 25, 2011
Messages
8
Hi,

I am using the following macro to cut and paste and then clear contents of a protected sheet but when I try to change it to delete the entire row instead of clearing the contents using "EntireRow.Delete" instead of "ClearContents" I get an error.

I would really appreciate help with this.


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect ("")
Dim lrow As Long, lcell As Long

lrow = Sheet2.Cells(Rows.Count, 18).End(xlUp).Row + 1 'next available row on sheet2
lcell = Sheet1.Cells(Rows.Count, 18).End(xlUp).Row 'last cell in column-J on sheet1

If Target.Cells.Count > 1 Then Exit Sub


If Not Intersect(Target, Range("J2:J" & lcell)) Is Nothing Then
If Target.Value <> vbNullString Then
Range(Cells(Target.Row, 1), Cells(Target.Row, 18)).Copy Destination:=Sheet2.Range("A" & lrow)
Range(Cells(Target.Row, 1), Cells(Target.Row, 18)).ClearContents
End If
End If
ActiveSheet.Protect Password:=""
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
For future reference, you will generally get better responses if you tell us what error you get and where.
Try this version:
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect ""
Dim lrow As Long, lcell As Long

lrow = Sheet2.Cells(Rows.Count, 18).End(xlUp).Row + 1 'next available row on sheet2
lcell = Sheet1.Cells(Rows.Count, "J").End(xlUp).Row 'last cell in column-J on sheet1

If Target.Cells.Count > 1 Then Exit Sub


If Not Intersect(Target, Range("J2:J" & lcell)) Is Nothing Then
If Target.Value <> vbNullString Then
Range(Cells(Target.Row, 1), Cells(Target.Row, 18)).Copy Destination:=Sheet2.Range("A" & lrow)
On Error Resume next
Application.Enableevents = False
Target.EntireRow.Delete 
Application.Enableevents = True
On Error Goto 0
End If
End If
Me.Protect Password:=""
End Sub
 
Upvote 0
Colin,

This may be irrelevant now after Rory's reply, but I tried your code and it ran fine. I did change some column values to make it fit the data I used, but I didn't get an error. (BTW, J is col 10 not 18.)

Alison
 
Upvote 0
Hi,
Thank you both for your assistance, I used Rory's and it works perfectly.
I have one more thing to ask, if I try to share the workbook it stops working, is there any way around this as I need to share this with eveyone in the office.
Thanks again
 
Upvote 0
No, since you cannot alter the protection in a shared workbook. Additionally, shared workbooks are an abomination and should be avoided if at all possible.
 
Upvote 0
Hi Rory,

Thanks for the quick reply. I gues we will have to work it in the office just using the 1 sheet.

Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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