Your question, your audience. Choose who sees your identityâ€”and your questionâ€”with question security.

I am trying to convert the following double lookup formula to VB with no success.

=if(A?="","",vlookup(A?,ProdData,Match(E?,ProcessTimes!B1:FQ1,0),TRUE))

The formula works if I put it in the cells in column G but I can't get VB to do so.

The wildcard ? is a variable row number. ProdData is a named range in the worksheet ProcessTimes. Here is the code I was trying to use but have tried many different variations with no success:

For Cntr2 = 2 To lastRow

Range("G" & Cntr2).Formula = "=IF(A"&Cntr2&"="""","""",VLOOKUP(A"&Cntr2&",ProcessTimes,MATCH(E"&Cntr2"&,ProcessTimes!B1:FQ1,0),TRUE))"

Next Cntr2

=if(A?="","",vlookup(A?,Pr

The formula works if I put it in the cells in column G but I can't get VB to do so.

The wildcard ? is a variable row number. ProdData is a named range in the worksheet ProcessTimes. Here is the code I was trying to use but have tried many different variations with no success:

For Cntr2 = 2 To lastRow

Range("G" & Cntr2).Formula = "=IF(A"&Cntr2&"="""","""",

Next Cntr2

You have:

Range("G" & Cntr2).Formula = "=IF(A"&Cntr2&"="""","""",

It should be

Range("G" & Cntr2).Formula = "=IF(A"&Cntr2&"="""","""",

"="""","""",VLOOKUP(A"

and the statement: Expected: end of statement

lastrow should always have to maximum rows in the worksheet. to do this you need to have lastrow updated in the beginning of the sub or after you add rows. You choose a column that have the most data downward so you pickup the maximum row (sometimes it is not always Col A that have all the data downward sometimes it is G or F ... whatever) for the example I will use G as I see your past formula use G so it is safe to use G

So just before the Fro to Loop I would have it this way:

lastrow = Range("G" & Rows.count).End(XlUp).Row

For Cntr2 = 2 To lastRow

...

Next Cntr2

This should make sure your lastrow is always updated with the maximum rows in the worksheet all the time.

I would recommend when using VBA to always recourse to explicit variable naming and try to avoid as much as possible implicit variables (using Range("A1") = "abc" is implicit as it assumes that Range("A1") of the activesheet, whereas Explicit would be:

Dim WS as worksheet

Set WS = Sheets("Sheet2")

WS.Range("A1")="abc"

this way you are always sure regardless where the activesheet is that A1 will be updated with abc string in sheet2 whereas in the first example you and your luck when the code hits Range("A1") if the activesheet is Sheet10 then A1 of sheet10 will hold value of "abc"

for this case is no big deal but extend this to variable like the one you have and you may be messing up your whole workbook simply due to a sheet getting focus by accident !!

so finally your formula should look like:

WS.Range("G" & Cntr2).Formula = "=IF(A" & Cntr2 & "="""","""",VLOOKUP(A" & Cntr2 & ",ProdData,MATCH(E" & Cntr2 & ",ProcessTimes!B1:FQ1,0),T

gowflow

Guessed you had the value for lastrow already, since the For..Next loop used it.

Just use the formula with R1C1 notation to insert all formulas at once, it will work.

R1C1 is how Excel works with formulas, references are relative, so RC[-6] means same row, and 6 columns to the left. In column G (column 7), 6 rows to the left is column A.

In the last part of the formula, ProcessTimes!R1C2:R1C173, means fixed range $B$1:$FQ$1.

R1C1 notation is probably not as easy to read, but is quite flexible.

The first part of the statement Range("G2:G" & lastrow) defines where to insert the formulas, from G2 down to the row defined by lastrow.

You can't use column G to find the last row, since the cells are probably empty, but since column A is used in the formula, the statement

lastrow = Range("A" & Rows.count).End(XlUp).Row

will find the last row.

The statement

```
Range("G2:G" & lastrow).FormulaR1C1 = "=IF(RC[-6]="""","""",VLOOKUP(RC[-6],Proddata,MATCH(RC[-2],ProcessTimes!R1C2:R1C173,0),TRUE))"
```

does exactly the same as the loop```
For Cntr2=2 to lastrow
Range("G" & Cntr2).Formula = "=IF(A" & Cntr2 & "="""","""",VLOOKUP(A" & Cntr2 & ",ProdData,MATCH(E" & Cntr2 & ",ProcessTimes!B1:FQ1,0),TRUE))"
Next Cntr2
```

So the code generates the formula perfectly. However, I get a #REF error in the cells with the formula. I find that if I go to the ProcessTimes worksheet, delete the named range ProdData and then recreate it exactly as before, the formulas all work.

Should I delete the named range ProdData and recreate it every time I run the macro? That seems crude.

Also, I recorded a Macro to see how the Range naming code works and got the code below. However, it does not give me the code for a situation where the ProdData table may be a different size every time I run the macro.

Sub NameRange()

'

' NameRange Macro

'

Range("B1").Select

Range(Selection, ActiveCell.SpecialCells(xl

ActiveWorkbook.Names.Add Name:="VistaData", RefersToR1C1:= _

"=ProcessTimes!R1C2:R735C1

End Sub

ActiveWorkbook.Names.Add Name:="ProdData", RefersToR1C1:= _ "=ProcessTimes!R1C2:" & "R" & Cells(Rows.Count, "B").End(xlUp).row & "C" & Cells(1, Columns.Count).End(xlToLef

I found that the RC code was ignoring the "Process Times" location definition and was looking in the active worksheet, which was not ProcessTimes. When I added a line to make ProcessTimes the active worksheet, it worked like a charm.

Ref error means that the formula can't find the column for the lookup, because the range does not have enough columns.

The definition of ProdData indicate that the match function looks in the first line of ProdData, so I think you should change

ProcessTimes!R1C2:R1C173 (= B1:FQ1) to Index(ProdData,1,).

Then it will update when ProdData is updated to the new range.

When the Index function only has row specification (1), and not a column specification, it returns the entire first row of ProdData, not a specific cell.

If the ProdData table is imported from a csv or txt file, it should not be needed to redefine the range at every update, Excel should do that automatically.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Another error (does not prevent compiling) is that the first ProcessTimes must be replaced with ProdData to match the original formula.

ProcessTimes is a sheet, not a named range.

Range("G" & Cntr2).Formula = "=IF(A" & Cntr2 & "="""","""",VLOOKUP(A" & Cntr2 & ",ProdData,MATCH(E" & Cntr2 & ",ProcessTimes!B1:FQ1,0),T

Inserting the formula row by row can take a lot of time.

It is more efficient to us R1C1 notation and insert all formulas in one statement, like this.

Range("G2:G" & lastrow).FormulaR1C1 = "=IF(RC[-6]="""","""",VLOO

It does the same as the For..Next loop.