Speed up Copy/Paste with variable range size

willywonka11

New Member
Joined
Nov 6, 2017
Messages
2
I need to speed up my code. This code is looped through hundreds of times and is super slow. The problem is that the range of cells I need to copy and paste changes.

Here is my original code:
Worksheets(Sht2).Activate
Worksheets(Sht2).Range(Cells(13, 1), Cells(LastPt, 15)).Copy Worksheets(ShtName).Activate
Worksheets(ShtName).Range(Cells(13, 1), Cells(13, 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Here is the code that I can't get to work:
Worksheets(ShtName).Range(Cells(13, 1), Cells(LastPt, 15)).Value = Worksheets(Sht1).Range(Cells(13, 1), Cells(LastPt, 15)).Value

What is frustrating is that I have been able to get this code to work but I need to be able to change the number of rows being copied.:
Worksheets(ShtName).Range(Cells(1, 1), Cells(60, 15)).Value = Worksheets(Sht1).Range("A1:O60").Value
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Unless you particularly like the flicker and flash, you don't need to use the 'Activate' or 'Select' syntax. You can just use direct coding.
Code:
Sheets(Sht2).Range("A13:O" & Sheets(Sht2).UsedRange.Rows.Count).Copy
Sheets(ShtName).Range("A13").PasteSpecial xlPasteValues
You are most likely having difficulty because when you use the Cells property to define your range, it has to be qualified for each use like:
Code:
Worksheets(ShtName).Range(Worksheet(ShtName).Cells(1, 1), Worksheet(ShtName).Cells(60, 15)).Value =
Or a shorter version using With statement
Code:
With Worksheets(ShtName)
 .Range(.Cells(1, 1), .Cells(60, 15) = Sheets(Sht1).Range("A1:O60").Value
End With
Where the periods tie the Range and Cells to the parent worksheet
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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