3
x.Worksheets("Sheet1").Activate Range("A65536").Select ActiveCell.End(xlUp).Select lastrow = ActiveCell.Row Range("A2:A" & lastrow).Copy y.Worksheets("Sheet1").Range("a65536").End(xlUp).Offset(1, 0) Range("B2:B" & lastrow).Copy y.Worksheets("Sheet1").Range("b65536").End(xlUp).Offset(1, 0) 

I have read several places that using select is not recommended for copy pasting, and how can one do it without select?

4
  • 3
    Range(this).Value = Range(that).Value Commented Mar 12, 2015 at 18:31
  • Or iterate through the cells if you have more to copy Commented Mar 12, 2015 at 18:32
  • Using Loops... for loop for example Commented Mar 12, 2015 at 18:32
  • Find is also superior to xlup etc which doesn't handle edge cases (empty columns, full columns, or hidden cells). See stackoverflow.com/questions/8283797/… Commented Mar 13, 2015 at 13:38

2 Answers 2

2

As an exact analog to your code:

lastRow = x.worksheets("Sheet1").range("A65536").end(xlup).row 

Added because of comment recommending better practices:

with x.worksheets("Sheet1") lastRow = .Cells(.Rows.Count, 1).End(xlup).Row end with 
Sign up to request clarification or add additional context in comments.

3 Comments

-1 for bad practice. What if we're using a 2007+ Version of Office and we have a spreadsheet with hundreds of thousands of rows? This would say row 65536 is the last row.
@RubberDuck, good point (1,048,576 to be exact). Added 2nd option.
Good show old bean. Duly noted and vote reversed.
2

You can set lastRow = x.worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1

You need the first empty cell in the column, no the last one occupied

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.