Macro is Slow!!!

jonjones069

New Member
Joined
Jun 2, 2014
Messages
5
Hey guys, Im relatively new to VBA coding and was wondering if there was a quicker way to make the below coding run faster?? WithActiveSheet.Range("B:B")
.Value = .Value
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With Cheers!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hey guys, Im relatively new to VBA coding and was wondering if there was a quicker way to make the below coding run faster?? WithActiveSheet.Range("B:B")
.Value = .Value
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With Cheers!

Hi Jon,

Welcome to the board. This is the fastest way I have ever found to delete rows in a column is blank (its one of ron de bruns codes :-) )

Code:
Sub Delete_Row_If_Value_Isnt()
    Dim Firstrow As Long
    Dim LastRow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With Sheets("Imported Strikes")

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(1).Row
        LastRow = .UsedRange.Rows(.UsedRange.Rows.count).Row

        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = LastRow To Firstrow Step -1

            'We check the values in the A column in this example
            With .Cells(Lrow, "A")

                If Not IsError(.Value) Then

                    If .Value = "" Then .EntireRow.Delete
                    'This will delete each row with if its blank
                    'in Column A

                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub
 
Upvote 0
jonjones069,

Welcome to the MrExcel forum.

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub MakeCodeRunFaster()
' hiker95, 06/02/2014, ME781703
Dim lr As Long
Application.ScreenUpdating = False
With ActiveSheet
  lr = .Cells(Rows.Count, "B").End(xlUp).Row
  With .Range("B2:B" & lr)
    .Value = .Value
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  End With
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the MakeCodeRunFaster macro.
 
Upvote 0
Maybe this
Code:
Sub MM1()
With Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    .Value = .Value
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub
 
Upvote 0
Thank you guys this has been most useful! Michael M - Yours fit for what I needed so cheers! Shall be using this site again for any future needs!
 
Upvote 0
jonjones069

Thanks for the feedback.

You are very welcome. Glad we could help.

And, come back anytime.
 
Upvote 0
If you can sort on column B first - then all blanks are grouped together & the deletion is of only a single block - that should really help with execution time.

Does that suit your situation?
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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