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!
 
Hi Dave, thank you for getting back to me,

Where I have made the changes detailed above, I am now getting the following error pop up;

Run-time error '91':

Object variable or With block variable not Set

Do you know whats causing this and how to fix it?
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I would hazard a guess that you have not deleted the +1 value in the function

Here are both codes with all updates

VBA Code:
Sub CopyDone()
    Dim Cell As Range, CopyRange As Range
    Dim Target As Range
    Dim Source As Worksheet
    
    Set Source = ThisWorkbook.Worksheets("Main Tab")
   
     With ThisWorkbook.Worksheets("Done")
        Set Target = .Cells(NewRow(.Name), 1)
     End With
    
    For Each Cell In Source.Range("L2:L1000")
        If UCase(Cell.Value) = "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
        Target.ListObject.ListRows.Add AlwaysInsert:=True
        With CopyRange.EntireRow
        .Copy
        Target.Offset(1).PasteSpecial xlPasteValues
        .Delete shift:=xlShiftUp
    End With
    End If
    Application.CutCopyMode = False
End Sub

Function NewRow(ByVal sh As String) As Long
    On Error Resume Next
    NewRow = Worksheets(sh).Cells.Find(What:="*", After:=Worksheets(sh).Range("A1"), _
                                        Lookat:=xlPart, LookIn:=xlFormulas, _
                                        SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
                                        MatchCase:=False).Row
    On Error GoTo 0
End Function

untested but hopefully, will do what you want

Dave
 
Upvote 0
Hi Dave, I had removed the +1 and it does copy the data to the first available row as intended, the only issue seems to be there is an error in deleting the row on the main tab as the error pops up after the data is moved
 
Upvote 0
Hi Dave, I had removed the +1 and it does copy the data to the first available row as intended, the only issue seems to be there is an error in deleting the row on the main tab as the error pops up after the data is moved

whats the error?

Dave
 
Upvote 0
Just made dummy workbook & code seems to work ok for me

Does the code stop for you on this line

VBA Code:
.Delete shift:=xlShiftUp

Dave
 
Upvote 0
It does appear so yes, also now coming up saying Delete method of range class failed. When Debugging it stops on the above mentioned line
 
Upvote 0
I take it sheet is not protected?

If still having issues can you place copy of of the workbook with sample data in a dropbox?

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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