PDA

View Full Version : [SOLVED] Loop through Columns - Copy & Paste



LordDragon
10-04-2015, 07:49 AM
I seem to be missing something in my code.

I'm getting no errors, but it isn't working.

It is supposed to copy each cells data from one sheet and paste it in another.

Here is a snippet of the code:



For Each Cell In Range("A2:BP2")
Range(Cells(Cell.Row, "A"), Cells(Cell.Row, "A")).Copy
Sheets("Rig Survey Form").Range("D4").PasteSpecial xlPasteValues
Range(Cells(Cell.Row, "B"), Cells(Cell.Row, "B")).Copy
Sheets("Rig Survey Form").Range("N4").PasteSpecial xlPasteValues
Range(Cells(Cell.Row, "C"), Cells(Cell.Row, "C")).Copy
Sheets("Rig Survey Form").Range("C6").PasteSpecial xlPasteValues
Range(Cells(Cell.Row, "D"), Cells(Cell.Row, "A")).Copy
Sheets("Rig Survey Form").Range("M6").PasteSpecial xlPasteValues

Range(Cells(Cell.Row, "BO"), Cells(Cell.Row, "A")).Copy
Sheets("Rig Survey Form").Range("J66").PasteSpecial xlPasteValues
Range(Cells(Cell.Row, "BP"), Cells(Cell.Row, "B")).Copy
Sheets("Rig Survey Form").Range("A68").PasteSpecial xlPasteValues

Next Cell


End With


I'm not good with loops, but I'm not against using them. I just don't see what I'm doing wrong.

p45cal
10-04-2015, 12:06 PM
Does this do it (I'm not clear on what you want to copy)?:
DestnCellsAddresses = Array("D4", "N4", "C6", "M6", "J66", "A68")
i = 0
For Each cll In Range("A2:D2,BO2:BP2").Cells
Sheets("Rig Survey Form").Range(DestnCellsAddresses(i)).Value = cll.Value
i = i + 1
Next cll

LordDragon
10-05-2015, 10:13 AM
p45cal,

As usual, you managed to make it simply work. Thanks.

By the way, I figured out where my problem was before.

The code I was using, while much larger than what you provided, worked. However, it is written in a Function. I forgot to add the call to that function in the sub that the button activated. :doh:

Thank again for the help though.