INDEX-MATCH with Duplicate Values

pcardenasm

New Member
Joined
Oct 28, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi :)
I have a code that works well for copy data to some sheets to main sheet, I realized in some sheets are duplicated values for the same date, I used an expression to count them in each sheet (column A)
I tried to use INDEX and MATCH (two criteria) with new created ranges, but it doesn't work. Or maybe i should use another function?

TEP-EjBD-F.RevB.xlsm
ABCDEFGHIJKLMN
137124/09/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,850PuntualMEN-LMA-016
138224/09/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,050CompuestaMEN-LMA-016
139324/09/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,850PuntualMEN-LMA-016
140424/09/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,0702,0501,2701,2701,9201,140Compuesta + Puntual MEN-LMA-016 + MEN-LMA-057
141101/10/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,1202,130CompuestaMEN-LMA-016
142108/10/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,0902,090CompuestaMEN-LMA-016
143115/10/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,0902,1301,8401,8502,3001,630Compuesta + Puntual MEN-LMA-016 + MEN-LMA-057
144122/10/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,0702,060CompuestaMEN-LMA-016
145222/10/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,870PuntualMEN-LMA-016
146127/10/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,1002,080CompuestaMEN-LMA-016
147104/11/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,0102,010CompuestaMEN-LMA-016
148113/11/2020Intercontrol Levante S.A.Intercontrol Levante S.A.812CompuestaMEN-LMA-016
149120/11/2020Intercontrol Levante S.A.Intercontrol Levante S.A.894CompuestaMEN-LMA-016
150124/11/2020Intercontrol Levante S.A.Intercontrol Levante S.A.889CompuestaMEN-LMA-016
151104/12/2020Intercontrol Levante S.A.Intercontrol Levante S.A.9711,230CompuestaMEN-LMA-016
152111/12/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,0501,990CompuestaMEN-LMA-016
153117/12/2020Intercontrol Levante S.A.Intercontrol Levante S.A.2,0902,0901,8501,7002,5001,250Compuesta + Puntual MEN-LMA-016 + MEN-LMA-057
154217/12/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,880PuntualMEN-LMA-016
155123/12/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,840PuntualMEN-LMA-016
156223/12/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,9302,020CompuestaMEN-LMA-016
Conductividad
Cell Formulas
RangeFormula
A137:A156A137=COUNTIF($B$12:B137,B137)


VBA Code:
Option Explicit
Sub CopyValues()
    Dim cont As Long
    Dim ultlinea As Long
    Dim fecha As Date
    Dim muestras As Variant
    Dim analisis As Variant
    Dim parametro As Variant
    Dim tipo As Variant
    Dim metodo As Variant
    Dim influente As Variant
    Dim MEnt As Variant
    Dim MEntf As Variant
    Dim MA As Variant
    Dim MAf As Variant
    Dim MB As Variant
    Dim MBf As Variant
    Dim MC As Variant
    Dim MCf As Variant
    Dim ML As Variant
    Dim MLf As Variant
    Dim m As Long
    Dim y As Long
    Dim EfluentTerc As Variant                    'Cuando puede ser texto o número
    Dim EfluentTercF As Variant
    Dim rango As Variant
    Dim fila As Long                              '<- added: wasn't declared
    Dim myArray As Variant                        '<- added: list of sheets with data to be copied (ordered)
    Dim shtName As Variant                        '<- added
    Dim firstRow As Long                          '<- added2

    Dim MAf2 As Variant
    
    Dim contador As Long

    Dim rngfecha As Range           'added dic23
    Dim rngcont As Range            'added dic23
    Dim rngmuest As Range           'added dic23

    Application.ScreenUpdating = False            '<- added: avoids screen flickering
    firstRow = 3                                  '<- added2
    ultlinea = sheets("BD").Range("A" & Rows.Count).End(xlUp).Row '<- added2
    If ultlinea < 3 Then ultlinea = 3             '<- added2
    sheets("BD").Range("A3:O" & ultlinea).ClearContents '<- added2
    myArray = Array("pH", "Temperatura", "Conductividad", "Oxígeno", "Turbidez", "Sólidos en suspensión", "Sólidos Susp. Volatiles", "DQO", "DBO5", "Nitrógeno", "Fósforo", "Ortofosfatos")
    For Each shtName In myArray                   '<- added: loop on all sheets listed in myArray
        '<- changed: from here to end changed all "Fósforo" to shtName
        sheets(shtName).Select
        'Range("B12").Select
        Range(Range("A12:B12"), Range("A12:B12").End(xlDown)).Copy '<- changed2
        'Sheets("BD").Select
        'Range("A" & firstRow).Select
        'Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        sheets("BD").Range("A" & firstRow).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '<- changed2
        'Range("A3").Select
        'Sheets(shtName).Select
        'Application.CutCopyMode = False
        'Range("B12").Select
        ultlinea = sheets("BD").Range("A" & Rows.Count).End(xlUp).Row
        fila = sheets(shtName).Range("B12").End(xlDown).Row 'última fila de la hoja a copiar
        
        Set rango = sheets(shtName).Range("B12:N" & fila) 'define el rango de la hoja a copiar
        
        Set rngcont = sheets(shtName).Range("A12:A" & fila)     'added dic23
        Set rngfecha = sheets(shtName).Range("B12:B" & fila)    'added dic23
        Set rngmuest = sheets(shtName).Range("C12:C" & fila)    'added dic23
        
        For cont = firstRow To ultlinea           '<- changed2
            contador = sheets("BD").Cells(cont, 1)              'added dic23
            fecha = sheets("BD").Cells(cont, 2)
            m = Month(fecha)
            y = Year(fecha)
            muestras = Application.Index(rngmuest, Application.Match(contador & CLng(CDate(fecha)), rngcont & rngfecha, 0)) 'it doesn't work
            
            'i want to do the same for these expressions
            analisis = Application.VLookup(CLng(CDate(fecha)), rango, 3, False)
            parametro = sheets(shtName).Name
            tipo = Application.VLookup(CLng(CDate(fecha)), rango, 12, False)
            metodo = Application.VLookup(CLng(CDate(fecha)), rango, 13, False)
            EfluentTerc = Application.VLookup(CLng(CDate(fecha)), rango, 5, False)
            If Application.WorksheetFunction.IsText(EfluentTerc) Then
                EfluentTercF = Mid(EfluentTerc, 2, 4) / 2
            Else
                EfluentTercF = EfluentTerc
            End If
            MEnt = Application.VLookup(CLng(CDate(fecha)), rango, 6, False)
            If Application.WorksheetFunction.IsText(MEnt) Then
                MEntf = Mid(MEnt, 2, 4) / 2
            Else
                MEntf = MEnt
            End If
            MA = Application.VLookup(CLng(CDate(fecha)), rango, 7, False)
            If Application.WorksheetFunction.IsText(MA) Then
                MAf = Mid(MA, 2, 4) / 2
            Else
                MAf = MA
            End If
            MB = Application.VLookup(CLng(CDate(fecha)), rango, 8, False)
            If Application.WorksheetFunction.IsText(MB) Then
                MBf = Mid(MB, 2, 4) / 2
            Else
                MBf = MB
            End If
            MC = Application.VLookup(CLng(CDate(fecha)), rango, 9, False)
            If Application.WorksheetFunction.IsText(MC) Then
                MCf = Mid(MC, 2, 4) / 2
            Else
                MCf = MC
            End If
            ML = Application.VLookup(CLng(CDate(fecha)), rango, 10, False)
            If Application.WorksheetFunction.IsText(ML) Then
                MLf = Mid(ML, 2, 4) / 2
            Else
                MLf = ML
            End If
            
            
            
            If IsNumeric(MEntf) Then
                influente = MEntf
                If IsEmpty(MEntf) And IsNumeric(EfluentTercF) Then
                    influente = EfluentTercF
                    If IsEmpty(MEntf) And IsEmpty(EfluentTercF) Then influente = MLf
                End If
            End If
            
            
            If IsNumeric(MAf) Then
            
                MAf2 = Format(Round(MAf, 2), "#,##0.00")
            
                If IsEmpty(MAf) Then
                    MAf2 = ""
                End If
            End If
            

            
            sheets("BD").Cells(cont, 3) = m
            sheets("BD").Cells(cont, 4) = y
            sheets("BD").Cells(cont, 5) = muestras
            sheets("BD").Cells(cont, 6) = analisis
            sheets("BD").Cells(cont, 7) = parametro
            sheets("BD").Cells(cont, 8) = tipo
            sheets("BD").Cells(cont, 9) = metodo
            sheets("BD").Cells(cont, 10) = EfluentTercF
            sheets("BD").Cells(cont, 11) = influente
            sheets("BD").Cells(cont, 12) = MAf2
            sheets("BD").Cells(cont, 13) = MBf
            sheets("BD").Cells(cont, 14) = MCf
            sheets("BD").Cells(cont, 15) = MLf
        
        Next cont
        firstRow = ultlinea + 1                   '<- added2
        Application.CutCopyMode = False           '<- moved2
    Next shtName                                  '<- added
    Application.ScreenUpdating = True             '<- added
    sheets("BD").Select
    Range("A3").Select
    MsgBox "Valores copiados exitosamente", vbInformation, "Copiar" '<- changed
End Sub


file

Thank you
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I tried to use INDEX and MATCH (two criteria) with new created ranges, but it doesn't work
What do you want to do with Index-Match you haven't explained. Having multiple entries for one date is no problem until you have some second unique way of identifying the data you are looking for.

Or maybe i should use another function?

It all depends on purpose and kind of lookup you want to perform there can be multiple solution to your problem with Excel 365 in your hand.

Explain better what you want to achieve...
 
Upvote 0
Hi!

Thank you for your response.

The code is for copy information from some sheets (12) to BD. In sheet BD should be all the information from the other sheets (manually registered) with its format and must be updated (BD) every time data is entered in the other sheets. Headers in BD are fixed.

The code works well, but then I realized in some sheets for the same date (duplicated dates) are different values registered, and the code just identify the first value (not all values registered as I would like them to be copied) when i use the function vlookup.

I used an expression to count them in each sheet (column A), I tried to use INDEX and MATCH (with two criteria) with new created ranges (rngcont, rngfecha, rngmuest), but it doesn't work (muestras). I want to do the same for 'analisis, parametro, tipo, metodo, EfluentTerc, MEnt, MA, MB, MC, ML'



Patricia CM
 
Upvote 0
What I understand from the information you have provided is probably you need a better method of dala consolidation. Check the video below it can help you in some useful way -

 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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