Solved

VBA - VLOOKUP if #N/A is returned Then do something Else Next cell in range

Posted on 2011-03-02
9
3,177 Views
Last Modified: 2012-05-11
Experts,

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 Range
Dim rng As Range

' Run with Sheet1 as active sheet

ActiveSheet.Columns("B:B").Insert Shift:=xlToRight
ActiveSheet.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 If
    
For 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 If
Next cell

End Sub

Open in new window

EE-VBA-VLookUp-Automation-Versio.xls
0
Comment
Question by:redrumkev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 35022021
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 0
Next cell

Open in new window

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 35022071
This would be a simple way ot handle it:

If IsError(ActiveCell.Value) Then
...
0
 
LVL 23

Author Comment

by:redrumkev
ID: 35022121
StephenJR.

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)

Open in new window

EE-VBA-VLookUp-Automation-Versio.xls
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Author Comment

by:redrumkev
ID: 35022144
rspahitz,

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 Range
Dim rng As Range

ActiveSheet.Columns("B:B").Insert Shift:=xlToRight
ActiveSheet.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 If
    
For 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 If
Next cell

End Sub

Open in new window

EE-VBA-VLookUp-Automation-Versio.xls
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35022154
What about this?
Sub VLookUpAutomation()

Dim cell As Range
Dim rng As Range

Sheet1.Activate
ActiveSheet.Columns("B:B").Insert Shift:=xlToRight
ActiveSheet.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 If
    
For 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 0
Next cell
End Sub

Open in new window

0
 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
ID: 35022189
Or slightly abbreviated and tidied up:
Sub VLookUpAutomation()

Dim cell As Range
Dim rng As Range

Sheet1.Activate
Columns("B:B").Insert Shift:=xlToRight
Range("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 Next
For 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 If
Next cell
On Error GoTo 0

End Sub

Open in new window

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 35022236
For mine, replace the word ActiveCell with cell:

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
0
 
LVL 23

Author Closing Comment

by:redrumkev
ID: 35022279
StephenJR,

Thank you, I was going to accept the posting before this, but this is even better!

Thanks again,
Kevin
0
 
LVL 23

Author Comment

by:redrumkev
ID: 35022304
rspahitz,

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
78 files, need to delete row 2 in every file 3 34
NEED LOOK FOR NUM 23 29
need help to look for 16 42
Excel VBA Script 9 57
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question