Amy has attended:
Excel VBA Intro Intermediate course
VBA Copy and paste not working, please help!
I have a table within the worksheet "OPA 1", with a table of data. I want to copy some of these columns across into a seperate worksheet ("All") to make a new table. I have created a header row with a 1 above all the columns I want to copy across to create the new table.
The coding I've used so far seems to work all the way to the 3rd to last line "Cells(1, columncount2 + 1).Paste
" but it wont paste!
I really don't know what I'm doing wrong...
Any help would be very much appreciated!
Sub Sort()
Sheets("OPA 1").Select
Cells(1, 1).Select
Dim i As Integer
Dim columncount1 As Integer
columncount1 = Cells.CurrentRegion.Columns.Count
Dim columncount2 As Integer
For i = 1 To columncount1
Cells(1, i).Select
If ActiveCell = 1 Then
ActiveSheet.Range(Cells(2, i), Cells(751, i)).Select
Selection.Copy
Sheets("All").Select
Cells(1, 1).Select
columncount2 = Cells.CurrentRegion.Columns.Count
Cells(1, columncount2 + 1).Select
Sheets("OPA 1").Select
ActiveSheet.Range(Cells(2, i), Cells(751, i)).Select
Sheets("All").Select
Cells(1, columncount2 + 1).Paste
End If
Next i
RE: VBA Copy and paste not working, please help!
Hi Amy
Thanks for getting in touch. The issue here is that the Cells object cannot take a Paste method.
You can instead replace the problem line with:
Cells(1, columncount2 + 1).Select
ActiveSheet.Paste
I hope this helps.
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: VBA Copy and paste not working, please help!
Gary it's great to hear from you. Thank you again for all your help during the course, this stuff is coming in very useful.
Brilliant! It's working! Thank you!
I'm sure I'll be on here a lot in the coming weeks...
RE: VBA Copy and paste not working, please help!
Hi Amy
Happy to help! If you fancy setting yourself a task there are some optimisations you could make to this routine. As I said before, start with something that works and gradually iterate a better solution (when time allows!).
Remember the Cells process we walked through, where we copied "cells above 800 move to their own sheet" ? I'm confident a solution like that could be incorporated here. If you manage to work through this solution I'm confident you will see significant performance gains, especially in the case of large data sets.
Best of luck!
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: VBA Copy and paste not working, please help!
Thanks Gary, that's Brill!
I will give it a go and see how I get on. Thanks for the tips.
RE: VBA Copy and paste not working, please help!
Hi Amy
One last tip to solve the initial problem you had: you'd correctly identified the problem line. To figure out what was wrong, go into the Immediate Window (from the View menu) and type your line out manually. You'll notice .Paste is not an option from the Intellisense drop-down which should give you a clue.
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector