vba code help

isacp

Board Regular
Joined
Dec 16, 2004
Messages
135
Sub Macro13()
'
' Macro13 Macro
' Macro recorded 1/18/2005 by isacp
'
' Keyboard Shortcut: Ctrl+Shift+N
'
For Each Item In Selection
If Item.Value = "NA" Then
Item.EntireRow.Delete
End If
Next Item
End Sub

this is my code
for some reason when i select say b1:b20
and all the cells have NA in it it only deletes 10
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Barrie Davidson

MrExcel MVP
Joined
Feb 10, 2002
Messages
2,330
Would this help you out?
Code:
Dim DelRange As Range
Dim RangeSet As Boolean

RangeSet = False
For Each Item In Selection
    If Item.Value = "NA" Then
        If RangeSet = False Then
            Set DelRange = Item
            RangeSet = True
        Else
            Set DelRange = Union(DelRange, Item)
        End If
    End If
Next Item
If RangeSet = False Then Exit Sub
DelRange.EntireRow.Delete
 

isacp

Board Regular
Joined
Dec 16, 2004
Messages
135
it worked great on the cells that have na but not all cells that im celecting have na.

and anyway i still cant figure out whats wrong wth my code?
 

Barrie Davidson

MrExcel MVP
Joined
Feb 10, 2002
Messages
2,330
isacp said:
it worked great on the cells that have na but not all cells that im celecting have na.
It won't delete any cells that don't have na.

isacp said:
and anyway i still cant figure out whats wrong wth my code?
I think (not tested) it's because you're looping through a selection and the selection keeps getting smaller. That is, you're deleting rows each time na is found. My code finds all the na's and then deletes them all at once.

Regards,
 

tactps

Well-known Member
Joined
Jan 20, 2004
Messages
3,460
The issue is that you are deleting a row (which then automatically selects the next row) and then you have a "next item" statement (which drops down two.

So if you have 3 "NA" in a row, the second one is missed.

I suggest that if you find an "NA" you restart the macro like this:

Sub Macro13()
'
' Macro13 Macro
' Macro recorded 1/18/2005 by isacp
'
' Keyboard Shortcut: Ctrl+Shift+N
'
For Each Item In Selection
If Item.Value = "NA" Then
Item.EntireRow.Delete
Macro13
End If
Next Item
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,119
Messages
5,768,221
Members
425,460
Latest member
Astros1243

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
Top