Search code examples
excelvbavlookupworksheet-function

Vlookup macro doesn't return all values needed but the values are input to pull from


I'm getting stumped, my Vlookup is messed up and I've tried to change all values in a column to text and all values to general. Here is my code, I can do the vlookups seperately but I need them to do one after another and when the first vlookup formula comes back with #N/A or #NAME or #VALUE I need the 2nd vlookup formula to come through and fill the fields appropraitely, any suggestions, I've tried so many different variations. Here is what I have now but the error says expected list seperator or (, if I take of the IFERROR and the 2nd function all number values work because most brand names can be determined by serial numbers 4th and 5th digit, but some serials have letters in place and this is the whole issue, some even have a letter and a dash, A- is an example, I need to be able to VLOOKUP all of these types but not replace the fields that have accurate values, so the first vlookup replaces all the 2nd comes in to fill the errored fiels. Help will be much appreciated.

This is the code that reproduces errors.

Sub Work2()
Dim LR As Long
Dim row As String
row = "W2"
row = "W" & x
LR = Range("W" & Rows.Count).End(xlUp).row
Range(Columns(6), Columns(7)).Select
For x = 0 To 2
row = "W" & x
Range("A2:A" & LR).Formula = "=IFERROR(VLOOKUP(VALUE(" & row &  "),[gpic.xslx]Sheet1!$R:$S,2,FALSE),"=VLOOKUP(" & row & ",[gpic.xslx]Sheet1!$R:$S,2,FALSE)")"


Next


End Sub

This is the code that can do all the number Vlookup fields but cant do the letters right I tried to fix it with the above code, but like I said it produces an error

Sub Work2()
Dim LR As Long
Dim row As String
row = "W2"
row = "W" & x
LR = Range("W" & Rows.Count).End(xlUp).row
Range(Columns(6), Columns(7)).Select
For x = 0 To 2
row = "W" & x
Range("A2:A" & LR).Formula = "=VLOOKUP(VALUE(" & row & "),[gpic.xslx]Sheet1!$R:$S,2,FALSE)"

Next




End Sub

For added info, the R and S in the worksheet I look up from have all the codes that equal the brand so the really I would only get right results or #VALUE I believe.

Column R Column S
1        Ford
2        Dodge
AE       Hyundai

That is how the vlookups work


Solution

  •     A      B        C        D        E       F
        1     Adam      1      Adam
        2     Peter     3      John
        3     John      4      Phil
        4     Phil      6      #N/A
    
        A      B        C        D        E       F 
        1     Adam      1      Adam       6      Peter
        2     Peter     3      John
        3     John      4      Phil
        4     Phil      6      Peter
    

    Cell D1 Contains the formula: =IF(ISERROR(INDEX(B:B,MATCH(C1,A:A,0))),INDEX(F:F,MATCH(C1,E:E,0)),INDEX(B:B,MATCH(C1,A:A,0))) Copy it down.