VBA Code for copy and paste entire row to another sheet

caos88

Board Regular
Joined
Mar 12, 2020
Messages
66
Office Version
  1. 2010
Platform
  1. Windows
Good morning all,

I have data on 1 sheet that i want to copy and paste (clearing the data ) to another sheet. I wish to connect this macro to a "form control button" so when the operator is filling up the form, he clicks on the button for storage and the data will be moved. So far i have the current code for cut and paste which works well. Any suggestions on how to modify it? in the case below, the code works when triggered from the word "Closed" and i don't really need it now. Thank you.

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


    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets("Sheet1")
    Set Target = ActiveWorkbook.Worksheets("Sheet2")


    j = Target.Range("A" & Rows.Count).End(xlUp).Row + 1     ' Start copying 1 down from the last row on sheet
    For Each c In Source.Range("A1:K1000")   ' Do 1000 rows
        If c = "Closed" Then
           Source.Rows(c.Row).Copy Target.Rows(j)
           Source.Rows(c.Row).Cut Target.Rows(j)
           j = j + 1
      
        End If
    Next c
End Sub
 
Last edited by a moderator:
This assumes that you have a header that is not to be copied
Code:
Sub Maybe_1()
With ActiveSheet
    With .Cells(1, 1).Resize(.UsedRange.Rows.Count - 1, .UsedRange.Columns.Count).Offset(1)
        .Copy Sheets("final sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .ClearContents
    End With
End With
End Sub
Copies the whole sheet.
Code:
Sub Maybe_2()
    With ActiveSheet.UsedRange
        .Copy Sheets("final sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .ClearContents
    End With
End Sub

You do mention "for all the rows filled with data"
Does that mean that there could be empty rows in your data range or are all the filled rows contiguous?
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
This assumes that you have a header that is not to be copied
Code:
Sub Maybe_1()
With ActiveSheet
    With .Cells(1, 1).Resize(.UsedRange.Rows.Count - 1, .UsedRange.Columns.Count).Offset(1)
        .Copy Sheets("final sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .ClearContents
    End With
End With
End Sub
Copies the whole sheet.
Code:
Sub Maybe_2()
    With ActiveSheet.UsedRange
        .Copy Sheets("final sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .ClearContents
    End With
End Sub

You do mention "for all the rows filled with data"
Does that mean that there could be empty rows in your data range or are all the filled rows contiguous?

sorry for the misunderstanding and yes, there might be rows without data that doesn't copy
 
Upvote 0
I guess you don't read posts. I am referring to Post #9, last sentence.
 
Upvote 0
This assumes that when a cell in Column A is empty that the whole row is empty
Code:
Sub Maybe_3()
Application.ScreenUpdating = False
    With ActiveSheet
        .UsedRange.Columns(1).SpecialCells(4).EntireRow.Delete
        .UsedRange.SpecialCells(2).Copy Sheets("final sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .UsedRange.ClearContents
    End With
Application.ScreenUpdating = True
End Sub


If the Cell in Column A is empty but other cells have data.
Code:
Sub Maybe_4()
Dim lr As Long, lc As Long, i As Long
lr = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
For i = 2 To lr    '<---- Start at Row2. Change as required
If Not Application.CountA(Rows(i)) = 0 Then
Cells(i, 1).Resize(, lc).Copy Sheets("final sheet").Cells(Sheets("final sheet").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row, 1).Offset(1)
End If
Next i
ActiveSheet.Cells(1, 1).Resize(lr, lc).ClearContents    '<---- Change (1, 1) to (2, 1) to keep header
End Sub


How many rows max are we talking about that need to be copied and pasted?
 
Upvote 0
This assumes that when a cell in Column A is empty that the whole row is empty
Code:
Sub Maybe_3()
Application.ScreenUpdating = False
    With ActiveSheet
        .UsedRange.Columns(1).SpecialCells(4).EntireRow.Delete
        .UsedRange.SpecialCells(2).Copy Sheets("final sheet").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .UsedRange.ClearContents
    End With
Application.ScreenUpdating = True
End Sub


If the Cell in Column A is empty but other cells have data.
Code:
Sub Maybe_4()
Dim lr As Long, lc As Long, i As Long
lr = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
For i = 2 To lr    '<---- Start at Row2. Change as required
If Not Application.CountA(Rows(i)) = 0 Then
Cells(i, 1).Resize(, lc).Copy Sheets("final sheet").Cells(Sheets("final sheet").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row, 1).Offset(1)
End If
Next i
ActiveSheet.Cells(1, 1).Resize(lr, lc).ClearContents    '<---- Change (1, 1) to (2, 1) to keep header
End Sub


How many rows max are we talking about that need to be copied and pasted?

Thank you, it's what I was looking for
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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