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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
where did it fail on my solution?

Dave

it seems to get stuck into a loop when it gets to

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

on the debug it goes back up to the first line
 
Upvote 0
it seems to get stuck into a loop when it gets to

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

on the debug it goes back up to the first line

tad strange as runs ok for me
made couple subtle changes - see if any help

VBA Code:
Sub CopyDone()
    Dim Cell As Range, CopyRange As Range
    Dim Target As Range
    Dim wsSource As Worksheet
   
    Set wsSource = ThisWorkbook.Worksheets("Main Tab")
    Set Target = ThisWorkbook.Worksheets("Done").Range("A2")
   
    For Each Cell In wsSource.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
        With CopyRange.EntireRow
        .Copy Target
        .Delete shift:=xlShiftUp
    End With
    End If
End Sub
 
Upvote 0
Is your data in a structured table?
 
Upvote 0
I think I worked out the issue, the table that was given to me to look at consisted of a couple of tables, Now that I have organised it into one table the macro works.

However I was wondering, is there a way to make the data copy not necessarily to the top row of another sheet but instead to the next empty row?

Also can it copy only the value?
 
Upvote 0
However I was wondering, is there a way to make the data copy not necessarily to the top row of another sheet but instead to the next empty row?

Also can it copy only the value?

Hi,
try this update

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(.Cells(.Rows.Count, "A").End(xlUp).Row + 1, 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
        With CopyRange.EntireRow
        .Copy
        Target.PasteSpecial xlPasteValues
        .Delete shift:=xlShiftUp
    End With
    End If
    Application.CutCopyMode = False
End Sub

Dave
 
Upvote 0
Hi Dave, Thanks for your response, this is almost perfect, just unfortunately the data copies underneath the table on the second tab rather than into the next free line in the table.
 
Upvote 0
Hi Dave, Thanks for your response, this is almost perfect, just unfortunately the data copies underneath the table on the second tab rather than into the next free line in the table.

if your data is structured as a Table- then locating last low may need a different approach

not tested but see if this change resolves

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
        With CopyRange.EntireRow
        .Copy
        Target.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 + 1
    On Error GoTo 0
End Function

Dave
 
Last edited:
Upvote 0
Hi Dave, this still seems to be Putting the data underneath the table. Any other ideas on how to get it to populate the table?
 
Upvote 0
Hi Dave, this still seems to be Putting the data underneath the table. Any other ideas on how to get it to populate the table?

Sorry not much time at moment but try these changes

replace this section
VBA Code:
If Not CopyRange Is Nothing Then
        With CopyRange.EntireRow
        .Copy
        Target.PasteSpecial xlPasteValues
        .Delete shift:=xlShiftUp
    End With
    End If

with this

VBA Code:
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

In the Function code delete the "+1"

Rich (BB code):
MatchCase:=False).Row + 1

Dave
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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