VBA code for deleting rows that contains #VALUE!

tommybone30

New Member
Joined
Dec 14, 2017
Messages
14
Hello,

I need help with providing the right code.

After doing one Macro automation, I would like to delete any rows that have a cell value of "#VALUE!" (which is essentially a computed formula that comes back with that message). Is it possible for it to be done? And keep in mind that this needs to be done on multiple worksheets, so the code needs to be applied on each sheet (if the "#VALUE!" message exists).

Please help!

Thank you!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Can the error message #value appear in any cell in the worksheet or is it in a particular column. If in a particular column, which one.
 
Upvote 0
Code:
Option Explicit


Sub DelErr()
    Dim lr As Long, i As Long
    lr = Range("I" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If IsError(Range("I" & i)) Then
            Range("I" & i).EntireRow.Delete
        End If
    Next i
End Sub

Use the above code to do each sheet separately. If you wish to loop through the sheets then use the code below.
Code:
Option Explicit


Sub DelErr()
    Dim lr As Long, i As Long
    Dim w As Worksheet
    For Each w In Worksheets
    lr = w.Range("I" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If IsError(w.Range("I" & i)) Then
            w.Range("I" & i).EntireRow.Delete
        End If
    Next i
End Sub
 
Last edited:
Upvote 0
Here is another macro that you can consider...
Code:
Sub DelErr()
  Columns("I").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
End Sub
 
Upvote 0
Code:
Option Explicit


Sub DelErr()
    Dim lr As Long, i As Long
    lr = Range("I" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If IsError(Range("I" & i)) Then
            Range("I" & i).EntireRow.Delete
        End If
    Next i
End Sub

Use the above code to do each sheet separately. If you wish to loop through the sheets then use the code below.
Code:
Option Explicit


Sub DelErr()
    Dim lr As Long, i As Long
    Dim w As Worksheet
    For Each w In Worksheets
    lr = w.Range("I" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If IsError(w.Range("I" & i)) Then
            w.Range("I" & i).EntireRow.Delete
        End If
    Next i
End Sub

I kept getting "Compile error: For without Next". Any reasons why?
 
Upvote 0
Yup. Missed a next command. After the Next i, insert on the next line,
Code:
Next w
 
Upvote 0
Awesome, it works!

However, I have a follow-up question. It is very similar to the one above.

In another Excel project, there are usually monthly benefits data that are filled out within multiple columns. In my coding, I ask it to create a new column (see yellow highlight column below), and fill out a Vlookup formula (using the SSN as my lookup value). However, the amount of people in the file changes month to month, and since I do not know what the limit was, I simply ask the Vlookup formula to go two times above the amount of data (e.g. there's roughly 600 people average per month, but I ask Vlookup to look for 2000 cells because you would never know if there are WAY more people the next month that could go way above 600).

Now, obviously the cells that does not have any corresponding data in the same row will issue a #N/A error. However, due to the nature of the Vlookup, it's possible that a few rows that DOES contain corresponding data will also get the #N/A error, which I do not want to delete (I will simply make manual changes to get the row's data accurate).

For visual aid:

xxx-xx-0833THOMAS, MICHAEL3170COICancer24.924.9
xxx-xx-0191THOMAS, SCOTT3225COIAccident26.3326.33
xxx-xx-9027THORNTON, TATIANA3160COIAccident11.3111.31
xxx-xx-4156TUCKER, JHAN#N/A#N/AAccident11.3111.31
xxx-xx-9327UN, AMANDA3435COICancer27.0827.08
xxx-xx-3523VANN, WENDIE#N/A#N/ACancer24.924.9
xxx-xx-2773WASHINGTON, SYLVIA3435COIAccident21.9921.99
xxx-xx-8740WILLIAMS, ASHANTI#N/A#N/AAccident11.3111.31
xxx-xx-5681WILSON, CHRISTINA3226COICancer34.3134.31
xxx-xx-7111WRIGHT, TRINA3435COIAccident11.3111.31
xxx-xx-1354YOUNGBLOOD, ALEXUS3160COIAccident11.3111.31
#N/A#N/A
#N/A#N/A
#N/A#N/A
#N/A#N/A
#N/A#N/A
#N/A#N/A
<colgroup><col width="215" style="width: 161pt; mso-width-source: userset; mso-width-alt: 7862;" span="2"> <col width="172" style="width: 129pt; mso-width-source: userset; mso-width-alt: 6290;"> <col width="172" style="width: 129pt; mso-width-source: userset; mso-width-alt: 6290;" span="2"> <col width="113" style="width: 85pt; mso-width-source: userset; mso-width-alt: 4132;"> <col width="73" style="width: 55pt; mso-width-source: userset; mso-width-alt: 2669;"> <col width="126" style="width: 95pt; mso-width-source: userset; mso-width-alt: 4608;"> <col width="66" style="width: 50pt; mso-width-source: userset; mso-width-alt: 2413;"> <tbody> </tbody>


My question is, similar to how was done in the post before, how do you delete any #N/A cells that do not have any corresponding info in the other columns along the same row, but keep the #N/A cells that does have corresponding info? Is there a work-around on that?

This will help me tons!

Thanks.
 
Upvote 0
Assumes data begins in Column A

Code:
Option Explicit


Sub DelNA()
    Dim lr As Long, i As Long
    lr = Range("c" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If IsError(Range("C" & i)) Then
            If Range("A" & i) = "" Then
                Range("A" & i).EntireRow.Delete
            End If
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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