Inefficient code taking too long to run

Dokat

Active Member
Joined
Jan 19, 2015
Messages
304
Office Version
  1. 365
Hi,

I have 18 macro codes similar to the one below with different cell ranges . I created a macro that runs all of them in sequence. However it's taking too long to run and very inefficient. Is there a way to speed the run time on this code.

VBA Code:
Sub MoveRangeWBC()
Worksheets("WBC").Range("E25:E60").Copy Destination:=Worksheets("Summary").Range("E4")
Worksheets("WBC").Range("W25:W60").Copy Destination:=Worksheets("Summary").Range("F4")
Worksheets("WBC").Range("E25:E60").Copy Destination:=Worksheets("Summary").Range("E40")
Worksheets("WBC").Range("AB25:AB60").Copy Destination:=Worksheets("Summary").Range("F40")

Dim iCntr
Dim rng As Range
Set rng = Worksheets("Summary").Range("A4:F1111")

For iCntr = rng.Row + rng.Rows.Count - 1 To rng.Row Step -1

If Application.WorksheetFunction.CountA(Rows(iCntr)) = 0 Then Rows(iCntr).EntireRow.Delete

Next
With Worksheets("WBC")
.Range("W22").Copy Destination:=Worksheets("Summary").Range("A4:A34")
.Range("AB22").Copy Destination:=Worksheets("Summary").Range("A35:A65")
.Range("O18").Copy Destination:=Worksheets("Summary").Range("D4:D65")


End With

End Sub
 
Is there any reason why you want to start pasting data in row 4 in the Summary sheet?
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I'm sure it will be mentioned but have you considered starting with
VBA Code:
Application.Screenupdating = False
and ending with
VBA Code:
Application.Screenupdating = True
It still takes quite a bit to run but still tremendously shorten the run time. It took only 10 mins to finish runing.
 
Upvote 0
Is there any reason why you want to start pasting data in row 4 in the Summary sheet?
Yes that's where my header is, but if i need to change is it is not mission critical. However it also means i have to change cell ranges. for all 18 macros.
 
Upvote 0
Are you headers in row 3 or row 4?
 
Upvote 0
Try:
VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, ws As Worksheet
    Set desWS = Sheets("Summary")
    For Each ws In Sheets
        If ws.Name <> "ST" And ws.Name <> "Summary" Then
            With desWS
                ws.Range("E25:E60").Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1)
                ws.Range("W25:W60").Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1)
                ws.Range("E25:E60").Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1)
                ws.Range("AB25:AB60").Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1)
                Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(31).Value = ws.Range("W22")
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(31).Value = ws.Range("AB22")
                .Cells(.Rows.Count, "D").End(xlUp).Offset(1).Resize(62).Value = ws.Range("O18")
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, ws As Worksheet
    Set desWS = Sheets("Summary")
    For Each ws In Sheets
        If ws.Name <> "ST" And ws.Name <> "Summary" Then
            With desWS
                ws.Range("E25:E60").Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1)
                ws.Range("W25:W60").Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1)
                ws.Range("E25:E60").Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1)
                ws.Range("AB25:AB60").Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1)
                Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(31).Value = ws.Range("W22")
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(31).Value = ws.Range("AB22")
                .Cells(.Rows.Count, "D").End(xlUp).Offset(1).Resize(62).Value = ws.Range("O18")
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub

It works alot faster. Is there a way to combine all 18 macros together?
 
Upvote 0
Try:
VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, ws As Worksheet
    Set desWS = Sheets("Summary")
    For Each ws In Sheets
        If ws.Name <> "ST" And ws.Name <> "Summary" Then
            With desWS
                ws.Range("E25:E60").Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1)
                ws.Range("W25:W60").Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1)
                ws.Range("E25:E60").Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1)
                ws.Range("AB25:AB60").Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1)
                Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(31).Value = ws.Range("W22")
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(31).Value = ws.Range("AB22")
                .Cells(.Rows.Count, "D").End(xlUp).Offset(1).Resize(62).Value = ws.Range("O18")
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
I just realized it copy pasted all values however column E starts from row #1 and column A from row #2 can you both start from same row #?
 
Upvote 0
Just make sure that columns A and E have a header in A3 and E3. It can be anything.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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