Please Help Me Speed Up My Code

Alfred12

New Member
Joined
Feb 28, 2011
Messages
15
Hello all --

I hadn't even heard of VBA two weeks ago, but with much struggle, and some help from the kind folks here (thanks!), I've got my code working exactly the way I want. :biggrin:

But it is really sloooow! I assume it is the copying and pasting that is the culprit, but perhaps it is something (or somethings) else. I am sure there is a more elegant way to write this that might speed it up some. Any thoughts would be greatly appreciated. Thanks!

Alfred

Code:
Sub Compile()
    Dim S As Integer
    Dim z As Integer
    Dim i As Integer
    Worksheets(1).Activate
    Range("A2:J35").ClearContents
    For z = 1 To 5
        For S = 2 To Sheets.Count
            Worksheets(S).Activate
            For i = 1 To 25
                If Worksheets(S).Cells(i, 5).Value = z Then
                    If Worksheets(S).Cells(i, 6).Value = "yes" Then
                        Worksheets(1).Activate
                        Range("A500").End(xlUp).Offset(1, 0) = z
                        Worksheets(S).Cells(i, 1).Copy
                        Worksheets(1).Activate
                        Range("A500").End(xlUp).Offset(0, 1).Select
                        ActiveSheet.Paste
                        Worksheets(S).Cells(i, 7).Copy
                        Worksheets(1).Activate
                        Range("A500").End(xlUp).Offset(0, 2).Select
                        ActiveSheet.Paste
                        Worksheets(S).Cells(i, 8).Copy
                        Worksheets(1).Activate
                        Range("A500").End(xlUp).Offset(0, 3).Select
                        ActiveSheet.Paste
                        Worksheets(S).Cells(i, 9).Copy
                        Worksheets(1).Activate
                        Range("A500").End(xlUp).Offset(0, 4).Select
                        ActiveSheet.Paste
                    End If
                End If
            Next i
        Next S
        Worksheets(1).Activate
        Range("A100").End(xlUp).Offset(1, 0) = " "
    Next z
    Worksheets(1).Activate
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try without all that activating and selecting. For example

Code:
Worksheets(1).Activate
Range("A500").End(xlUp).Offset(1, 0) = Z
Worksheets(S).Cells(i, 1).Copy
Worksheets(1).Activate
Range("A500").End(xlUp).Offset(0, 1).Select
ActiveSheet.Paste

could be replaces with

Code:
Worksheets(1).Range("A500").End(xlUp).Offset(1, 0) = Z
Worksheets(S).Cells(i, 1).Copy Destination:=Worksheets(1).Range("A500").End(xlUp).Offset(0, 1)
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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