Error while looping through column and deleting specific values

Brock_Hardchest

New Member
Joined
Feb 23, 2018
Messages
27
Hello,

In the below code I am trying to delete cells that say Inactive in column G, then I create a new column B filled with Vlookup values some of which return errors as "#N/A". The last part is identical to the earlier column G code but is supposed to delete cells with #N/A in column B. Running this produces a Type Mis-Match error for the line: If ws.Range("B" & k).Value = "#N/A" Then

This line works fine in the earlier code but fails the second time. Any help is appreciated!

Code:
Dim xrow As Long
Dim k As Long
Dim ws As Worksheet




Application.ScreenUpdating = False


Set ws = ActiveSheet


    For k = ws.Range("G" & Rows.Count).End(xlUp).Row To 2 Step -1
        If ws.Range("G" & k).Value = "Inactive" Then
            Rows(k).EntireRow.Delete
        End If
        
    
    Next k




xrow = Cells(Rows.Count, 1).End(xlUp).Row


Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Range("B2").Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(RC[-1],'[Copy of Candlestick Product Crosswalk.xlsx]Crosswalk from Dallas'!R2C1:R188C3,3,FALSE)"
    Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:B" & xrow)


    For k = ws.Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
        If ws.Range("B" & k).Value = "#N/A" Then
            Rows(k).EntireRow.Delete
        End If
        
    
    Next k
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Maybe try like this instead:

Rich (BB code):
  For k = ws.Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
    If IsError(ws.Range("B" & k).Value) Then
      Rows(k).EntireRow.Delete
    End If
  Next k
 
Upvote 0
Another option
Code:
On Error Resume Next
Range("B:B").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
On Error GoTo 0
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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