+ Reply to Thread
Results 1 to 6 of 6

Thread: VBA: Match in Vlookup

  1. #1
    Registered User
    Join Date
    02-09-2010
    Location
    Denmark
    MS-Off Ver
    MS Office 2007 Enterprice
    Posts
    15

    VBA: Match in Vlookup

    Hi excel experts.

    I've googled my issue alot, and found some possible solutions, but I cannot get anyone of them to work with my workbook.
    (Perhabs due to a lack of competance)

    Anyway, In my workbook I have 2 sheets "Datagrundlag" and "Meddelelser".

    What I would like to achive is, to pull the Tid1, Tid2 and Tid3 data into the "Datagrundlag" sheet, where a match is found with date and name field like this:
    A | B | C | D | E |
    01-10-2010 | Person 1 | 1510 | 6768 | 36 |

    Workbook is enclosed

    Thanks alot in advance.

    Regards,
    Scifo
    Attached Files Attached Files
    Last edited by scifo; 11-10-2010 at 02:26 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: VBA: Match in Vlookup

    Here is one way.

    First change the information in Meddelelser column C to be dates rather than text. You can always format the dates to have - rather than / separators.

    The use a SUMPRODUCT formula to indentify matches. This assumes no duplication of date and person.

    Finally use the resulting row number in INDEX formula.
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    02-09-2010
    Location
    Denmark
    MS-Off Ver
    MS Office 2007 Enterprice
    Posts
    15

    Re: VBA: Match in Vlookup

    Hi Andy.

    Thank you, this works exactly as I want.

    I was however hoping for a VBA solution.

    Are you perhaps able to rewrite the function so it calculates in VBA and puts the value into the fields?

    I can write the loop myself, but I am uncertain on how to write the SUMPRODUCT formula in VBA.

    thx.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: VBA: Match in Vlookup

    If you want it in code then there are other approaches.

    First off the SUMPRODUCT approach. There is information here but to get around the limitations the Evaluate method is needed.
    http://www.xldynamic.com/source/xld.SUMPRODUCT.html#vba

    You could just use the formular1c1 property.

    activecell.Formular1c1 = "
    =SUMPRODUCT((Meddelelser!R5C3:R122C3=Datagrundlag!RC[-2])*(Meddelelser!R5C4:R122C4=Datagrundlag!RC[-1])*ROW(Meddelelser!R5C4:R122C4))"
    This will provide the Row number, as per example file, which you can then use to transfer data across. You can even overwrite the cell used for the formula.

    Or you could use a FIND approach. Find the person and for each find check the date.

    Or you could use autofilter.

    Or simply two loops.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    02-09-2010
    Location
    Denmark
    MS-Off Ver
    MS Office 2007 Enterprice
    Posts
    15

    Re: VBA: Match in Vlookup

    I've made a loop that inserts the desired values in the correct cells.

    However, the cells shows #NAME? instead of the value, but if I enter a cell, and just press enter (without changing the formula) the cells shows the desired values.
    Why is that?

    Sub Assemble_data_from_medd()
    Dim NumOfRows_data, NumOfRows_medd, NumOfRows_frav As Integer
    Dim Counter_row, OffsetNum As Integer
    
    Application.ScreenUpdating = False
    
    ' Get number of values from meddelelser, fravær and datagrundlag
    NumOfRows_data = Application.CountA(Range("A:A"))
    NumOfRows_medd = Application.CountA(Sheets("Meddelelser").Range("C:C")) + 3
    NumOfRows_frav = Application.CountA(Sheets("Fravær").Range("C:C"))
    OffsetNum = "1"
    
    '
    For Counter_row = 2 To NumOfRows_data + 1
        Range("C1").Offset(OffsetNum, 0).FormulaR1C1 = "=SUMPRODUKT((Datagrundlag!R" & Counter_row & "C1=Meddelelser!R5C3:R" & NumOfRows_medd & "C3)*(Datagrundlag!R" & Counter_row & "C2=Meddelelser!R5C4:R" & NumOfRows_medd & "C4)*(Meddelelser!R5C5:R" & NumOfRows_medd & "C5))"
        OffsetNum = OffsetNum + 1
    Next Counter_row
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by scifo; 11-10-2010 at 11:42 AM.

  6. #6
    Registered User
    Join Date
    02-09-2010
    Location
    Denmark
    MS-Off Ver
    MS Office 2007 Enterprice
    Posts
    15

    Re: VBA: Match in Vlookup

    I solved the last problem myself.

    As you know, the formulas in excel are language-specific, so I need to type the danish name for the function, if I insert it manually into a cell.

    But if I insert it from VBA, I need to insert the english name for the function, which automaticly gets converted after insertion.

    And now I get the results.

    You might already know this Andy, but I wrote my solution so that others may benefit from it, if they encounter the same problem.

    Thanks again for your help, and I will set the status for this thread to SOLVED

    //Scifo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0