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? Commented 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.. Commented 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!? Commented Jun 18, 2014 at 11:38
  • Yes.. And the second column is pastes infinite times in sheet 2 Commented Jun 18, 2014 at 11:39
  • You can't copy an entire column to anywhere besides row 1. Commented Jun 18, 2014 at 11:42

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.