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
 
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 #?
I also need to incorporate below code as it deletes blank rows in source data

VBA Code:
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
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I also need to incorporate below code as it deletes blank rows in source data

This line of code deletes the blank rows in the Summary:
VBA Code:
Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
Upvote 0
Just make sure that columns A and E have a header in A3 and E3. It can be anything.
The issue is there are blank rows in source data that needs to be removed before pasting. I think that's what's causing the row misalignment.
 
Upvote 0
This line of code deletes the blank rows in the Summary:
VBA Code:
Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Thank you it works however it copies column e starting row 1 and column a row 2. There is misalignment where the data starts and ends Please see attached screenshot.
 

Attachments

  • Test.png
    Test.png
    50.4 KB · Views: 7
Upvote 0
Did you put headers in A3 and E3?
 
Upvote 0
Replace this line of code:
VBA Code:
Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
with this one:
VBA Code:
Range("E4", Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
Upvote 0
Replace this line of code:
VBA Code:
Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
with this one:
VBA Code:
Range("E4", Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Thank you .Just saw your message it works flawlessly!
 
Upvote 0
Replace this line of code:
VBA Code:
Columns("E:E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
with this one:
VBA Code:
Range("E4", Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
How can I replicate same process and also copy paste AG25:AG60 from every worksheet. and copy paste O18 to column D and copy paste AG21 to column B.
 
Upvote 0
Into which column do you want to paste AG25:AG60? This line of code already copies O18 into column D:
VBA Code:
.Cells(.Rows.Count, "D").End(xlUp).Offset(1).Resize(62).Value = ws.Range("O18")
 
Upvote 0

Forum statistics

Threads
1,216,216
Messages
6,129,566
Members
449,517
Latest member
Lsmich

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