Copy & Paste - Automatically

SG666

New Member
Joined
Aug 22, 2018
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I know this has come up a million times before, and I have search for the answer and seemed to find some, but when I try to use the code it doesn't seem to work for me, as it deletes the original row or wont paste into the correct area on my second worksheet.

What I'd like to do is be able to fill in the below example, and when i choose FAIL it will copy the entire row into the next available row on the Failures worksheet

1672240772708.png


So that the above "test" would result in a failure list as per the below example...

1672240931415.png


This is the code that I'd found online to try and edit to fit my needs but I'm struggling to get it to work with my VERY basic knowledge of macros....

1672241220784.png


Sub MoveBasedonValue()

Dim xRg As Range
Dim xCell As Range
Dim A As Long
Dim B As Long
Dim C As Long

A = Worksheets("master").UsedRange.Rows.Count
B = Worksheets("completed").UsedRange.Rows.Count

If B = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("completed").UsedRange) = 0 Then B = 0
End If
Set xRg = Worksheets("master").Range("D1:D" & A)
On Error Resume Next
Application.ScreenUpdating = False
For C = 1 To xRg.Count
If CStr(xRg(C).Value) = "Done" Then
xRg(C).EntireRow.Copy Destination:=Worksheets("completed").Range("A" & B + 1)
xRg(C).EntireRow.Delete
If CStr(xRg(C).Value) = "Done" Then
C = C - 1
End If
B = B + 1
End If
Next
Application.ScreenUpdating = True
End Sub


Any help with getting this working would be fantastic and greatly appreciated!!

Thanks,
Shwan
 
Hi SG666,

on sheet Report the first line of entries is 10, Column to check is "J", entry must be "Fail" (I changed the value to be lower case "fail"), first line on sheet Failures will be the first free row as that is a variable which will be calculated on each run.

You would need to give us information about where the data is located (row number and columns to copy) and what the contents of the first row are.

Holger
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi,

Sorry this is probably taking longer then you might have hoped.

I have duplicated the Reports WS so that the Failures WS looks exactly the same, so in theory the items that disappear from "Reports" should reappear in the available rows within "Failures" (i have removed the original Failures to avoid conflicts), however this still doesn't work. They will disappear from Reports, but wont appear on Failures.

The below screengrab shows the kind of data that will be input, you can see that this is within the WS "Reports", the result column is empty at present but its a dropdown list that allows the choice of either Pass or Fail.

If i choose Fail from row 10, column J it will disappear from that worksheet, but nothing happens on the other worksheet.

1672328940396.png
1672329073353.png


I hope this helps, and thank you so far for you help and patience!
 
Upvote 0
Hi SG666,

please find my sample workbook here where you can check after download if the procedure is triggered once any status on Report is changed to Fail.

Ciao,
Holger
 
Upvote 0
Thanks for this, seems to work perfectly!

How hard is it to change the code to get it to only copy on the data from the Report to the Failures and not delete it after?
 
Upvote 0
Hi SG666,

instead of

VBA Code:
If Not Intersect(Target, Columns("J:J")) Is Nothing Then
  If LCase(Target.Value) = "fail" Then
    Set wsFailures = Worksheets("Failures")
    lngWrite = wsFailures.Range("A" & wsFailures.Rows.Count).End(xlUp).Row + 1
    wsFailures.Range("A" & lngWrite & ":J" & lngWrite).Value = Range("A" & Target.Row & ":J" & Target.Row).Value
    Application.EnableEvents = False
    Range("A" & Target.Row & ":J" & Target.Row).Delete
    Application.EnableEvents = True
    Set wsFailures = Nothing
  End If
End If

which will delete either comment the lines

VBA Code:
If Not Intersect(Target, Columns("J:J")) Is Nothing Then
  If LCase(Target.Value) = "fail" Then
    Set wsFailures = Worksheets("Failures")
    lngWrite = wsFailures.Range("A" & wsFailures.Rows.Count).End(xlUp).Row + 1
    wsFailures.Range("A" & lngWrite & ":J" & lngWrite).Value = Range("A" & Target.Row & ":J" & Target.Row).Value
'    Application.EnableEvents = False
'    Range("A" & Target.Row & ":J" & Target.Row).Delete
'    Application.EnableEvents = True
    Set wsFailures = Nothing
  End If
End If

or delete the commented lines. I would take a different route here to opt for both possibilties by using a Boolean to determine whether to delete or not like

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lngWrite As Long
Dim wsFailures As Worksheet

Const cblnKeep As Boolean = True      'True means keep Data, False will delete

If Target.Cells.Count > 1 Then Exit Sub
If Target.Row < 10 Then Exit Sub

If Not Intersect(Target, Columns("J:J")) Is Nothing Then
  If LCase(Target.Value) = "fail" Then
    Set wsFailures = Worksheets("Failures")
    lngWrite = wsFailures.Range("A" & wsFailures.Rows.Count).End(xlUp).Row + 1
    wsFailures.Range("A" & lngWrite & ":J" & lngWrite).Value = Range("A" & Target.Row & ":J" & Target.Row).Value
    If Not cblnKeep Then
      Application.EnableEvents = False
      Range("A" & Target.Row & ":J" & Target.Row).Delete
      Application.EnableEvents = True
    End If
    Set wsFailures = Nothing
  End If
End If

End Sub

Ciao,
Holger
 
Upvote 0
This is great thanks!

So am I right in thinking if i use the last example (with the Boolean) I can change the below from either True (to keep the data that has been copied) or False (to delete it once its been copied) ?

Const cblnKeep As Boolean = True
 
Upvote 0
Hi SG666,

you're right on this.

Holger
 
Upvote 0

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

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