+ Reply to Thread
Results 1 to 9 of 9

Lookup against two columns to return value from another column on same sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Lookup against two columns to return value from another column on same sheet

    Lookup1.xlsx

    Hi

    I have a data download from a system that is not very well designed and I cannot change (see attached).

    What I want to do is lookup to see if the value in column A of the output sheet is present in the column A or B of the input sheet if it is I would like it to return the value in column C to Colum B of the output sheet if not return "not present" in column B of the output sheet.

    JB

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Lookup against two columns to return value from another column on same sheet

    Hi batchjb69,

    here is one option for you:

    Sub Main()
    
        Dim wsIn As Worksheet: Set wsIn = ThisWorkbook.Sheets("Input")
        Dim wsOut As Worksheet: Set wsOut = ThisWorkbook.Sheets("Output")
        Dim rngIn As Range, rngOut As Range, rngFind As Range, c As Range
    
        'define input range
        With wsIn.Columns("A:B")
            Set rngIn = .Range(.Cells(1), .Find("*", , , , xlByRows, xlPrevious)).Resize(, 3)
        End With
    
        'define output range
        Set rngOut = wsOut.Range("A2", wsOut.Range("A" & Rows.Count).End(xlUp))
    
        'loop through output lookup range
        For Each c In rngOut
            Set rngFind = rngIn.Find(c.Value, , xlValues, xlWhole)
            If Not rngFind Is Nothing Then
                c(1, 2).Value = wsIn.Cells(rngFind.Row, rngIn(1, rngIn.Columns.Count).Column).Value
                Set rngFind = Nothing
            Else
                c(1, 2).Value = "not present"
            End If
        Next c
    
    End Sub
    Hope it helps,

    berlan

  3. #3
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Lookup against two columns to return value from another column on same sheet

    Lookup1.xlsm

    Many thanks saved me a load of work

    What about if the value = "not present" it looks up to a third sheet (manual list) and again would return the value in column B. If not in column B it remains "not present"

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Lookup against two columns to return value from another column on same sheet

    No need for VBA for this. Put this in B2 and copy down...
    =IFERROR(IFERROR(VLOOKUP(A2,Input!$A$2:$C$57,3,0),VLOOKUP(A2,Input!$B$2:$C$57,2,0)),"Not Present")
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Lookup against two columns to return value from another column on same sheet

    You're welcome, and thanks for the kind feedback

    To also go through a manual list, if the value is not present in the input list, try if this works for you (see attached):
    Sub Main()
    
        Dim rngIn As Range, rngOut As Range, rngManual As Range
        Dim rngFind As Range, c As Range
    
        'define input range
        With ThisWorkbook.Sheets("Input").Columns("A:B")
            Set rngIn = .Range(.Cells(1), .Find("*", , , , xlByRows, xlPrevious)).Resize(, 3)
        End With
    
        'define output range
        With ThisWorkbook.Sheets("Output")
            Set rngOut = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
        End With
    
        'define manual list
        With ThisWorkbook.Sheets("Mannual list")
            Set rngManual = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
        End With
    
        'loop through cells in output range
        For Each c In rngOut
    
            'lookup against input range
            Set rngFind = rngIn.Find(c.Value, , xlValues, xlWhole)
    
            'if found in input range, use that
            If Not rngFind Is Nothing Then
                c(1, 2).Value = Intersect(rngFind.EntireRow, rngIn.Columns(rngIn.Columns.Count)).Value
                Set rngFind = Nothing
    
                'if not found, then go through the manual list, else "not present"
            Else
                Set rngFind = rngManual.Find(c.Value, , xlValues, xlWhole)
                If Not rngFind Is Nothing Then
                    c(1, 2).Value = rngFind(1, 2).Value
                    Set rngFind = Nothing
                Else
                    c(1, 2).Value = "not present"
                End If
            End If
    
        Next c
    
        'clean up
        Set rngIn = Nothing
        Set rngOut = Nothing
        Set rngManual = Nothing
    
    End Sub
    Best,
    berlan
    Attached Files Attached Files

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Lookup against two columns to return value from another column on same sheet

    Berlan, thanks for the feedback But I think you clicked the negative rep lol...no biggie though

  7. #7
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Lookup against two columns to return value from another column on same sheet

    FDibbins, apologies! Realised it when I had pressed the button and it gave me a different response. Was meant to be positive rep added -- nice and simple solution to the problem -- as your other contributions on the forum!

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Lookup against two columns to return value from another column on same sheet

    Its the thought that counts, thanks for the kind words

  9. #9
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Lookup against two columns to return value from another column on same sheet

    Again, thanks for the rep points, that should make things square

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. lookup for a cell value in entire sheet and return value from a column
    By atulkumar.goel in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-22-2014, 01:38 PM
  2. [SOLVED] Lookup column in another sheet and return corresponding value
    By dougmorgan in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-30-2013, 06:21 AM
  3. Replies: 0
    Last Post: 01-21-2013, 10:31 AM
  4. lookup date in column, search columns to right for name, return "x"
    By roothog in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-22-2012, 03:27 AM
  5. Two Columns must match in sheet one and sheet two then lookup column C
    By Pavan Kumar in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-18-2011, 11:32 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1