Copy-paste from partly unknown range VBA

dotsent

Board Regular
Joined
Feb 28, 2016
Messages
89
Office Version
  1. 365
Platform
  1. Windows
Hi!

Sorry for the vague subject. I have a seemingly simple task at hand, however lacking ideas towards solution. Can't even figure out great keywords for search.

I need to copy some data from Sheet2 to Sheet1 via VBA. There are 10 columns of data on Sheet2 (range A:J - can copy them all). However, the actual row count is unknown (varies). Still, I know data is consistent, so first empty row in Sheet2 column A means the end of area to be copied.

How can I copy an area where the row count is yet to be identified (although column count is known)?

Ideally I'd like to copy starting from Sheet2 cell A2 and paste to Sheet1 cell A2, as both sheets have headers, but overwriting them is not a big deal. Still, Copy-Paste just values should be more important, but I don't expect trouble handling this.

I would prefer NOT to hardcode the range which would likely cover all the filled area to the like of:

Code:
Worksheets("Sheet2").Range("A2:J5000").Copy

Thanks a bunch!
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Phew, here's perhaps a partial answer. I guess I have to identify the final row and somehow integrate that value in the Range selection? So this should give me the final row number to be copied:


Code:
Sub Last_row()


Dim sht As Worksheet
Dim LastRow As Long


Set sht = ActiveSheet
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row


End Sub

However how to use LastRow variable next to column J instead of 5000?

Code:
Worksheets("Sheet2").Range("A2:J5000").Copy
 
Last edited:
Upvote 0
How about
Code:
Sub dotsent()
   With Sheets("sheet1")
      .UsedRange.ClearContents
      Sheets("sheet2").Range("A1").CurrentRegion.Copy
      .Range("A1").PasteSpecial xlPasteValues
   End With
   Application.CutCopyMode = False
End Sub
 
Upvote 0
Thanks, that does the basic job quite well! Didn't know the .UsedRange and .CurrentRegion methods, but we are here to learn! I don't really need (nor want) to first clean the entire Sheet1 as there is some necessary data there and the area where I'm pasting in Sheet1 will be empty anyway.

However does the CurrentRegion.Copy function provide any flexibility in ignoring the 1st row, so I could not copy the header from Sheet2 (and I could then run the Paste function in A2, leaving the existing header intact)?
 
Last edited:
Upvote 0
How about
Code:
Sub dotsent()
   Sheets("sheet2").Range("A1").CurrentRegion.Offset(1).Copy
   Sheets("sheet1").Range("A2").PasteSpecial xlPasteValues
   Application.CutCopyMode = False
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,748
Messages
6,180,721
Members
452,995
Latest member
isldboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top