Optimizing VBA macro

hejredersejre

New Member
Joined
Aug 5, 2013
Messages
7
Dear Excel experts :)

I have made a macro that sorts my data in one sheet based on a value in a second sheet and then deletes the rows that doesn't contain the given value.

I have tried to enable the macro to consider more than one value when sorting but to no success :confused:

The macro takes quite a while to run. Is there any way the following code could be improved and hereby shortening the time it takes to run??

thank you in advance.

Code:
Sub Button_13()
    
    Sheets("Styring").Select
    
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim X As Long
    Dim Y As Long
    Dim Z As Long
    Dim V As Long
        
    X = Range("I43").Value
    Y = Range("I45").Value
    Z = Range("I47").Value
    V = Range("I49").Value
    

    Sheets("Data - Level 1").Select
    
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    With ActiveSheet

        .DisplayPageBreaks = False

        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        For Lrow = Lastrow To Firstrow Step -1

            
            With .Cells(Lrow, "A")

                If Not IsError(.Value) Then

                    If .Value <> (X) Then .EntireRow.Delete
                    If .Value <> (Y) Then .EntireRow.Delete
                    If .Value <> (Z) Then .EntireRow.Delete
                    If .Value <> (V) Then .EntireRow.Delete

                End If

            End With

        Next Lrow

    End With

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

End Sub
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
1) first problem I can see here:
Code:
If .Value <> (X) Then .EntireRow.Delete 'so it deletes also Y,V,Z !!! 
                    If .Value <> (Y) Then .EntireRow.Delete 'same - here is X deleted
'...
proper way:
Code:
If .Value <> (X) and .Value <> (Y) and .Value <> (Z) and .Value <> (V) Then .EntireRow.Delete
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,447
Latest member
M V Arun

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