Reducing code for copy/cut paste

nunuc

New Member
Joined
Jul 4, 2019
Messages
3
Hi everyone,

first up - apologies for the very beginner level question, I have never learnt VBA and don't use it much, so I typically just play around until I get it to do what I want. No hate!

I am looking to cut down my code a little on 4 lines that I use quite a lot, which is basically just a copy or cut and paste. Here is an example:

Code:
Range("C2").Select    
Range(Selection, Selection.End(xlDown)).Copy
Range("T2").Select
ActiveSheet.Paste

Basically selecting an entire column (except the header) and pasting it to another area. What is an easier and cleaner way to accomplish this without having 4 lines of code, every time I do that? My current Macro does it about 8 times, so lots of unnecessary code.

I've played around with it a little and Google'd a bit, but none of that worked for me.

Thanks!
nunuc
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Code:
Range([C2], [C2].End(xlDown)).Copy [T2]

What code are you using for the other 8 times?
 
Upvote 0
Thanks, worked a charm! Lots to learn :)

The other code looks very similar, can easily use that same method for those, eg.

Code:
Range("P2").SelectRange(Selection, Selection.End(xlDown)).Copy
Range("S2").Select
ActiveSheet.Paste

I just used that over and over.

Thank you kindly sir.
 
Upvote 0
If you post all the cell refs to be copied and the corresponding destination cells, might be able to avoid repeating the code.
 
Upvote 0
If you post all the cell refs to be copied and the corresponding destination cells, might be able to avoid repeating the code.

So for the two examples you posted, could do like this :
Code:
Dim s, d, i%
s = Array(3, 16)  'Source column numbers(for C and P)
d = Array(20, 19) 'Destination column numbers(for T and S)
For i = 0 To UBound(s)
    Range(Cells(2, s(i)), Cells(2, s(i)).End(xlDown)).Copy Cells(2, d(i))
Next
 
Upvote 0
So for the two examples you posted, could do like this :
Code:
Dim s, d, i%
s = Array(3, 16)  'Source column numbers(for C and P)
d = Array(20, 19) 'Destination column numbers(for T and S)
For i = 0 To UBound(s)
    Range(Cells(2, s(i)), Cells(2, s(i)).End(xlDown)).Copy Cells(2, d(i))
Next


Thank you, I will give that a go this afternoon and let you know if I get stuck.
 
Upvote 0

Forum statistics

Threads
1,213,485
Messages
6,113,931
Members
448,533
Latest member
thietbibeboiwasaco

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