Pasting certain columns

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
Hi

I'm trying to alter the below code but I'm stuck.
On the penultimate line where it pastes all to wb2, how could I change the code so that it only pastes columns B to K. I'm guessing I would need another line just before it to copy only those columns?

VBA Code:
lrw = ws.Range("A" & Rows.Count).End(xlUp).Row
lrw2 = ws.Range("B" & Rows.Count).End(xlUp).Row


If lrw > 2 Then


lc = ws.Cells(1, Columns.Count).End(xlToLeft).Column
lr = wb.Range("A" & Rows.Count).End(xlUp).Row + 1
lr2 = wb2.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Rows("3:" & lrw).Copy
wb.Range("A" & lr).PasteSpecial xlPasteAll
wb2.Range("A" & lr2).PasteSpecial xlPasteAll
ws.Range(ws.Cells(3, 1), ws.Cells(lrw, lc)).EntireRow.ClearContents


End If
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,025
Office Version
  1. 365
Platform
  1. Windows
If I am understanding you correctly, try changing this part:
VBA Code:
ws.Rows("3:" & lrw).Copy
wb.Range("A" & lr).PasteSpecial xlPasteAll
wb2.Range("A" & lr2).PasteSpecial xlPasteAll
to:
VBA Code:
ws.Range("B3:K" & lrw).Copy
wb.Range("B" & lr).PasteSpecial xlPasteAll
wb2.Range("B" & lr2).PasteSpecial xlPasteAll
 
Solution

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
A little tweak to make it apply to just wb2 and it's spot on.

Thanks, Joe
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,025
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
 

Forum statistics

Threads
1,141,415
Messages
5,706,308
Members
421,441
Latest member
VapesRub

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
Top