Copy complete range with 1 line of code using currentRegion

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hello VBA pros,

I want to be able to copy a range on sheet2 using .currentRegion but exclude the heading by using OFFSET and then .end(xlup) to un select the blank row at the bottom.

I have a feeling this is possible, below is the line of code that excludes the heading row, but includes the blank row due to the OFFSET.

Code:
Sheet2.Range("A1").CurrentRegion.Offset(1, 0).Copy

and here is the code that is not working, that is meant to exclude the blank row:

Code:
Sheet2.Range("A1").CurrentRegion.Offset(1, 0) , Sheet2.Range("A1").CurrentRegion.End(xlUp)).copy


Below is the Code that pastes the data to an existing database (no challenges with this code just an fyi):
Code:
Sheet1.Range("A" & WorksheetFunction.CountA(Sheet1.Columns("B:B")) + 1).PasteSpecial xlPasteValuesAndNumberFormats
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Perhaps (untested)

Code:
Sheet2.Range("A1").CurrentRegion.Offset(1).Resize(Sheet2.Range("A1").CurrentRegion.Rows.Count - 1).Copy
 
Upvote 0
Hi

Try, similar to Peter's:

Code:
With Sheet1.Range("A1").CurrentRegion
    .Resize(.Rows.Count - 1).Offset(1, 0).Copy
End With
 
Upvote 0
Hi there VoG,

Yup that works really well, thanks.

Hi pcg01,

That works as well, thank you.



Just out of curiosity, :) is there away to make the 2 mines of code into 1 line?

Code:
Sub test()
Sheet2.Range("A1").CurrentRegion.Offset(1).Resize(Range("A1").CurrentRegion.Rows.Count - 1).Copy
Sheet1.Range("A" & WorksheetFunction.CountA(Sheet1.Columns("B:B")) + 1).PasteSpecial xlPasteValuesAndNumberFormats
End Sub

thanks again you 2 are always very helpfull.
 
Upvote 0
With PasteSpecial you need two lines of code - one for the copy and one for the paste.
 
Upvote 0
Hi Mark

You have an error in the copy statement. You forgot the sheet reference:

Code:
Sheet2.Range("A1").CurrentRegion.Offset(1).Resize([COLOR=red]Sheet2.[/COLOR]Range("A1").CurrentRegion.Rows.Count - 1).Copy
 
Upvote 0
Hi Mark

You have an error in the copy statement. You forgot the sheet reference:

Code:
Sheet2.Range("A1").CurrentRegion.Offset(1).Resize([COLOR=red]Sheet2.[/COLOR]Range("A1").CurrentRegion.Rows.Count - 1).Copy

That's my fault. I edited my post to correct that error.
 
Upvote 0
Combining these two will mean VBA won't do Paste Special but Paste. In the second row of your code if you intend to find the last row where data is filled then following code is better equipped:
Code:
Sheet1.Range("A"[I][COLOR=blue] & Rows.Count).End(xlUp)(2).[/COLOR][/I]PasteSpecial xlPasteValuesAndNumberFormats
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

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