0

I want to copy some columns of an excel sheet to another sheet.

I have written the code, which doesnt work. It gets into an infinite loop, and exits with an error. The code is:

Sub customCopy()

Dim i As Integer
Dim j As Integer


j = 1
For i = 1 To 700
   If i Mod 5 = 2 Then
       Columns(i).Copy Destination:=Sheets(2).Rows(j)
       j = j + 1
   End If
   If i Mod 5 = 3 Then
       Columns(i).Copy Destination:=Sheets(2).Rows(j)
       j = j + 1
   End If
   If i Mod 5 = 4 Then
       Columns(i).Copy Destination:=Sheets(2).Rows(j)
       j = j + 1
   End If
Next i

End Sub

Please help..

5
  • What is there error and where does it occur? Did you try stepping through the code in single-step mode to find out why the error occurs?
    – MP24
    Jun 18, 2014 at 11:28
  • Error code 1004 - "We cant paste because the Copy and the Paste area are not the same size". i is not getting incremented, and the same row is getting copied infinite times..
    – Sanket
    Jun 18, 2014 at 11:37
  • This error appears once i equals 2, I assume, i.e. the first time your copy code should run!?
    – MP24
    Jun 18, 2014 at 11:38
  • Yes.. And the second column is pastes infinite times in sheet 2
    – Sanket
    Jun 18, 2014 at 11:39
  • You can't copy an entire column to anywhere besides row 1.
    – Rory
    Jun 18, 2014 at 11:42

1 Answer 1

1

Copying a column into a row won't work.............a row is too small!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.