I am trying to figure out how to determine if the VLOOKUP formula returns #N/A. I want to find what the formula returns, if it is NOT #N/A, then go to the next cell.

If it IS #N/A, then I want to:

1.Copy cell A (and the row number the #N/A occurs) as well as cell C of the same row.
In my example, that would be A7 and C7.

2. Paste these values into Sheets(Corrected ID-Name), at the first open row at the bottom
In my example, that would be row 7 on corrected ID-Name.

3. Hopefully B8 on Sheet1, will automatically update the Vlookup formula, if not, do I need to say “update calculations, then go on to next cell”?

I understand that the names on Sheet1 column C, are different for the same ID Number, the first one will be copied, whatever that maybe and then from now on, that is what that ID will refer to.

Code and example workbook attached

Thank you,
Kevin

Sub VLookUpAutomation()Dim cell As RangeDim rng As Range' Run with Sheet1 as active sheetActiveSheet.Columns("B:B").Insert Shift:=xlToRightActiveSheet.Range("B1").Value = "VL"ActiveSheet.Range("B2").FormulaR1C1 = _ "=vlookup(RC[-1],'Corrected ID-Name'!C1:C2,2,FALSE)"Set rng = [B2]Set rng = Range(rng, Cells(Cells(Rows.Count, 1).End(xlUp).Row, rng.Column)) If rng.Rows.Count = 1 Then rng.Select Else rng.Cells(1, 1).AutoFill Destination:=rng, Type:=xlFillDefault End IfFor Each cell In rng If cell = "#N/A" Then MsgBox "Found an #N/A" 'copy cell to left and copy cell to right 'paste onto Correct Agent ID-Name 'Using first open row at bottom 'Will vlookup update so that repeated #N/A's are not copyied again? 'If not, do I need to say "recalculate" prior to going on to next cell looking for #N/A Else MsgBox "Not a #N/A" End IfNext cellEnd Sub

Not sure I follow, but if you replace you lines 21-32 with this, does it work?

For Each cell In rng.SpecialCells(xlCellTypeFormulas, xlErrors) On Error Resume Next If cell.Value = CVErr(xlErrNA) Then 'MsgBox "Found an #N/A" cell.Offset(, -1).Copy Sheet2.Range("A" & Rows.Count).End(xlUp)(2) cell.Offset(, 1).Copy Sheet2.Range("B" & Rows.Count).End(xlUp)(2) 'copy cell to left and copy cell to right 'paste onto Correct Agent ID-Name 'Using first open row at bottom 'Will vlookup update so that repeated #N/A's are not copyied again? 'If not, do I need to say "recalculate" prior to going on to next cell looking for #N/A Else 'MsgBox "Not a #N/A" End If On Error GoTo 0Next cell

That sort of did it. See attached workbook, sheet corrected ID-Name, your code produced A1:B11, I need it to be like D1:E8, in that it only copies the name once.

After the first copy:
cell.Offset(, -1).Copy Sheet2.Range("A" & Rows.Count).End(xlUp)(2)
cell.Offset(, 1).Copy Sheet2.Range("B" & Rows.Count).End(xlUp)(2)

Is there a way to say "refresh the VLOOKUP", so it would populate SUE ME in cell B8 (sheet1) and thus on the next loop that would be an else, resulting in the name being copied only on the first time an error occurs.

For Each cell In rng.SpecialCells(xlCellTypeFormulas, xlErrors) On Error Resume Next If cell.Value = CVErr(xlErrNA) Then 'MsgBox "Found an #N/A" cell.Offset(, -1).Copy Sheet2.Range("A" & Rows.Count).End(xlUp)(2) cell.Offset(, 1).Copy Sheet2.Range("B" & Rows.Count).End(xlUp)(2)

I put that line in, but the messages box never triggers to:

MsgBox "Found an #N/A"

only the else:

MsgBox "Not a #N/A" comes up.

Sub VLookUpAutomation()Dim cell As RangeDim rng As RangeActiveSheet.Columns("B:B").Insert Shift:=xlToRightActiveSheet.Range("B1").Value = "VL"ActiveSheet.Range("B2").FormulaR1C1 = _ "=vlookup(RC[-1],'Corrected ID-Name'!C1:C2,2,FALSE)"Set rng = [B2]Set rng = Range(rng, Cells(Cells(Rows.Count, 1).End(xlUp).Row, rng.Column)) If rng.Rows.Count = 1 Then rng.Select Else rng.Cells(1, 1).AutoFill Destination:=rng, Type:=xlFillDefault End IfFor Each cell In rng If IsError(ActiveCell.Value) Then MsgBox "Found an #N/A" 'copy cell to left and copy cell to right 'paste onto Correct Agent ID-Name 'Using first open row at bottom 'Will vlookup update so that repeated #N/A's are not copyied again? 'If not, do I need to say "recalculate" prior to going on to next cell looking for #N/A Else MsgBox "Not a #N/A" End IfNext cellEnd Sub

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Sub VLookUpAutomation()Dim cell As RangeDim rng As RangeSheet1.ActivateActiveSheet.Columns("B:B").Insert Shift:=xlToRightActiveSheet.Range("B1").Value = "VL"ActiveSheet.Range("B2").FormulaR1C1 = _ "=vlookup(RC[-1],'Corrected ID-Name'!C1:C2,2,FALSE)"Set rng = [B2]Set rng = Range(rng, Cells(Cells(Rows.Count, 1).End(xlUp).Row, rng.Column)) If rng.Rows.Count = 1 Then rng.Select Else rng.Cells(1, 1).AutoFill Destination:=rng, Type:=xlFillDefault End IfFor Each cell In rng.SpecialCells(xlCellTypeFormulas, xlErrors) On Error Resume Next If cell.Value = CVErr(xlErrNA) Then 'MsgBox "Found an #N/A" With Sheet2 If IsError(Application.Match(cell.Offset(, -1), .Columns(1), 0)) Then cell.Offset(, -1).Copy .Range("A" & Rows.Count).End(xlUp)(2) cell.Offset(, 1).Copy .Range("B" & Rows.Count).End(xlUp)(2) End If End With Else 'MsgBox "Not a #N/A" End If On Error GoTo 0Next cellEnd Sub

Sub VLookUpAutomation()Dim cell As RangeDim rng As RangeSheet1.ActivateColumns("B:B").Insert Shift:=xlToRightRange("B1").Value = "VL"Set rng = Range("C2", Range("C2").End(xlDown)).Offset(, -1)rng.FormulaR1C1 = "=vlookup(RC[-1],'Corrected ID-Name'!C1:C2,2,FALSE)"On Error Resume NextFor Each cell In rng.SpecialCells(xlCellTypeFormulas, xlErrors) If cell.Value = CVErr(xlErrNA) Then With Sheet2 If IsError(Application.Match(cell.Offset(, -1), .Columns(1), 0)) Then cell.Offset(, -1).Copy .Range("A" & Rows.Count).End(xlUp)(2) cell.Offset(, 1).Copy .Range("B" & Rows.Count).End(xlUp)(2) End If End With End IfNext cellOn Error GoTo 0End Sub

For Each cell In rng
If IsError(cell.Value) Then
MsgBox "Found an #N/A"
'copy cell to left and copy cell to right
'paste onto Correct Agent ID-Name
'Using first open row at bottom
'Will vlookup update so that repeated #N/A's are not copyied again?
'If not, do I need to say "recalculate" prior to going on to next cell looking for #N/A
Else
MsgBox "Not a #N/A"
End If
Next cell

StephenJR beat you to it, but thank you for your comment, I really like your proposed solution, as I have used the ISNA and ISERROR in formulas quite often, so I will try implementing this in future projects.

Regard,
Kevin

0

Featured Post

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Here are a few simple, working, games that you can use as-is or as the basis for your own games.
Tic-Tac-Toe
This is one of the simplest of all games.
The game allows for a choice of who goes first and keeps track of the number of wins for…

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…

The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…