Copy and pasting Data (on the same sheet) using VBA

mikeHunt

New Member
Joined
Jul 16, 2018
Messages
5
Hey Everyone!

I'm having trouble writing code to copy data from a table and paste the important columns under the original to compare the data. I needed to put this in a loop to go through all the sheets of the workbook.. Here is my code.. Any help will be appreciated!

Sub CopyData()

Dim WS_Count As Integer
Dim I As Integer

WS_Count = ActiveWorkbookWorksheets.Count

For I = 1 To WS_Count

ActiveWorkbook.Worksheets(I).Range("A1:F80").Copy
With ActiveSheet.Range("A85")
.PasteSpecial x1PasteFormats
.PasteSpecial x1PasteValue
End With

ActiveWorkbook.Worksheets(I).Range("L1:L80").Copy
With ActiveSheets.Range("G85")
.PasteSpecial x1PasteFormats
.PasteSpecial x1PasteValues
End With

Next I

End Sub



It Is givin me errors saying all merged cells need to be the same size??
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
vba does not easily lend itself to copy data with merged cells due to the underlying application that merged the cells. If you are copying a range where part of a merged cell is within the specified copy range and part of the merged cell is not withing the specified copy range, vba does not know what to do with the other part of the merged cell and will send you the error message. There are other conditions with merged cells that cause the error to display. It might be easier to unmerge the cells then remerge them to copy your data.
 
Upvote 0
As well as JLGWhiz's comments on merged cells the constants x1PasteFormats and x1PasteValue should actually be xlPasteFormats and xlPasteValue.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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