Very slow code....

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hello

Is there anyone who could tell my why this code is very slow? I copy columns from one book to another:

Code:
Sub KopierProdukter()

    Windows("Produktliste.xlsm").Activate
    Sheets("Produktliste").Select
    Range("C:C, L:L, U:U, AD:AD, AM:AM, AV:AV, BE:BE, BN:BN").Select
    Selection.Copy
    
    Windows("Prisliste.xlsm").Activate
    Sheets("Sheet2").Select
    Range("C1").Select
    ActiveSheet.Paste
        
    Workbooks("Prisliste.xlsm").Activate
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    
End Sub

Regards
Espen
 
You should run the macro line by line and see exactly which instruction(s) take(s) so long to execute.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
GO into Developer tab, click on Macros icon, select the macro and then click on Step into. A Yellow Line should appear highlighting the procedure.
Then press F8 and the first line of code should be executed. Keep pressing F8 to move line by line. Once you have detected the culprit lines pressing F5 will execute the rest of the lines in one go.
 
Upvote 0
Espen

If it is to do with 'jumping' between workbooks, don't do it.

You don't need that for what you want to do.

In fact in the KopierProdukter you aren't jumping between workbooks, you've used full workbook/worksheet references.
Code:
Sub copysheet()
Dim wbPris As Workbook
Dim wbProdukt As Workbook
Dim DesktopOpen8 As String
 
    Application.DisplayAlerts = False

    Application.Calculation = xlCalculationManual
 
    DesktopOpen8 = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
 
    Set wbPris = Workbooks.Open(DesktopOpen8 & "\SalesTools\Prisliste.xlsm")
 
    Set wbProdukt = Workbooks("Produktliste.xlsm")    ' use ThisWorkbook if the code is in the 'Produktliste' workbook
 
    wbPris.Sheets("Sheet1").Columns("C:P").ClearContents
 
    wbProdukt.Sheets("Priser").Sheets("Priser").Columns("C:C").Copy
    wbPris.Sheets("Sheet1").Columns("C1").PasteSpecial xlPasteValues
 
    wbProdukt.Sheets("Priser").Columns("E:E").Copy
    wbPris.Sheets("Sheet1").Columns("E1").PasteSpecial xlPasteValues
 
    wbProdukt.Sheets("Priser").Columns("N:P").Copy
    wbPris.Sheets("Sheet1").Columns("N1").PasteSpecial xlPasteValues
 
    Call KopierProdukter
 
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
 
End Sub
I think you should also try not to copy entire columns.

Is the last row in each column the same, or is there a specific column you could use to find the last row of data?

If there is it could be used to only copy the relevant no of rows.
 
Upvote 0
Hi Norie

Thank you for answering me.

As for kopierprodukter the columns do have different sizes, or length, but the first one C:C will always be the longest one. So I could use the column C:C for a standard number of rows to copy.

Regards
Espen
 
Upvote 0
Hi Albatros

Thank you for the information, I will go through it now and see if I can spot the problem.

Regards
Espen
 
Upvote 0
Ok, that was quick. It seems like the most time-eating issue is when I open the other workbook.

It takes quite some time to open it. Even thought it only have a small ammount of data inside, and no codes or formulas.

I fell like my excel has become slower when opening and saving books.

This is where it stopped and took 10-15 seconds:


Code:
Sub copysheet()

    Dim DesktopOpen8 As String
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    DesktopOpen8 = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    Workbooks.Open DesktopOpen8 & "\SalesTools\Prisliste.xlsm"
    
    [B][SIZE="4"]Windows("Prisliste.xlsm").Activate[/SIZE][/B]
    Sheets("Sheet1").Columns("C:P").ClearContents

    Windows("Produktliste.xlsm").Activate
    Sheets("Priser").Columns("C:C").Copy
    
    Windows("Prisliste.xlsm").Activate
    Sheets("Sheet1").Columns("C:C").PasteSpecial xlPasteValues

    
    Windows("Produktliste.xlsm").Activate
    Sheets("Priser").Columns("E:E").Copy
    
    Windows("Prisliste.xlsm").Activate
    Sheets("Sheet1").Columns("E:E").PasteSpecial xlPasteValues

    
    Windows("Produktliste.xlsm").Activate
    Sheets("Priser").Columns("N:P").Copy
    
    Windows("Prisliste.xlsm").Activate
    Sheets("Sheet1").Columns("N:P").PasteSpecial xlPasteValues
    Range("F2").Select
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    
    Call KopierProdukter

End Sub
 
Upvote 0
If you didn't use Activate/Select etc this code would be quicker.

Try just using the last code I posted in place of what you've got.

The code I posted does the same without using Activate/Select.
 
Upvote 0
Ok, I tried that code now, it's much more clean and tidy. But the slowdown project seems to be related to opening the book.

I have written many codes latly, and it seems to me that excel has become slower and slower. Could it be that I have opened up librarys under Tools in Developer?

Regards
Espen
 
Upvote 0
Espen

I've never heard of anything like that.

Having a lot of references/add-ins could perhaps slow down opening Excel itself but it shouldn't impact on opening workbooks.

What happens when you just open the workbook(s) with a double click or whatever?
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,930
Members
449,094
Latest member
teemeren

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