+ Reply to Thread
Results 1 to 11 of 11

Vlookup not working

Hybrid View

  1. #1
    Registered User
    Join Date
    04-17-2014
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Unhappy Vlookup not working

    I have a workbook with multiple worksheets and a user form that has two combo boxes, the first one fills from a named range and when a user selects a value from this combo box it fills another combo box. When a user selects a value from this combo box I need Vlookup to find the value in a named range and column 1 (left) is the same column that fills the combo box so the value that is being searched for has to be identical to the value found but the result always comes back empty?

    I'm passing two variables to Vlookup - the search name (from the selected combo box) and an integer for the column offset. The first sub is activated when the user clicks button CmdDetails on the form.

    Here is the code
    Private Sub CmdDetails_Click()
         
        Dim Name As String
        Dim ColoffSet As Integer
        
           Name = CmbArea.Value
           ColoffSet = 5
               
          Call AreaChange(Name, ColoffSet)
            
       
         
    End Sub
    [/CODE]

    Private Sub AreaChange(Name, ColoffSet)

    Dim R As Range
    Dim T As Range
    Dim Result As Variant
    On Error Resume Next
    Err.Clear
    For Each R In Range("Table")
    Result = Application.WorksheetFunction.VLookup(Name, R, ColoffSet, False)
    T.AddItem Result

    'If Err.Number = 0 Then (I took the error handling out because the vbYesNoCancel button won't cancel and I have to end the program to get out of it using Ctrl Alt Delete, but if I leave it in it says value not found)
    'Else
    'MsgBox ("result not found"), vbYesNoCancel

    'End If
    Next R

    Call AddRoom(T)

    End Sub
    [/CODE]

    'Forget the call to AddRoom... I don't get that far!

    If I toggle a breakpoint on the For each line I can see that the name value is being passed to the function (hovering the mouse over the variables) and when it cycles through the table R (named range) I can see that the value is found but not passed to the result variable which remains empty.

    Any ideas?
    Last edited by hallofus; 04-30-2014 at 06:01 AM. Reason: to make the code easier to read

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,287

    Re: Vlookup not working

    Please put code tags around your code excerpt.

    Can you post a sample workbook?

    I'd hazard a guess that this:

    For Each R In Range("Table")
    Result = Application.WorksheetFunction.VLookup(Name, R, ColoffSet, False)
    T.AddItem Result
    should be this:

    Result = Application.WorksheetFunction.VLookup(Name, Range("Table"), ColoffSet, False)
    T.AddItem Result
    and lose the Next R loop.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    04-17-2014
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Vlookup not working

    Thanks TMS, I used the named range name R initially and only changed it to Range("Table") to see if that was where I was going wrong so it's not that but I can send the workbook if you like

  4. #4
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Vlookup not working

    Welcome to the forum.

    We'd like to help you but first..

    Pls take some minutes to read forum rules and specially-in this case- rule#3 and add code tags around your code.
    Then anyone will be able to help you.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,287

    Re: Vlookup not working

    Please fix the code tags and yes, please upload a workbook

  6. #6
    Registered User
    Join Date
    04-17-2014
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Vlookup not working

    code tags fixed. I've removed all of the excess worksheets and just left in sheet 1 (items) and sheet L9. use the vba editor, click on the form frmMain click show object (right click) and then run the form but select only level 9 from the first combo box otherwise the code will fail looking for worksheets and named ranges that I've just removed.

    Regards,

    Simon (grateful)
    Attached Files Attached Files
    Last edited by hallofus; 04-30-2014 at 07:05 AM. Reason: changing the column offset to be within the range

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Vlookup not working

    The ColOffset = 5 will reference area outside of named range Table, which is only 4 columns wide.

    Not sure why you have for each loop, as that will loop through each individual cell in range Table.
    As suggested I think you just need to use the named range.

    Set R = Range("Table")
       Result = Application.WorksheetFunction.VLookup(Name, R, ColoffSet, False)
    Again not sure what T.additem is show you will have to work out what you want to do with result
    Cheers
    Andy
    www.andypope.info

  8. #8
    Registered User
    Join Date
    04-17-2014
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Vlookup not working

    Thanks Andy... I should have changed the column offset integer when I cleaned up the workbook and uploaded it for scrutiny. I removed several columns and worksheets and now I've uploaded the same file again with the column offset changed to 2. Also, I need the for next loop to pick up every instance of the search name in the column as in reality it repeats several times but the offset column provides a different answer every time i.e. several pieces of equipment can be in the one room and several rooms can be in the location being search for. T.additem allows the answer (if found) to be added to a range that is then used to fill another combo box in another sub but as I said I don't get that far because result is always empty even though a match is found.

    Thanks for all of your help... any suggestions as to why I'm not getting a result from the vlookup?

    Regards,

    Simon

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Vlookup not working

    VLOOKUP will only return 1 match, so not best suited for looking up multiple matches.

    This will loop through each row in the table and display matches. I still think you ColOffset is incorrect as it currently returns the Code rather than Room

    Dim Result As Variant
    For Each R In Range("Table").Rows
       Result = Application.VLookup(Name, R, ColoffSet, False)
       If Not IsError(Result) Then
            MsgBox Result
        End If
    Next R
    I don't see how you are defining range T so just went with message box.

  10. #10
    Registered User
    Join Date
    04-17-2014
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Vlookup not working

    Thanks again Andy, although when I run this code result = error 2042 instead of the column value. In my case the column offset doesn't matter if it returns code instead of room... I can change that later and will have to for different values that I am looking for whether it be code or room as in this case. I know Vlookup will only return 1 match but there will be many matches in the column and therefore different codes or rooms will fill the Range T. Any idea why it still doesn't give me back a value.. this if the real crux of the matter and the reason for needing the help. If you step through the loop you will see that the value being searched for is found but the result variable is still not being returned.

    Regards,

    Simon

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Vlookup not working

    In the example select 9, then Area 3. As I said msgbox will display result.

    Again I don't see how VLOOKUP will return all matches if Function Area contains multiple "Area 3" items, unless you process a single row at a time.
    And T does not make sense. How can you .Additem to a range?
    Attached Files Attached Files

+ 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. [SOLVED] VLOOKUP not working
    By x65140 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-10-2013, 09:43 AM
  2. [SOLVED] Vlookup not working
    By excellearner121 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-04-2013, 08:57 PM
  3. [SOLVED] Vlookup is not working and giving =vlookup(B2,$T$2:$U$135,2,false) this kind of values.
    By yogeshsharma1981 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-09-2013, 04:08 PM
  4. Vlookup not working
    By vamshi57 in forum Excel General
    Replies: 8
    Last Post: 01-28-2010, 07:42 AM
  5. VLOOKUP Value Not Working
    By Dunda in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-17-2009, 03:10 PM

Tags for this Thread

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