Macro to copy range of cells to the next free column in another worksheet

TBW_MK

New Member
Joined
Sep 27, 2011
Messages
11
I have the following macro which is taking data from one range of cells & copying it to the next free column in another worksheet (starting from row 14).

Sub CopyColumnBandPasteToFirstFreeColumnSheet2()
Dim PasteToCol As Long

PasteToCol = Sheet5.Cells(14, Columns.Count).End(xlToLeft).Column + 1
Sheet1.Range("F14:F83").Copy Sheet5.Cells(14, PasteToCol)

End Sub

It works well.

However I need it to Paste the Value rather than the formula - I've tried to add the .PasteSpecial Paste:=xlPasteValues part to both line, but have no luck.

How can I get it to work / where am I going wrong?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hmmm... that creates a Ruin time error '13' - Type mismatch error message

So I now have...

Sub CopyColumnBandPasteToFirstFreeColumnSheet2()
Dim PasteToCol As Long, wb As Workbook
Set wb = Workbooks.Open("S:\Helpdesks\MMS Admin\HELPDESK\Call monitoring\Agents\" & Range("B4").Value & "\Performance-Overall.xlsx")
PasteToCol = Range("F5").Value + 3
ThisWorkbook.Sheets(1).Range("F3:F83").Copy
wb.Sheets(1).Cells(2, PasteToCol).PasteSpecial Paste:=xlPasteValues
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub

I've tried various combinations of the amendment with the previous code for the line starting PasteToCol, but all seem to have the same error...

Do I need this line of code or can it go in the line starting wb.Sheets(1)?

Thanks.
 
Upvote 0
Perhaps it should be

Code:
PasteToCol = ThisWorkbook.Sheets(1).Range("F5").Value + 3

or at least you need to qualify the workbook and sheet that you want to grab the value from.
 
Upvote 0
Thanks VoG for your ongoing help here...

I'm now getting a runtime error 9: subscript out of range message & a highlight on the final line of code...
wb.Sheets(5).Cells(2, PasteToCol).PasteSpecial Paste=:xlPasteValues

I also can't revert to the previous code without an error... why would that be?
 
Upvote 0
That error basically means that something doesn't exist. Do you still have a sheet 5?
 
Upvote 0
I think so...

The 'destination' file opens - it only has the one sheet - Sheet5.

In the VBAProject pane it is listed as: Sheet12 (Sheet5). I've tried listing both, but get the same result...
 
Upvote 0
If there is only one sheet, its index number is 1. Try

Code:
wb.Sheets(1).Cells(2, PasteToCol).PasteSpecial Paste:=xlPasteValues
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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