+ Reply to Thread
Results 1 to 9 of 9

LookUp Code in VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-22-2012
    Location
    Spain
    MS-Off Ver
    Excel 2008
    Posts
    304

    LookUp Code in VBA

    Hi,

    Im using the following lookup code in VBA but cannot make it work:


    Range("AU" & x) = Sheet2.Application.WorksheetFunction.Lookup(Range("A" & x).Value, Range("A9:A15"), Range("F9:F15").Value)
    The thing is that:
    Range("A" & x) is located in Sheet3
    Range("A9:A15") is located in Sheet2
    Range("F9:F15") is located in Sheet2

    Thanks!

    Regards,

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,645

    Re: LookUp Code in VBA

    Try
    Sheets("Sheet2").Range("AU" & x).value = Application.WorksheetFunction.Lookup(Sheets("Sheet3").Range("A" & x), Sheets("Sheet2").Range("A9:A15"), Sheets("Sheet2").Range("F9:F15"))

  3. #3
    Forum Contributor
    Join Date
    07-22-2012
    Location
    Spain
    MS-Off Ver
    Excel 2008
    Posts
    304

    Re: LookUp Code in VBA

    Hi!

    It pops up an error also... "Unable to get the Lookup property of the WorksheetFunction class"

  4. #4
    Forum Contributor
    Join Date
    07-22-2012
    Location
    Spain
    MS-Off Ver
    Excel 2008
    Posts
    304

    Re: LookUp Code in VBA

    Hi again!

    I manage to make work it work with the code above but it displays #NA instead the values... strange...

    I have attached my workbook if it helps.

    Thanks!!

    Regards
    Attached Files Attached Files

  5. #5
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,645

    Re: LookUp Code in VBA

    Quote Originally Posted by pezalmendra View Post
    I manage to make work it work with the code above but it displays #NA instead the values... strange...
    Because your first value is range and not cell.

    Sheets("Trips Overview").Range("AU9:AU20") = Application.WorksheetFunction.LookUp(Sheets("Trips Overview").Range("A9:A20").Value, Sheets("Schedule Stops").Range("A9:A15"), Sheets("Schedule Stops").Range("X9:X15").Value)
    Check mine example above. You need to use first cell Sheets("Trips Overview").Range("AU9") and then loop to others.
    see mike's solution.

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: LookUp Code in VBA

    Also if your going to use the Lookup you need to convert your values in column A to numbers

    For i = 9 To 15
        Sheets("Trips Overview").Range("AU" & i) = _
            Application.WorksheetFunction.LookUp(Sheets("Trips Overview").Range("A" & i).Value, _
            Sheets("Schedule Stops").Range("A9:A15"), _
            Sheets("Schedule Stops").Range("X9:X15").Value)
    Next
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: LookUp Code in VBA

    Give this a try,

    Sub LookUp()
    Const shStops As String = "Schedule Stops"
    Const shTrips As String = "Trips Overview"
    Dim wsTrips As Worksheet, wsStops As Worksheet
    Dim FoundCell As Range
    Dim Ptr As Long, LastRow As Long
        
        Set wsTrips = Worksheets(shTrips)
        Set wsStops = Worksheets(shStops)
        
        LastRow = wsTrips.Cells(Rows.Count, "a").End(xlUp).Row
        With wsStops
            For Ptr = 9 To LastRow
                Set FoundCell = .Range("a9:a" & .Cells(Rows.Count, "a").End(xlUp).Row) _
                .Find(What:=wsTrips.Cells(Ptr, "a"), Lookat:=xlWhole)
                
                If Not FoundCell Is Nothing Then
                    wsTrips.Cells(Ptr, "au").NumberFormat = "[h]:mm"
                    wsTrips.Cells(Ptr, "au") = .Cells(FoundCell.Row, "x")
                End If
            Next
        End With
        Set wsTrips = Nothing
        Set wsStops = Nothing
        Set FoundCell = Nothing
    End Sub
    Last edited by mike7952; 08-30-2012 at 09:37 AM.

  8. #8
    Forum Contributor
    Join Date
    07-22-2012
    Location
    Spain
    MS-Off Ver
    Excel 2008
    Posts
    304

    Re: LookUp Code in VBA

    Yesss!! works great mike!!!
    thanks you both for the help and attention!!!

    Regardssss!!!

  9. #9
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: LookUp Code in VBA

    @ pezalmendra

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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