Question About Paste and insert?

benpatz

New Member
Joined
May 21, 2015
Messages
9
I have two sets of code, The first one, copies and inserts the copied range on to a different sheet and the Second, copies and pastes the range on to a different sheet. There is one line of code that has to be different for me. I put the lines in question in bold. Why is this? Does it have to do something with VBA objects? I am fairly new and was wondering why such a small change makes programming seem more frustrating than it often appears. Is there any easier way write either of these codes?

Sub CopyandInsert()
'This code copies and inserts via shift cells down.
ThisWorkbook.Worksheets("Sheet1").Select
ActiveSheet.Range("A2:D5").Select
Selection.Copy
ThisWorkbook.Worksheets("Sheet2").Select
ActiveSheet.Range("A12:D15").Select
Selection.Insert Shift:=xlDown


Columns("D:D").NumberFormat = "$0.00"
End Sub

Sub CandP()
'This code copies and pastes
ThisWorkbook.Worksheets("Sheet1").Select
ActiveSheet.Range("A2:D4").Select
Selection.Copy
ThisWorkbook.Worksheets("Sheet2").Select
ActiveSheet.Range("A4:D6").Select
ActiveSheet.Paste

Columns("D:D").NumberFormat = "$0.00"
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
firstly i think you can shorten some of your code so instead of

Code:
ThisWorkbook.Worksheets("Sheet1").Select
ActiveSheet.Range("A2:D4").Select
Selection.Copy
you can just have

Code:
ThisWorkbook.Worksheets("Sheet1").ActiveSheet.Range("A2:D4").Copy
but i duno maybe you just prefer your way

as for your other question, i dont know much about VBA yet and dont understand objects and such too well, but i think it's just how paste and insert work differently. when you copy a cell and right click paste it just does it but when you right click insert a copied cell it has to ask you whether you want to do it down or right so you need that extra Shift:=xlDown bit in the code

hope that helped. sorry if it didnt
 
Upvote 0

Forum statistics

Threads
1,223,959
Messages
6,175,645
Members
452,663
Latest member
MEMEH

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