Vlookup table help.

Hi guys

I need some help with an Excel sheet I am working on. In the attached spreadsheet, I am trying to get the values in column B and D into columns G, H, I and J. I will explain.

In the left hand most column are printer serial numbers. These printers have readings in either black or colour. Sometimes only black though and no colour. I need to be able to lookup the serial number of a printer, determine whether i am looking for the values for black or colour and then copy the values into the associated cells.

So for serial number JWF96125, there is a reading for black. I need the value in column C (Last Reading) to then be copied into column G (mono start reading). I also need column E's reading in that instance to be copied into column H of that same row.

That same serial number though, also has a colour reading. So now the value in column C (last reading) for the associating row has to be copied into the same row in column I. Then the value in column E for that row will need to be copied into column J.

I've done an example so you can see by filling out cells G2-J2.  But i had a hard time using Vlookups due to the variations in values and really could do with your help.

Any questions, let me know. Hope it makes sense.

Thanks for helping
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Please give this a try...

Sub VlookupTable()
Dim ws As Worksheet
Dim lr As Long, i As Long
Dim x, y(), dict, it

Application.ScreenUpdating = False

Set ws = Sheets("Sheet1")
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
x = ws.Range("A2:F" & lr).Value
ReDim y(1 To lr - 1, 1 To 4)
Set dict = CreateObject("Scripting.Dictionary")

For i = 1 To UBound(x, 1)
    If Not dict.exists(x(i, 1)) Then
        If x(i, 2) = "Black" Then
            dict.Item(x(i, 1)) = x(i, 3) & "_" & x(i, 5)
        End If
        If x(i, 2) = "Colour" Then
            dict.Item(x(i, 1)) = dict.Item(x(i, 1)) & "_" & x(i, 3) & "_" & x(i, 5)
        End If
    End If
Next i
On Error Resume Next
For i = 1 To UBound(x, 1)
    If x(i, 2) = "Black" Then
        y(i, 1) = Split(dict.Item(x(i, 1)), "_")(0)
        y(i, 2) = Split(dict.Item(x(i, 1)), "_")(1)
        y(i, 3) = Split(dict.Item(x(i, 1)), "_")(2)
        y(i, 4) = Split(dict.Item(x(i, 1)), "_")(3)
    End If
Next i
ws.Range("G2:J" & lr).ClearContents
ws.Range("G2").Resize(lr - 1, 4).Value = y
ws.Range("G2:J" & lr).Borders.Color = vbBlack
Application.ScreenUpdating = True
End Sub

Open in new window

In the attached, click the button called "VLookUp" on Sheet1 to run the code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YashyAuthor Commented:
Morning Subodh. Thank you so much. I wil try this once at work in 45mins.

Are you able to explain the logic you applied,i.e before you wrote the code, the process you thought about? I assume you are using VBA and that this can't just be done using simple Vlookups?
YashyAuthor Commented:
It worked Subodh!! I ran it and it's all working. Thank you so much for your help man. I appreciate it.
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Morning Yashy!
It's difficult to explain the logic. :)
Actually I didn't try for the formulas to get the desired output. But if you want to use formulas to get the same output, you can try the attached.
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Did you try the formula version?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.