Solved

Excel copy selected data

Posted on 2013-06-06
3
327 Views
Last Modified: 2013-06-06
I am trying to copy data from Sheet1 to Sheet2. Can anyone help me to do that.

I have tried these.
Worksheets("Sheet2").Range("A1:S1000").Value = Worksheets("Sheet1").Range("A1:S1000").Value

This copies A1 to S1000 data. I want to set these value dynamically to copy only the cells having data. My data starts from A1.


With this, I am able to copy the header. How can I expand this to copy all rows also.

With Worksheets("Sheet1").Range("A1")
  .Range(.Cells(1), .End(xlToRight)).Copy Destination:= Worksheets("Sheet2").Range("A1")
End With

Thanks in advace.
Sharath
0
Comment
Question by:Sharath
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
Michael earned 250 total points
ID: 39227480
Hi Sharath,

you can use
With Worksheets("Sheet1")
  .Range("A1").Currentregion.Copy Destination:= Worksheets("Sheet2").Range("A1")
End With

Open in new window

However, this assumes there are no empty rows or empty columns in your dataset

You can also use:
to determine the last used row in a sheet:
.Cells(.Rows.Count, "A").End(xlUp).Row

Open in new window

and determine the last column:
.Cells(1, .Columns.Count).End(xlToLeft).Column

Open in new window


So, the determine the dataset try this:
With Worksheets("Sheet1")
  .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column)).Copy Destination:= Worksheets("Sheet2").Range("A1")
End With

Open in new window

0
 
LVL 40

Author Comment

by:Sharath
ID: 39227577
Thanks JazzyJoop. I tried your first suggestion and it is working. Will check the 2nd one also and close the question. Thanks again.
0
 
LVL 40

Author Comment

by:Sharath
ID: 39227619
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now