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:

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,957
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
 

Jeffreyxx01

Board Regular
Joined
Oct 23, 2017
Messages
156
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,263
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

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
 

Jeffreyxx01

Board Regular
Joined
Oct 23, 2017
Messages
156
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,263
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

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
 

Jeffreyxx01

Board Regular
Joined
Oct 23, 2017
Messages
156
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,263
Office Version
  1. 2013
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,259
Messages
5,600,572
Members
414,389
Latest member
MarkElla

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
Top