VBA for Replacing #N/A with Value from an Offset Cell in Multiple Columns

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a macro that uses VLOOKUP to pull values from an older version of a spreadsheet on a newer version to look for differences in the values. I then filter for any differences over 0. It works perfectly if there is an exact 1:1 match for the VLOOKUP values. Sometimes, though, the newer data will have a value that isn't in the original data, and the VLOOKUP will leave behind #N/A.

So, I need some code that will look for #N/A in Columns L and M and replace with the offset value from the same row in Column J. For example:

Rank Final IncentiveFW User JF ConcatenateLogin User IdFiscal WeekEmp IDAutolink File NumPay GrpEarning CodeIncentive Job FunctionFinal Incentive PayoutOriginalDifference
17aboydBAG PICKaboyd17BAG PICK$25.21
#N/A​
#N/A​

Should look like this:

Rank Final IncentiveFW User JF ConcatenateLogin User IdFiscal WeekEmp IDAutolink File NumPay GrpEarning CodeIncentive Job FunctionFinal Incentive PayoutOriginalDifference
17aboydBAG PICKaboyd17BAG PICK$25.21$25.21$25.21

Can anyone help?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How would I plug that into my code?

This is the original VLOOKUP part of the code:

VBA Code:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-10],C[3]:C[4],2,FALSE)"
 
Upvote 0
Try this:
VBA Code:
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-10],C[3]:C[4],2,FALSE),RC10)"
 
Upvote 0
Ok, that works in the VLOOKUP section for Column L, but Column M is based on this formula:

VBA Code:
ActiveCell.FormulaR1C1 = "=RC[-3]-RC[-1]"

How can I adapt to do the same thing?
 
Upvote 0
You are using relative addressing which makes it really hard to work out what columns you want any amendments to use.
If you want the above modified with If error what column do you want to return the value from ?
 
Upvote 0
Then same format as before.
VBA Code:
 ActiveCell.FormulaR1C1 = "=IFERROR(RC[-3]-RC[-1], RC10)"
 
Upvote 0
Ah, unfortunately by fixing the #N/A issue at the VLOOKUP phase, I end up with a zero in Column M, because the value in Column L now matches what is in Column J. So I need the error correction part to be done after VLOOKUP in Column L and the subtraction formula in Column M.
 
Upvote 0
I figured out a solution from doing some Googling. Here is what I came up with:

VBA Code:
Columns("L:L").Select
    Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

    
    Dim NARange As Range, CheckCell As Range

    Set NARange = Range("M2:M1000")

    For Each CheckCell In NARange
    If Application.WorksheetFunction.IsNA(CheckCell) Then
         CheckCell.Offset(0, -3).Copy
         CheckCell.Select
         ActiveSheet.Paste
    End If
    Next CheckCell

I realized that I actually needed to make the value in Column L a zero because otherwise it wouldn't make sense to claim that the value in Column M has a difference compared to Column J.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,477
Messages
6,125,030
Members
449,205
Latest member
Eggy66

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