Move data from one sheet to another based on value

JJabra

New Member
Joined
Aug 19, 2019
Messages
37
Hi Guys,

I want a code that moves a whole row from one tab to another when there is the word done in row L. This would run on the press of a button.

I have the below code, just not sure exactly how to adapt it to delete the row

VBA Code:
Sub CopyDone()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet

    
    Set Source = ActiveWorkbook.Worksheets("Main Tab")
    Set Target = ActiveWorkbook.Worksheets("Done")

    j = 2
    For Each c In Source.Range("L2:L1000")
        If c = "Done" Then
           Source.Rows(c.Row).Copy Target.Rows(j)
           j = j + 1
        End If
    Next c
End Sub


Any help would be greatly appreciated. Thanks in advance!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi,
try

VBA Code:
Sub CopyDone()
    Dim Cell As Range, CopyRange As Range
    Dim Target As Range
    Dim Source As Worksheet
    
    Set Source = ActiveWorkbook.Worksheets("Main Tab")
    Set Target = ActiveWorkbook.Worksheets("Done").Range("A2")
    
    For Each Cell In Source.Range("L2:L1000")
        If UCase(Cell) = "DONE" Then
            If CopyRange Is Nothing Then
                Set CopyRange = Cell
            Else
                Set CopyRange = Union(CopyRange, Cell)
            End If
        End If
    Next Cell
    
    If Not CopyRange Is Nothing Then
        With CopyRange.EntireRow
        .Copy Target
        .Delete shift:=xlShiftUp
    End With
    End If
End Sub

Dave
 
Upvote 0
Another option
VBA Code:
Sub CopyDone()
    Dim Source As Worksheet
    Dim Target As Worksheet

    
    Set Source = ActiveWorkbook.Worksheets("Main Tab")
    Set Target = ActiveWorkbook.Worksheets("Done")

    With Source
      .Range("L1:L1000").AutoFilter 1, "Done"
      .AutoFilter.Range.Offset(1).EntireRow.Copy Target.Range("A2")
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Hi both thanks for your quick responses, When attempting to run the macros both came up with "Run-time error '1004' Application- defined or object-defined error" I'm not sure i have missed something?
 
Upvote 0
Which line of code gave the error?
 
Upvote 0
Hi both thanks for your quick responses, When attempting to run the macros both came up with "Run-time error '1004' Application- defined or object-defined error" I'm not sure i have missed something?

Are any of the sheets protected?

Dave
 
Upvote 0
It came up when i got to

.Range("L1:L1000").AutoFilter 1, "Done"
.AutoFilter.Range.Offset(1).EntireRow.Copy Target.Range("A2")
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
 
Upvote 0
Which of those lines were highlighted when you clicked Debug?
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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