Link to home
Start Free TrialLog in
Avatar of Milind Agarwal
Milind AgarwalFlag for United States of America

asked on

A macro that would look up the a column in a worksheet1 to populate the column of a worksheet2

Hello Experts,

Looking for a Macro/Formula that would look up the column A of the sheet 1 and populate the column B of sheet 2 . If it is not present in the Sheet1 it would just populate itself. Please find the attached workbook for example. The highlighted column in the sheet2  needs to be populated.
Book1.xlsx
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi Milind,

Please find attached as requested:
VLook-up-for-Alphabets.xlsm
Avatar of Milind Agarwal

ASKER

Hi Shums,

Thanks for the reply but it should be the other way around. When the sheet1 data is present I would need to populate the sheet2 column B.
 
Thanks,
Milind
Could u also post the code, I would have to incorporate this in to another code.

Thanks,
I don't understand Sheet 2 is already updated with the descriptions.

Here is the code:
Private Sub Update_Click()
Dim Ws As Worksheet
Dim LR As Long
Application.ScreenUpdating = False
Call MakeUniqueList
Set Ws = Worksheets("Sheet1") 'Change the sheet name as required
LR = Ws.Range("A" & Rows.Count).End(xlUp).Row 'Change the column, which has the maximum number of rows filled
Ws.Range("B1:B" & LR).FormulaR1C1 = "=IFERROR(VLOOKUP(RC1,Sheet2!C1:C2,2,0),"""")"
Ws.Range("B1:B" & LR).Value = Ws.Range("B1:B" & LR).Value
Ws.Range("B1").Select
Application.ScreenUpdating = True
End Sub
Sub MakeUniqueList()
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Dim LR As Long
    Dim vaData As Variant
    Dim colUnique As Collection
    Dim aOutput() As Variant
    Dim i As Long
    
    Set Ws1 = Worksheets("Sheet2")
    Set Ws2 = Worksheets("Sheet1")
    LR = Ws1.Range("A" & Rows.Count).End(xlUp).Row
    
    'Put the data in an array
    vaData = Ws1.Range("A1:A" & LR).Value

    'Create a new collection
    Set colUnique = New Collection

    'Loop through the data
    For i = LBound(vaData, 1) To UBound(vaData, 1)
        'Collections can't have duplicate keys, so try to
        'add each item to the collection ignoring errors.
        'Only unique items will be added
        On Error Resume Next
            colUnique.Add vaData(i, 1), CStr(vaData(i, 1))
        On Error GoTo 0
    Next i

    'size an array to write out to the sheet
    ReDim aOutput(1 To colUnique.Count, 1 To 1)

    'Loop through the collection and fill the output array
    For i = 1 To colUnique.Count
        aOutput(i, 1) = colUnique.Item(i)
    Next i

    'Write the unique values to column A
    Ws2.Range("A1").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput

End Sub

Open in new window

In Sheet 1 E is not available, so in Sheet 2 it would be empty, that what you want?
Hi Shums,

I just removed the data that needs to be populated.  Please find the attached. Please let me know if you have any questions?

Thanks,
Book1.xlsx
E should be just populated as E in the sheet2 columnB
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works like charm
Glad I was able to help you again :)