VBA question vlookup with two criterias

MarioMagnus

New Member
Joined
Jul 8, 2021
Messages
14
Office Version
  1. 2019
Platform
  1. MacOS
Hi all,
I need help with VBA and to find a value from the sheet, when two criteria are met.
I have a sheet1 build up like structured in such a way that the name is in column A, the date in column B and the calculated total time in column G. There are more than a hundred lines of it. The name can appear several times in column A and of course the date can also appear several times.
What only happens once, however, is that the combination of name and date can only occur once.

Example:
Max 10.10.2020 9.5
Max 11.10.2020 11
Mike 11.10.2020 8.75
Mike 10.10.2020 8.5

I now have a second worksheet, where the name is entered dynamically and also a date. Now I have to find the exact combination from sheet 1, and it has to be written to me in sheet 2.

How can I do something like that - I lack any logic, because with VLOOKUP I can only use one variable.
Is it possible to run VLOOKUP or something similar until the name and then the appropriate date are found?

Thanks and regards, Mario.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this:

Dante Amor
ABC
1
2Max10/10/20209.5
3
Sheet2
Cell Formulas
RangeFormula
C2C2=SUMPRODUCT((Sheet1!A2:A20=A2)*(Sheet1!B2:B20=B2)*(Sheet1!C2:C20))


Or this
Dante Amor
ABC
1
2max10/10/20209.5
Sheet2
Cell Formulas
RangeFormula
C2C2=IFERROR(LOOKUP(2,1/((Sheet1!A2:A20=A2)*(Sheet1!B2:B20=B2)),Sheet1!C2:C20), "Not exists")
 
Upvote 0
Hi Dante,
thanks for the reply. How can I attach a sample file, because when I try your formula, I get as a result 0 but it must be 0.5 for example.
Regards, Mario.
 
Upvote 0
MarioMagnus.xlsx
ABC
1Zeiten
2
3
4NameDatumZeit gesamt
5Dauer dezimal
6Noémie18/02/20216.50
7Yanik18/02/20216.50
8Noémie22/02/20214.25
9Yanik22/02/20214.25
10Noémie03/03/20215.50
11Yanik03/03/20215.50
12Noémie17/03/20216.00
13Yanik17/03/20216.00
14Noémie19/03/20212.25
15Yanik19/03/20213.25
16Noémie24/03/20216.75
17Yanik24/03/20216.00
18Noémie25/03/202111.00
Zeiten
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C6:C1048576Cell Value>13textNO


MarioMagnus.xlsx
ABC
1NameDatumZeit
2Yanik2/18/210.00
3
Sheet3
Cell Formulas
RangeFormula
C2C2=SUMPRODUCT((Zeiten!A5:A200=A2)*(Zeiten!B5:B200=B2)*(Zeiten!G5:G200))
 
Upvote 0
Hi,
I found a solution, that gives me the right value return.

Excel Formula:
{=INDEX(Zeiten!G5:G200,MATCH(1,(AG5=Zeiten!$A$5:$A$200) * (AG6=Zeiten!$B$5:$B$200),0))}

How can I use this formula within excel VBA?
Thanks, Mario.
 
Upvote 0
Check the date format so that you have in both sheets dd / mm / yyyy


varios 09jul2021.xlsm
ABC
1NameDatumZeit
2Yanik18/02/20216.5
Sheet2a
Cell Formulas
RangeFormula
C2C2=IFERROR(LOOKUP(2,1/((Zeiten!A6:A20=A2)*(Zeiten!B6:B20=B2)),Zeiten!C6:C20), "Not exists")
 
Upvote 0
How can I use this formula within excel VBA?
Try this:

VBA Code:
Sub Macro1()
  Sheets("Sheet3").Range("E2").FormulaArray = _
    "=INDEX(Zeiten!G5:G200,MATCH(1,(AG5=Zeiten!$A$5:$A$200) * (AG6=Zeiten!$B$5:$B$200),0))"
End Sub
 
Upvote 0
Hi Dante,
can you have a look to my code?
When I try to start the macro, I get the error message: "There is a problem with this formula. Not trying to type a formula..."

VBA Code:
Private Sub CommandButton_Druck_Click()

    Dim vorlageWs As Worksheet, kinderWs As Worksheet, zeitenWs As Worksheet
    Dim saveLocation As String
    Dim x As Long, y As Long
    Dim intDaysInMonth As Integer
    Dim i As Integer
   
    Dim NachName As Range, VorName As Range
    Dim vNameLastRow As Long, vDateLastRow As Long, vTimeLastRow As Long
    Dim vNameRng As Range, vDateRng As Range, vTimeRng As Range
    Dim xName As String, zelleName As Range
    
   
    Set vorlageWs = Worksheets("Vorlage")
    Set kinderWs = Worksheets("KInder")
    Set zeitenWs = Worksheets("Zeiten")

    Set NachName = kinderWs.Range("A5").CurrentRegion.Columns(2)
    vNameLastRow = zeitenWs.Range("A" & Rows.Count).End(xlUp).Row
    vDateLastRow = zeitenWs.Range("B" & Rows.Count).End(xlUp).Row
    vTimeLastRow = zeitenWs.Range("G" & Rows.Count).End(xlUp).Row
 
    Set vNameRng = zeitenWs.Range("A6:A" & vNameLastRow)
    Set vDateRng = zeitenWs.Range("B6:B" & vDateLastRow)
    Set vTimeRng = zeitenWs.Range("G6:G" & vDateLastRow)
 
    x = 6
    xName = ComboBox_Familie.Value

    For Each zelleName In NachName.Cells
        If zelleName.Value = xName Then
           vorlageWs.Cells(x, 6).Value = zelleName.Offset(0, -1).Value
           x = x + 1
        End If
    Next zelleName
 
    vorlageWs.Cells(4, 6).Value = ComboBox_Familie.Value
    vorlageWs.Cells(2, 6).Value = ComboBox_Monat.Value
    
    'Schleife für die Eingabe der Werte basierend auf den Namen und das Datum
    For y = 20 To 50
        vorlageWs.Range("A" & y).FormulaArray = "=IFERROR(INDEX(vTimeRng,MATCH(1,(vorlageWs.Range('F6')=vNameRng) * (vorlageWs.Range('A' & y)=vDateRng),0)),"")"
        y = y + 1
    Next y
    
    'hier beginnt der bereich für die Druckfunktion
    saveLocation = "/Users/mario.mueller/Dropbox/Tagesfamilie/Verrechnungen/Stundenblatt_" & vorlageWs.Cells(2, 6) & "_" & vorlageWs.Cells(4, 6)
    
    Application.Wait (Now + TimeValue("0:00:01"))
    
'    With vorlageWs.PageSetup
'        .Orientation = xlPortrait
'        .PrintArea = "A1:AB53"
'        .Zoom = False
'        .FitToPagesTall = 1
'        .FitToPagesWide = 1
'    End With
'
'    vorlageWs.ExportAsFixedFormat _
'        Type:=xlTypePDF, _
'        FileName:=saveLocation, _
'        OpenAfterPublish:=True

    Unload Me
    
    Worksheets("Info").Activate
    
End Sub

I hope you can help me with this error.
Thanks, Mario.
 
Upvote 0
I'm not sure where you are going to put the formula.
But in your macro you are putting the formula in column A and also in that same column A you are taking the date.

With the following I hope you can adapt it.
Change "E" for the column where you want the formula.
"F6" for the cell where you have the name.
"A" for the column where you have the dates.

VBA Code:
    For y = 20 To 50
      vorlageWs.Range("E" & y).FormulaArray = _
        "=IFERROR(INDEX(" & vTimeRng.Address(1, 1, xlA1, 1) & _
        ",MATCH(1,(F6=" & vNameRng.Address(1, 1, xlA1, 1) & _
        ") * (A" & y & "=" & vDateRng.Address(1, 1, xlA1, 1) & "),0)),"""")"
    Next y
 
Upvote 0
Solution

Forum statistics

Threads
1,216,590
Messages
6,131,610
Members
449,657
Latest member
Timber5

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top