Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:09:35+00:00 2026-06-15T16:09:35+00:00

The following VBA code takes very long time to execute. I ran it 25

  • 0

The following VBA code takes very long time to execute. I ran it 25 minutes ago for 48,000 rows and it’s still running. How can I shorten the execution time?

Sub delrows()

Dim r, RowCount As Long
r = 2

ActiveSheet.Columns(1).Select
RowCount = UsedRange.Rows.Count
userresponse = MsgBox("You have " & RowCount & " rows", vbOKOnly, "Info")

Rows(RowCount).Delete Shift:=xlUp

' Trim spaces

Columns("A:A").Select
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, searchFormat:=False, _
    ReplaceFormat:=False

' Delete surplus columns

Range("L:T,V:AA,AE:AG,AR:AR,AU:AU,AZ:AZ").Select
    Selection.Delete Shift:=xlToLeft

' Delete surplus rows

Do
    If Left(Cells(r, 1), 1) = "D" _
       Or Left(Cells(r, 1), 1) = "H" _
       Or Left(Cells(r, 1), 1) = "I" _
       Or Left(Cells(r, 1), 2) = "MD" _
       Or Left(Cells(r, 1), 2) = "ND" _
       Or Left(Cells(r, 1), 3) = "MSF" _
       Or Left(Cells(r, 1), 5) = "MSGZZ" _
       Or Len(Cells(r, 1)) = 5 _
       Or Cells(r, 3) = 0 Then
       Rows(r).Delete Shift:=xlUp
    ElseIf Int(Right(Cells(r, 1), 4)) > 4000 Then
       Rows(r).Delete Shift:=xlUp
    Else: r = r + 1
    End If
Loop Until (r = RowCount)

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:09:37+00:00Added an answer on June 15, 2026 at 4:09 pm

    The biggest issue is probably the amount of data you are looping through. I’ve updated your code to create a formula to check if the row needs to be deleted, then you can filter on that formula result and delete all rows at once.

    I’ve made a bunch of comments to both help you clean your code and understand what I did. I prefaced my comments with '=>.

    One last note, loading the values into an array may help as well, but if you have many, many columns of data, this may be more difficult. I don’t have a ton of experience with it, but I know it makes things worlds faster!

    Good luck and have fun!

    Option Explicit
    
    Sub delrows()
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    Dim r As Long, RowCount As Long
    r = 2
    
    Dim wks As Worksheet
    Set wks = Sheets(1) '=> change to whatever sheet index (or name) you want
    
    '=> rarely a need to select anything in VBA [ActiveSheet.Columns(1).Select]
    
    With wks
    
        RowCount = .Range("A" & .Rows.Count).End(xlUp).Row '=> as opposed to  [RowCount = UsedRange.Rows.Count], as UsedRange can be misleading
                                                                'NOTE: this also assumes Col A will have your last data row, can move to another column
    
        userresponse = MsgBox("You have " & RowCount & " rows", vbOKOnly, "Info")
    
        .Rows(RowCount).Delete Shift:=xlUp
    
        ' Trim spaces
    
        '=> rarely a need to select anything in VBA [Columns("A:A").Select]
        .Range("A1:A" & RowCount).Replace What:=" ", Replacement:="", lookat:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, searchFormat:=False, _
            ReplaceFormat:=False
    
        ' Delete surplus columns
    
        '=> rarely a need to select anything in VBA [Range("L:T,V:AA,AE:AG,AR:AR,AU:AU,AZ:AZ").Select]
        .Range("L:T,V:AA,AE:AG,AR:AR,AU:AU,AZ:AZ").Delete Shift:=xlToLeft ' as opposed to Selection.Delete Shift:=xlToLeft
    
        ' Delete surplus rows
    
        '=> Now, here is where we help you loop:
    
        '=> First insert column to the right to capture your data
        .Columns(1).Insert Shift:=xlToRight
        .Range("A1:A" & RowCount).FormulaR1C1 = "=If(OR(Left(RC[1],1) = ""D"",Left(RC[1],1) = ""H"", Left(RC[1],1) = ""I"", Left(RC[1],2) = ""MD"",Left(RC[1],2) = ""ND"",Left(RC[1],3) = ""MSF"",Left(RC[1],5) = ""MSGZZ"",Len(RC[1])=5),""DELETE"",If(Int(Right(RC[1],4)) > 4000,""DELETE"",""""),""""))"
    
        '=> Now, assuming you something to delete ...
        If Not .Columns(1).Find("DELETE", LookIn:=xlValues, lookat:=xlWhole) Is Nothing Then
    
            '=> filter and delete
            .Range("A1:A" & RowCount).AutoFilter 1, "DELETE"
            Intersect(.UsedRange, .UsedRange.Offset(1), .Range("A1:A" & RowCount)).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    
        End If
    
        '=> Get rid of formula column
        .Columns(1).EntireColumn.Delete
    
    End With
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute the following VBA code for each worksheet at once in
I am using the following VBA code to change the color of the rows
I'm running Excel 2003. I have the following VBA code, corresponding to a simple
I have the following VBA code: BD.Sheets(Sheet1).Range(F & Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row).Formula = =SUMIF(' &
I got the following VBA code from the web a while ago: Private Sub
I am trying to put the following code into VBA. What I ideally want
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Win-XP / Excel 2003 / VBA .... I have the following piece of code
I am using the following VBA code to export PPT slides to images. The
I have the following in a piece of VBA code: For i = 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.