Modification needed in VBA

danelskibr

Board Regular
Joined
Dec 31, 2014
Messages
58
Hello,

The code below works fine. The problem is that it is very slow and clunk. I am not knowledgeable enough to find a better wat to make it work more efficiently.

WHAT IT DOES: In an alphabetically sorted column, it rinds each row with the word "Benchmark" and deletes that row. It does this until no Benchmarks remain and then it errors. I have it set to move along once it reaches the error.

WHAT I WANT IT TO DO: Find the first row with the word "Benchmark" in column B. Select down to the last row with the word Benchmark in Column B, and delete these rows.

Code:
 Dim StopCell3 As Range
    Set StopCell3 = Columns("B").Find("Benchmark", Cells(Rows.Count, "B"), xlValues, xlWhole, xlNext, True)
    
     On Error Resume Next
    Do
    Set StopCell3 = Columns("B").Find("Benchmark", Cells(Rows.Count, "B"), xlValues, xlWhole, xlNext, True)
   Range(StopCell3, StopCell3.Offset(0, 0)).Select
   Selection.EntireRow.Delete
    Selection.End(xlDown).Offset(1, 0).Select
   Loop While Not StopCell3 Is Nothing
       On Error GoTo 0

Thanks for your help!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You could do that with an autofilter if your column contains a header.

Code:
With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    x = .Range("B" & .Rows.Count).End(xlUp).Row
    With .Range("B1").Resize(x)
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="Benchmark"
        .Range("B1").Offset(1).Resize(x - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
 
Upvote 0
If your BENCHMARK cells contain text constants (that is, there are no formulas in those cells), then this code will work quite quickly and, if it is helpful to you at all, there is no need to sort or filter the data beforehand...
Code:
Sub DeleteRowWithBENCHMARK()
  Columns("B").Replace "BENCHMARK", "#N/A", xlWhole, , False, False, False
  On Error GoTo NoBenchmarks
  Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoBenchmarks:
End Sub
 
Last edited:
Upvote 0
Works perfectly! I was trying to avoid sorting or filtering so this is just what I needed. Thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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