MS Excel Formula in VBA

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks and advance. I am getting the following error: "Run-time error '1004' "Application-definded or object defined error" on the following line:

VBA Code:
Range("B" & i).Formula = "= IFERROR(VLOOKUP( Range(""C""" & i & "), Sheet2!$A:$A, 1, FALSE), IFERROR(VLOOKUP(Range (""B""" & i - 1 & "), Sheet2!$A:$A, 1, FALSE), ""DNE""))"

Within Column B of Sheet1, I am checking the corresponding cell value in column C by looking it up in column A of Sheet2. If it does not find it, it tries to look up the previous cell B value, if that gives an error, it goes to input "DNE" in cell B.

VBA Code:
Option Explicit

Public Sub Frmula()

Dim i As Long

Sheets("Sheet1").Activate

                    For i = 8 To 3000
                               Range("B" & i).Formula = "= IFERROR(VLOOKUP( Range(""C""" & i & "), Sheet2!$A:$A, 1, FALSE), IFERROR(VLOOKUP(Range (""B""" & i - 1 & "), Sheet2!$A:$A, 1, FALSE), ""DNE""))"
                    Next i


End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try:

VBA Code:
Public Sub Frmula()

Sheets("Sheet1").Range("B8:B3000.Formula = "=IFERROR(VLOOKUP(C3,Sheet2!$A:$A, 1, FALSE), IFERROR(VLOOKUP(B2,Sheet2!$A:$A, 1, FALSE), ""DNE""))"

End Sub
 
Upvote 0
Try:

VBA Code:
Public Sub Frmula()

Sheets("Sheet1").Range("B8:B3000.Formula = "=IFERROR(VLOOKUP(C3,Sheet2!$A:$A, 1, FALSE), IFERROR(VLOOKUP(B2,Sheet2!$A:$A, 1, FALSE), ""DNE""))"

End Sub
Thanks @RoryA as that worked for the most part. I just made a few minor changes to include the "), and as I working with rows i which started at 8 and then I also was using (i - 1) which would mean row 7. This was perfect because I don't have to loop it. I was looking for this method in old posts that I made and on the internet. I knew I saw this method previously, but I could not find it. I will post the modification so I can mark it as the correct one.

(1) changed the first part to include " ):
Sheets("Sheet1").Range("B8:B3000.Formula =

to
Sheets("Sheet1").Range("B8:B3000").Formula =

(2) changed this part of the (the C3 to C8):
VBA Code:
IFERROR(VLOOKUP(C3,Sheet2!$A:$A, 1, FALSE)

to:
VBA Code:
IFERROR(VLOOKUP(C8,Sheet2!$A:$A, 1, FALSE)

(3) changed this part of the code (the B2 to B7):
VBA Code:
IFERROR(VLOOKUP(B2,Sheet2!$A:$A, 1, FALSE), ""DNE""))"

to:
VBA Code:
IFERROR(VLOOKUP(B7,Sheet2!$A:$A, 1, FALSE), ""DNE""))"

Final code is as follows:
VBA Code:
Option Explicit

Public Sub Frmula()

Sheets("Sheet1").Range("B8:B3000").Formula = "=IFERROR(VLOOKUP(C8,Sheet2!$A:$A, 1, FALSE), IFERROR(VLOOKUP(B7,Sheet2!$A:$A, 1, FALSE), ""DNE""))"

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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