Adding an IfError function to VBA code

pleasehalp

New Member
Joined
Oct 8, 2013
Messages
41
I've been looking at the forum and I'm a bit confused on how this function works. I just want these vlookups to return a value and if they can't find one then to just leave it blank.

Here is an example of some vlookups that I want to have if error values added to. Thanks for the help.

Range("S1").FormulaR1C1 = "=Sheet0!RC[3]"
Range("S2").FormulaR1C1 = "=VLOOKUP(RC[-3],Sheet0!R2C5:R" & LR & "C31,18,FALSE)"
Range("T1").FormulaR1C1 = "=Sheet0!RC[5]"
Range("T2").FormulaR1C1 = "=VLOOKUP(RC[-4],Sheet0!R2C5:R" & LR & "C31,21,FALSE)"
Range("U1").FormulaR1C1 = "=Sheet0!RC[5]"
Range("U2").FormulaR1C1 = "=VLOOKUP(RC[-5],Sheet0!R2C5:R" & LR & "C31,22,FALSE)"
Range("V1").FormulaR1C1 = "=Sheet0!RC[8]"
Range("V2").FormulaR1C1 = "=VLOOKUP(RC[-6],Sheet0!R2C5:R" & LR & "C31,26,FALSE)"
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I have this code to wrap an existing formula in excel with the Iferror function:
Select cells you'd like to wrap with the iferror formula then run macro:

Sub Add_IFERROR_Selection()
Dim myCell As Range
For Each myCell In Selection.Cells
If myCell.HasFormula And Not myCell.HasArray Then
myCell.Formula = "=IFERROR(" & Right(myCell.Formula, Len(myCell.Formula) - 1) & ",0)"
End If
Next
End Sub
 
Upvote 0
That works but it is extremely slow. Does anyone have a better suggestion? Also even though I changed it from 0 to blank it still populates the cells with a zero. I understand that this is probably a format issue with my cells. For instnace i want a % to be blank if the numbers aren't there to calculate it but with this macro it makes it 0% which is not really what it should be.
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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