Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello!
I have a, Excel workbook in which there are 2 spreadsheets; sheet1 contains a large amount of employee data and sheet2 contains names & project teams.I currently use formula '=VLOOKUP(H4, Sheet2!A:B, 2, FALSE)' on Sheet1(columnA) to map project teams (from Sheet2, columnB) based by employee names from Sheet1, columnH.
I need to go 1 step further and delete any rows where a project team was not returned (now displaying #N/A) on sheet1.
I've tried to modify other provided examples, but with no success - just a bunch of errors.
Can anyone help?
Thanks!!

re: Your subject line Formula Based Row Deletion
I assume you are aware that you can't actually delete a Row via a "formula". You can however do it with VBA.
re: I've tried to modify other provided examples
What examples have you tried?

I do know that, but I'm new to VB.
Basically I'm trying to delete any rows that did not have a project team returned into ColumnA as a result of the VLOOKUP function.
Here's the one I tried using from online and tweaking it; my issue is that I use vlookup to get the project team into sheet1 (column A)based on a table with the individual's name & project team from sheet2.
In using the sample below, it expects actual data and not formulas in the cell to determine the criteria to delete.
Sub DeleteRows()
Dim strToDelete As String
Dim rngSrc As Range
Dim NumRows As Integer
Dim ThisRow As Integer
Dim ThatRow As Integer
Dim ThisCol As Integer
Dim J As Integer
Dim DeletedRows As IntegerstrToDelete = InputBox("Value to Trigger Delete?", "Delete Rows")
Set rngSrc = ActiveSheet.Range(ActiveWindow.Selection.Address)NumRows = rngSrc.Rows.Count
ThisRow = rngSrc.Row
ThatRow = ThisRow + NumRows - 1
ThisCol = rngSrc.ColumnFor J = ThatRow To ThisRow Step -1
If Cells(J, ThisCol) = strToDelete Then
Rows(J).Select
Selection.Delete Shift:=xlUp
DeletedRows = DeletedRows + 1
End If
Next J
MsgBox "Number of deleted rows: " & DeletedRows
End SubAny suggestions? Or could you point me in the right direction? (I'd like to understand so I can learn from this - I'm new to coding...)

If I wanted to delete rows that contained #N/A in Column A, and I wasn't concerned about any other error showing up in that cell, I'd use something like this:
Sub Del_NA() 'Find last row with data in Column A lastRow = Range("A" & Rows.Count).End(xlUp).Row 'Start at bottom and delete rows with errors For myNA = lastRow To 1 Step -1 If IsError(Cells(myNA, 1)) Then _ Cells(myNA, 1).EntireRow.Delete Next End Sub

Huh -simple...I like it!
I'm going to run it and I'll let you know how it goes. Thanks for your help!!

![]() |
Outlook Free/Busy Not Sho...
|
Excel custom tab order
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |