VBA - Deleting determinated cells in Determinated Range

frantheman97

New Member
Joined
Jul 4, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I'm trying to delete cells that are different to "0" or "-".
But, I'm having the problem that the macro is deleting in all the worksheet, instead of the specific range.

Do you know how can I do to delete in a specific range?
I want to determine range ("b8,Aj861",.end(xldown)), but the last row must be the last value. and I dont know why it doesnt work.

Also, I would like not to delete the title or the filter row.


VBA Code:
Sub prueba()

    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = ActiveSheet

   ' I want to determine this range, but the last row must be the last value.
   
   Set rng1 = ws.Range("b8:aj861")



    'Reset Existing Filters
    On Error Resume Next
    ws.ShowAllData
    On Error GoTo 0
 
    'Apply Filter
    rng1.AutoFilter Field:=20, Criteria1:="<>-", _
        Operator:=xlAnd, Criteria2:="<>0"
      

    'Delete Rows
       
    Application.DisplayAlerts = False
    rng1.SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True
    
    'Clear Filter
    On Error Resume Next
    ws.ShowAllData
    On Error GoTo 0

End Sub

Thanks for your help.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Just to clarify, you want to only have rows where column 20 (Field:=20) has either a zero or a dash in it?

Your code is worked for me to hide the zeros and dashes, delete the rest and show just those.

If you have one column that always has data in it, you can use this in place of the "Set rng1...":
VBA Code:
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row
Set rng1 = ws.Range("b8:aj" & LastRow)

I also added this just before the last "On Error Goto 0" to get rid of the column filters, if you want to.

VBA Code:
If ws.AutoFilterMode Then ws.AutoFilterMode = False
 
Upvote 0
Solution

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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