Code does not work, copy and paste data from a sheet to another

Jeffreyxx01

Board Regular
Joined
Oct 23, 2017
Messages
156
Hi guys, I got some problems
Can someone help on my code, it highlight the lastrow in yellow.

Thanks.

Code:
Sub copydata()
Dim LastRow As Long


Sheets("Datadump").Range("E:G,S:S,AW:AW,AY:AY,BE:BE,CC:CC,CF:CV").Copy
With Worksheets("Clean")
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    .Range("A1:Y" & LastRow).ClearContents
    .Range("A1").PasteSpecial xlPasteValues
End With


End Sub
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
Sub Copydata()
Dim iLastRow As Long


Worksheets("sheet2").Select
Cells.ClearContents


Sheets("sheet1").Select
iLastRow = ActiveSheet.UsedRange.Rows.Count
Sheets("sheet1").Range("E:G,S:S,AW:AW,AY:AY,BE:BE,CC:CC,CF:CV").Copy


Worksheets("sheet2").Select
ActiveSheet.Paste 
end sub
 
Upvote 0
Actually after assigning a button,
this macro does not work, it gives me the following error:

Run time error 1004
" you can't paste this here because the copy area and paste area aren't the same size. Select just one cell in the paste area or an area that's the same size, and try pasting again."

Code:
Sub Copydata()

Dim iLastRow As Long




Worksheets("Clean").Select
Cells.ClearContents




Sheets("Datadump").Select
iLastRow = ActiveSheet.UsedRange.Rows.Count
Sheets("Datadump").Range("E:G,S:S,AW:AW,AY:AY,BE:BE,CC:CC,CF:CV").Copy




Worksheets("Clean").Select
ActiveSheet.Paste
End Sub
 
Upvote 0
I think you need to use a line of code like this at the end of your script.
Code:
Sheets("Clean").Range("A1").PasteSpecial
Application.CutCopyMode = False
 
Upvote 0
This is the final code and now it works,
I added xlPasteValues as I needed all values,

Thanks guys.

Code:
Sub Copydata()

Dim iLastRow As Long




Worksheets("Clean").Select
Cells.ClearContents




Sheets("Datadump").Select
iLastRow = ActiveSheet.UsedRange.Rows.Count
Sheets("Datadump").Range("E:G,S:S,AW:AW,AY:AY,BE:BE,CC:CC,CF:CV").Copy




Sheets("Clean").Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False


End Sub
 
Upvote 0
Glad I was able to help you. If your copying entire columns you always have to paste to Row(1)
This is the final code and now it works,
I added xlPasteValues as I needed all values,

Thanks guys.

Code:
Sub Copydata()

Dim iLastRow As Long




Worksheets("Clean").Select
Cells.ClearContents




Sheets("Datadump").Select
iLastRow = ActiveSheet.UsedRange.Rows.Count
Sheets("Datadump").Range("E:G,S:S,AW:AW,AY:AY,BE:BE,CC:CC,CF:CV").Copy




Sheets("Clean").Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False


End Sub
 
Upvote 0
Do you have an idea how I can make this code work?
The error highlight the red one and it says: Compile error: Invalid or unqualified reference

Code:
[COLOR=#ff0000]Sub CopyData()[/COLOR]


Dim iLastRow As Long
Dim LastRow As Long




Worksheets("RawData2").Select
Range("A11:CF" & LastRow).ClearContents
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row




Sheets("RawData").Select
iLastRow = ActiveSheet.UsedRange.Rows.Count
Sheets("RawData").Range("A2:CF" & LastRow).Copy




Sheets("RawData2").Range("A11").PasteSpecial xlPasteValues
Application.CutCopyMode = False


End Sub
 
Upvote 0
Try this:

You have to define lastrow before you use it:

See I moved that line of code up one row.

Code:
Sub CopyData()

Dim iLastRow As Long
Dim LastRow As Long


Worksheets("RawData2").Select
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range("A11:CF" & LastRow).ClearContents


Sheets("RawData").Select
iLastRow = ActiveSheet.UsedRange.Rows.Count
Sheets("RawData").Range("A2:CF" & LastRow).Copy


Sheets("RawData2").Range("A11").PasteSpecial xlPasteValues
Application.CutCopyMode = False

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,529
Messages
6,114,155
Members
448,554
Latest member
Gleisner2

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