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
 
With these lines it also gives me the syntax error:

Public Sub ClearSomeCells()


Application.Calculation = xlManual


Dim lastRow As Long


lastRow = Cells(Rows.Count, "R").End(xlUp).Row
With Range("A1:DG" & lastRow)
.AutoFilter Field:=18, Criteria1:=Array("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
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Do you have a header row?
If so what row is it in?
 
Upvote 0
Do you have something like this

Excel 2013 32 bit
ABCDEFGHI
1PostcodeIn Use?LatitudeLongitudeEastingNorthingGridRefCountyDistrict
2AL1 5EGYes51.750185-0.32129515981207054TL159070HertfordshireSt Albans
3AL10 9WXYes51.750259-0.234901521944207203TL219072HertfordshireWelwyn Hatfield
4AL2 1UXYes51.721887-0.312479516662203921TL166039HertfordshireSt Albans
5AL2 2EJYes51.702462-0.326762515725201738TL157017HertfordshireHertsmere
Postcodes


Where the first row contains a header, or description of what that column holds, with the actual data starting in row 2?
 
Upvote 0
Not exactly.
It contains texts and functions, too. And there are some merged cells as well.




Do you have something like this
Excel 2013 32 bit
ABCDEFGHI
1PostcodeIn Use?LatitudeLongitudeEastingNorthingGridRefCountyDistrict
2AL1 5EGYes51.750185-0.32129515981207054TL159070HertfordshireSt Albans
3AL10 9WXYes51.750259-0.234901521944207203TL219072HertfordshireWelwyn Hatfield
4AL2 1UXYes51.721887-0.312479516662203921TL166039HertfordshireSt Albans
5AL2 2EJYes51.702462-0.326762515725201738TL157017HertfordshireHertsmere

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Postcodes



Where the first row contains a header, or description of what that column holds, with the actual data starting in row 2?
 
Upvote 0
Are you getting a syntax error or a 1004 error (AutoFilter method failed)? Is there anything in column A?

WBD
 
Upvote 0
AZON
clip_image002.png
Megrendelőnév

<tbody>
</tbody>
CikkCikknévPLUdbkiállási időpontMMokkészdb ok?BIZERBA 1dbdbdbdbdbjav
363784_010PENNY MARKET KFT.8113Trappista 1,25 kg darabolt *70112 4802017-12-13 04:001,74.lejárt
363799_010PENNY MARKET KFT.8125Edami 1,25 kg darabolt *704122017-12-13 04:000,04.lejárt
363799_020PENNY MARKET KFT.8129Trappista light 1,4 kg *70282017-12-13 04:000,04.lejárt
363783_010PENNY MARKET KFT.8311Trappista fel.egal.700g SISSY*03 8402017-12-13 04:000,24.lejárt 2 1 000
363727_010TESCO GLOBAL ÁRUHÁZAK ZRT.8103Trappista 1,5 kg *1351442017-12-13 06:000,04.lejárt11 500
363712_010PENNY MARKET KFT. /VESZPRÉMI/8113Trappista 1,25 kg darabolt *1723 3242017-12-13 06:000,44.lejárt12 100
363727_400TESCO GLOBAL ÁRUHÁZAK ZRT.8149Grande sajt 1,25 kg *707722017-12-13 06:000,04.lejárt
363745_010HÚS DEPÓ KFT. (E.M.É.K.)8103Trappista 1,5 kg *100642017-12-12 14:000,03.készx13 50
363687_010METRO KFT. MISKOLC8103Trappista 1,5 kg *117482017-12-12 14:000,03.készx
363687_020METRO KFT. MISKOLC8113Trappista 1,25 kg darabolt *118482017-12-12 14:000,03.készx15 20
363707_010K-ALFI-KER KFT. /REÁL/ 2. SZ. RAKT8141Trappista 1,5 kg falusi REAL*1231442017-12-12 14:000,03.készx
363690_010METRO KFT. KELET-PEST8103Trappista 1,5 kg *1172402017-12-12 21:000,03.készx
363681_010METRO KFT. BUDAPEST /FERENCVÁROS/8103Trappista 1,5 kg *117962017-12-12 21:000,03.készx19 10
363681_020METRO KFT. BUDAPEST /FERENCVÁROS/8113Trappista 1,25 kg darabolt *118482017-12-12 21:000,03.készx
363568_020METRO KFT. BUDAKALÁSZ8149Grande sajt 1,25 kg *716122017-12-12 21:000,03.készx

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col span="5"><col span="5"><col></colgroup><tbody>
</tbody>


Could you supply a snapshot of your data similar to post#14?
there are some tools here to help
https://www.mrexcel.com/forum/about-board/508133-attachments.html
 
Upvote 0
Judging by that you want to filter on col I not R, if that's right try this modified version of WBD's code
Code:
Public Sub ClearSomeCells()

Dim lastRow As Long

lastRow = Cells(Rows.Count, "R").End(xlUp).row
Range("A1:DG1").AutoFilter Field:=9, Criteria1:=Array("4.lejárt", "5.régi"), Operator:=xlFilterValues
With Range("A2:DG" & lastRow)
    .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
 
Upvote 0

Forum statistics

Threads
1,216,112
Messages
6,128,901
Members
449,477
Latest member
panjongshing

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