Copying Ranges from One Worksheet to another

steveh8204

Board Regular
Joined
Aug 20, 2018
Messages
151
I read on a website tutorial that a more efficient way of copying and pasting ranges was to use the following:

Code:
' This is faster
Range("A1:A10").Value = Range("B1:B10").Value

' This is slower
Range("B1:B1").Copy Destination:=Range("A1:A10")

I was led to believe this way made the code run significantly quicker, the problem I've got though is that I'm copying from a different spreadsheet so I was wondering whether this is still possible to do?

The code I am using to copy and paste is:

Code:
    Worksheets("SAP").Activate
    Dim wb As Workbook
    Dim myfilename As String
    myfilename = "P:\PFMSHARE\LOGISTICS\STOCK CHECK DOCUMENTS & BBE CHECKER\Macs Auto Stock Checker\Macs All.MHTML"
    Set wb = Workbooks.Open(myfilename)
    a = Range("A2:d200").Copy
    Workbooks("Macs Auto Stock Check.xlsm").Activate
    Range("a3").Select
    paste = a
     Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A3").Select
    Sheets(2).Activate

With the Macs All.MHTML file being another spreadsheet to the one currently open.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try with this:

Code:
    Dim wb As Workbook
    Dim myfilename As String
    Dim ws1 As Worksheet, ws2 As Worksheet
    
    Application.ScreenUpdating = False
    Set ws1 = Sheets("SAP")
    myfilename = "P:\PFMSHARE\LOGISTICS\STOCK CHECK DOCUMENTS & BBE CHECKER\Macs Auto Stock Checker\Macs All.MHTML"
    
    Set wb = Workbooks.Open(myfilename)
    Set ws2 = wb.Sheets(1)
    ws1.Range("A3:D201").Value = ws2.Range("A2:D201").Value
        
    wb.Close False
 
Upvote 0
Try with this:

Code:
    Dim wb As Workbook
    Dim myfilename As String
    Dim ws1 As Worksheet, ws2 As Worksheet
    
    Application.ScreenUpdating = False
    Set ws1 = Sheets("SAP")
    myfilename = "P:\PFMSHARE\LOGISTICS\STOCK CHECK DOCUMENTS & BBE CHECKER\Macs Auto Stock Checker\Macs All.MHTML"
    
    Set wb = Workbooks.Open(myfilename)
    Set ws2 = wb.Sheets(1)
    ws1.Range("A3:D201").Value = ws2.Range("A2:D201").Value
        
    wb.Close False

Nice one, runs a lot faster now. Had to get my head around it a bit to understand what to change and which code to still keep in but worth it. And I've learnt a bit more.

Thanks for your help.
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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