Copy Columns

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Copy Columns

Post by adam »

Hi,

The following code copies column contents from column I25:I33 to column D and column K25:K33 to column E.

Code: Select all

Sub Copy()
Range("I25:I33").Copy Destination:=Sheets("Datasheet").Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
Range("K25:K33").Copy Destination:=Sheets("Datasheet").Range("E" & Rows.Count).End(xlUp).Offset(1, 0)
End Sub
Suppose if column "I" has three data rows and column "K" has two data rows the code copies the data to the last empty rows in both the columns D and E.

How could I change the code so that the code copies data to the last empty row of column D only so that the data from column K also gets copied to the same row as with that of the column D.

I hope I've made my question clear.

Any help on this would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78548
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy Columns

Post by HansV »

Code: Select all

Sub Copy()
  Dim rng As Range
  Set rng = Sheets("Datasheet").Range("D" & Rows.Count).End(xlUp).Offset(1, 0)
  Range("I25:I33").Copy Destination:=rng
  Range("K25:K33").Copy Destination:=rng.Offset(0, 1)
End Sub
or

Code: Select all

Sub Copy()
  Dim r As Long
  r = Sheets("Datasheet").Range("D" & Rows.Count).End(xlUp).Row + 1
  Range("I25:I33").Copy Destination:=Sheets("Datasheet").Range("D" & r)
  Range("K25:K33").Copy Destination:=Sheets("Datasheet").Range("E" & r)
End Sub
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy Columns

Post by adam »

Thanks for the help Hans. Both the codes work fine. By the way whats the difference in them?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78548
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy Columns

Post by HansV »

They are just two slightly different ways to accomplish the same goal. Just use whichever one you like.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Copy Columns

Post by adam »

Thanks for the interpretation Hans. I do really appreciate it.
Best Regards,
Adam