Can You Show Me How To Shorten This Code?

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,196
Office Version
  1. 2010
Platform
  1. Windows
I have this code I know can be shortened Can you show me where and how? Thanks

Code:
    Application.ScreenUpdating = False
    Sheets("YTD Summary DATA").Select
    Range("E3:E44").Copy Range("Q3")
    Range("E45").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("YTD Summary Charts").Select
    Range("E2").Select
    ActiveSheet.Paste
    Sheets("YTD Summary DATA").Select
    Range("S2").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("YTD Summary Charts").Select
    Range("J5").Select
    ActiveSheet.Paste
    Sheets("YTD Summary DATA").Select
    Range("H45").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("YTD Summary Charts").Select
    Range("T2").Select
    ActiveSheet.Paste
    Sheets("YTD Summary Charts").Select
    ActiveSheet.Deselect
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You do not have to select sheets and ranges in order to do most things (like assign values, copy /paste, etc.) with them Besides being faster, the following code, which if I read what you posted correctly should be equivalent, is also much easier to follow (at least in my opinion)...
Code:
Sub Test()
  With Sheets("YTD Summary DATA")
    .Range("E3:E44").Copy .Range("Q3")
    .Range("E45").Copy Sheets("YTD Summary Charts").Range("E2")
    .Range("S2").Copy Sheets("YTD Summary Charts").Range("J5")
    .Range("H45").Copy Sheets("YTD Summary Charts").Range("T2")
  End With
End Sub
 
Upvote 0
Code:
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("YTD Summary Data")
Set sh2 = Sheets("YTD Summary Charts")
Application.ScreenUpdating = False 
sh1.Range("E3:E44").Copy Range("Q3") 'Did you want to paste to the same sheet here? If not, insert sh2. in front of Range("Q3")
sh1.Range("E45").Copy sh2.Range("E2")
sh1.Range("S2").Copy sh2.Range("J5")
sh1.Range("H45").Copy sh2.Range("T2") 
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Both of these return REF# instead of the data, it also copies and pastes the background colors? The cell this copys from has formulas, does that make a differance. What can I change to make this copy paste better. Thanks
 
Upvote 0
Try this
Code:
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("YTD Summary Data")
Set sh2 = Sheets("YTD Summary Charts")
Application.ScreenUpdating = False 
sh1.Range("E3:E44").Copy 
Range("Q3").PasteSpecial xlPasteValues 'Did you want to paste to the same sheet here? If not, insert sh2. in front of Range("Q3")
sh2.Range("E2") = sh1.Range("E45").Value
sh2.Range("J5") = sh1.Range("S2").Value 
sh2.Range("T2") = sh1.Range("H45").Value
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

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