VBA - match criteria then delete specific cells (not entire rows!)

keresztesi

Board Regular
Joined
Aug 14, 2017
Messages
64
Hi,

I'd like to have a vba code for that problem.

If column "R" contains whether "xxx" or "yyy" or "zzz" then delete from the specific rows thw following cells: U:AD and AV:BE and BW:CF and CX:DG.

So, for example in column "R" it finds "xxx" in row number 15 and "yyy" in row number 22, than it clears the following cells:
U15:AD15 and U22:AD22
AV15:BE15 and AV22:BE22
BW15:CF15 and BW22:CF22
CX15:DG15 and CX22:DG22

Thx
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
Public Sub ClearSomeCells()

Dim lastRow As Long
Dim thisRow As Long

lastRow = Cells(Rows.Count, "R").End(xlUp).Row
For thisRow = 1 To lastRow
    Select Case Cells(thisRow, "R")
        Case "xxx", "yyy", "zzz"
            With Cells(thisRow, "A")
                .Range("U1:AD1").ClearContents
                .Range("AV1:BE1").ClearContents
                .Range("BW1:CF1").ClearContents
                .Range("CX1:DG1").ClearContents
            End With
    End Select
Next thisRow

End Sub

WBD
 
Upvote 0
Thx, it works although it takes a lot of time (more then 5 minutes!)
Is there a way to "speed it up"?

Thx


Code:
Public Sub ClearSomeCells()

Dim lastRow As Long
Dim thisRow As Long

lastRow = Cells(Rows.Count, "R").End(xlUp).Row
For thisRow = 1 To lastRow
    Select Case Cells(thisRow, "R")
        Case "xxx", "yyy", "zzz"
            With Cells(thisRow, "A")
                .Range("U1:AD1").ClearContents
                .Range("AV1:BE1").ClearContents
                .Range("BW1:CF1").ClearContents
                .Range("CX1:DG1").ClearContents
            End With
    End Select
Next thisRow

End Sub

WBD
 
Upvote 0
You could try to suppress screen updating:

Code:
Public Sub ClearSomeCells()

Dim lastRow As Long
Dim thisRow As Long

Application.ScreenUpdating = False

lastRow = Cells(Rows.Count, "R").End(xlUp).Row
For thisRow = 1 To lastRow
    Select Case Cells(thisRow, "R")
        Case "xxx", "yyy", "zzz"
            With Cells(thisRow, "A")
                .Range("U1:AD1").ClearContents
                .Range("AV1:BE1").ClearContents
                .Range("BW1:CF1").ClearContents
                .Range("CX1:DG1").ClearContents
            End With
    End Select
Next thisRow

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Unfortunately it didn't become faster! :-(

It is maybe because my excel file is full with huge functions.
Maybe it recalculates everything cell by cell?
 
Upvote 0
I haven't tested this extensively but perhaps filtering would work for you instead:

Code:
Public Sub ClearSomeCells()

Dim lastRow As Long

lastRow = Cells(Rows.Count, "R").End(xlUp).Row
With Range("A1:DG" & lastRow)
    .AutoFilter Field:=18, Criteria1:=Array("xxx", "yyy", "zzz"), Operator:=xlFilterValues
    .SpecialCells(xlCellTypeVisible).Range("U:AD").ClearContents
    .SpecialCells(xlCellTypeVisible).Range("AV:BE").ClearContents
    .SpecialCells(xlCellTypeVisible).Range("BW:CF").ClearContents
    .SpecialCells(xlCellTypeVisible).Range("CX:DG").ClearContents
    .AutoFilter
End With

End Sub

WBD
 
Upvote 0
I tested it, but it gave me an error for this line:
.AutoFilter Field:=18, Criteria1:=Array("5.régi", "4.lejárt"), Operator:=xlFilterValues

Instead "xxx", "yyy" a used other criterias.





I haven't tested this extensively but perhaps filtering would work for you instead:

Code:
Public Sub ClearSomeCells()

Dim lastRow As Long

lastRow = Cells(Rows.Count, "R").End(xlUp).Row
With Range("A1:DG" & lastRow)
    .AutoFilter Field:=18, Criteria1:=Array("xxx", "yyy", "zzz"), Operator:=xlFilterValues
    .SpecialCells(xlCellTypeVisible).Range("U:AD").ClearContents
    .SpecialCells(xlCellTypeVisible).Range("AV:BE").ClearContents
    .SpecialCells(xlCellTypeVisible).Range("BW:CF").ClearContents
    .SpecialCells(xlCellTypeVisible).Range("CX:DG").ClearContents
    .AutoFilter
End With

End Sub

WBD
 
Upvote 0
add these two lines in code in top and before ending:
application.calculation = xlmanual <<< top
application.calculation = xlautomatic<<< before end sub
 
Last edited:
Upvote 0
It looks like this:
Public Sub ClearSomeCells()


Dim lastRow As Long
Application.Calculation = xlManual


lastRow = Cells(Rows.Count, "R").End(xlUp).Row
With Range("A1:DG" & lastRow)
.AutoFilter Field:=18, Criteria1:=("5.régi", "4.lejárt"), Operator:=xlFilterValues
.SpecialCells(xlCellTypeVisible).Range("U:AD").ClearContents
.SpecialCells(xlCellTypeVisible).Range("AV:BE").ClearContents
.SpecialCells(xlCellTypeVisible).Range("BW:CF").ClearContents
.SpecialCells(xlCellTypeVisible).Range("CX:DG").ClearContents
.AutoFilter
End With
Application.Calculation = xlAutomatic


End Sub

But it still gives an error message: "Syntax error" for that line:
.AutoFilter Field:=18, Criteria1:=("5.régi", "4.lejárt"), Operator:=xlFilterValues






add these two lines in code in top and before ending:
application.calculation = xlmanual <<< top
application.calculation = xlautomatic<<< before end sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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