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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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
 
Upvote 0
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?
 
Upvote 0
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,
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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