VBA to go through some sheets. Copy and Paste it to main sheet(BD)

pcardenasm

New Member
Joined
Oct 28, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,
I need some help to copy information from some sheets (only ph to Ortofosfatos,12 sheets) to BD. I've tried for one sheet, but i want to do the same with the other sheets in order, first ph, second Temperatura and so on. 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.

Excel file
TEP-EjBD-2023.xlsm

VBA Code:
Sub CopyFosforo()
    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
    Dim EfluentTercF As Variant
   
    Dim rango As Variant
     
   
    sheets("Fósforo").Select
    Range("B12").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    sheets("BD").Select
    Range("A3").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Range("A3").Select
    sheets("Fósforo").Select
    Application.CutCopyMode = False
    Range("B12").Select
   
    ultlinea = sheets("BD").Range("A" & Rows.Count).End(xlUp).Row
    fila = sheets("Fósforo").Range("B12").End(xlDown).Row
    Set rango = sheets("Fósforo").Range("B12:N" & fila)
   
       
    For cont = 3 To ultlinea
        fecha = sheets("BD").Cells(cont, 1)
        m = Month(fecha)
        y = Year(fecha)
        muestras = Application.VLookup(CLng(CDate(fecha)), rango, 2, False)
        analisis = Application.VLookup(CLng(CDate(fecha)), rango, 3, False)
        parametro = sheets("Fósforo").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
        End If
       
        sheets("BD").Cells(cont, 2) = m
        sheets("BD").Cells(cont, 3) = y
        sheets("BD").Cells(cont, 4) = muestras
        sheets("BD").Cells(cont, 5) = analisis
        sheets("BD").Cells(cont, 6) = parametro
        sheets("BD").Cells(cont, 7) = tipo
        sheets("BD").Cells(cont, 8) = metodo
        sheets("BD").Cells(cont, 9) = influente
        sheets("BD").Cells(cont, 10) = MAf
        sheets("BD").Cells(cont, 11) = MBf
        sheets("BD").Cells(cont, 12) = MCf
        sheets("BD").Cells(cont, 13) = MLf

    Next cont
   
    sheets("BD").Select
    Range("A3").Select
       
 
    MsgBox "Valores de fósforo copiados exitosamente", vbInformation, "Copiar"
   
End Sub


Thanks for considering my request.

Patricia CM
 
Last edited by a moderator:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Have a try with these few changes. Note that you have an error (number format) in sheet "Nitrógeno" row "186" column "I".
Please test on a copy of your file.
VBA Code:
Option Explicit
Sub CopyFosforo()
    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

    Application.ScreenUpdating = False            '<- added: avoids screen flickering
    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(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets("BD").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        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
        For cont = 3 To ultlinea
            fecha = Sheets("BD").Cells(cont, 1)
            m = Month(fecha)
            y = Year(fecha)
            muestras = Application.VLookup(CLng(CDate(fecha)), rango, 2, False)
            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
            Sheets("BD").Cells(cont, 2) = m
            Sheets("BD").Cells(cont, 3) = y
            Sheets("BD").Cells(cont, 4) = muestras
            Sheets("BD").Cells(cont, 5) = analisis
            Sheets("BD").Cells(cont, 6) = parametro
            Sheets("BD").Cells(cont, 7) = tipo
            Sheets("BD").Cells(cont, 8) = metodo
            Sheets("BD").Cells(cont, 9) = influente
            Sheets("BD").Cells(cont, 10) = MAf
            Sheets("BD").Cells(cont, 11) = MBf
            Sheets("BD").Cells(cont, 12) = MCf
            Sheets("BD").Cells(cont, 13) = MLf
        Next cont
    Next shtName                                  '<- added
    Application.ScreenUpdating = True             '<- added
    Sheets("BD").Select
    Range("A3").Select
    MsgBox "Valores copiados exitosamente", vbInformation, "Copiar" '<- changed
End Sub
 
Upvote 0
Thank you for your answer. I tried with your suggestion, in the main sheet (BD) all the data was copied only from the last sheet (Ortofosfatos). Additionally, in "Fecha" column (BD), the range of values ("Fecha" column) from the "Conductividad" sheet was copied below the data from "Ortofosfatos" sheet, generating #N/A values in BD sheet.


TEP-EjBD-2023_solucion.xlsm
ABCDEFGHIJKLM
2FechaMesAñoMuestrasAnálisisParámetroTipo de muestraMétodoInfluenteMAMBMCML
315/01/201412014UPVUPVOrtofosfatosPuntual0.0140.0230.0170.0170.014
405/02/201422014UPVUPVOrtofosfatosPuntual0.0080.0050.0120.0050.008
526/02/201422014UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
612/03/201432014UPVUPVOrtofosfatosPuntual0.0090.0050.0100.0050.009
702/04/201442014UPVUPVOrtofosfatosPuntual0.0050.0080.0050.0050.005
824/04/201442014UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
914/05/201452014UPVUPVOrtofosfatosPuntual0.0050.0050.0100.0050.005
1004/06/201462014UPVUPVOrtofosfatosPuntual0.0060.0050.0060.0050.006
1125/06/201462014UPVUPVOrtofosfatosPuntual0.0050.0060.0150.0050.005
1216/07/201472014UPVUPVOrtofosfatosPuntual0.0050.0050.0170.0050.005
1330/07/201472014UPVUPVOrtofosfatosPuntual0.0260.0150.0120.0050.026
1427/08/201482014UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
1524/09/201492014UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
1615/10/2014102014UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0350.005
1729/10/2014102014UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
1819/11/2014112014UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
1911/12/2014122014UPVUPVOrtofosfatosPuntual0.0090.0050.0050.0050.009
2007/01/201512015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
2126/01/201512015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
2211/02/201522015UPVUPVOrtofosfatosPuntual0.0050.0050.0080.0050.005
2304/03/201532015UPVUPVOrtofosfatosPuntual0.0050.0050.0080.0050.005
2425/03/201532015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
2520/04/201542015UPVUPVOrtofosfatosPuntual0.0050.0050.0090.0050.005
2606/05/201552015UPVUPVOrtofosfatosPuntual0.0050.0050.0060.0050.005
2727/05/201552015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
2817/06/201562015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
2908/07/201572015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
3029/07/201572015UPVUPVOrtofosfatosPuntual0.0050.0290.0100.0050.005
3126/08/201582015UPVUPVOrtofosfatosPuntual0.0050.0440.0050.0050.005
3217/09/201592015UPVUPVOrtofosfatosPuntual0.0050.0090.0050.0050.005
3307/10/2015102015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
3421/10/2015102015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
3511/11/2015112015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
3602/12/2015122015UPVUPVOrtofosfatosPuntual0.0050.0090.0050.0050.005
3723/12/2015122015UPVUPVOrtofosfatosPuntual0.0050.0050.0050.0050.005
3826/01/201612016UPVUPVOrtofosfatosPuntual0.0240.0050.024
3924/02/201622016UPVUPVOrtofosfatosPuntual0.0050.0100.005
4022/03/201632016UPVUPVOrtofosfatosPuntual0.0050.0050.005
4126/04/201642016UPVUPVOrtofosfatosPuntual0.0050.0050.005
4225/05/201652016UPVUPVOrtofosfatosPuntual0.0040.0070.004
4321/06/201662016UPVUPVOrtofosfatosPuntual0.0160.0050.016
4426/07/201672016UPVUPVOrtofosfatosPuntual0.0030.0020.003
4530/08/201682016UPVUPVOrtofosfatosPuntual0.0050.0540.005
4629/09/201692016UPVUPVOrtofosfatosPuntual0.0080.0230.008
4726/10/2016102016UPVUPVOrtofosfatosPuntual0.0340.0050.034
4830/11/2016112016UPVUPVOrtofosfatosPuntual0.0100.0170.010
4913/03/201732017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0030.0040.003
5029/03/201732017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0080.0080.008
5119/04/201742017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0230.0250.023
5217/05/201752017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0800.0630.080
5314/06/201762017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0100.0160.010
5412/07/201772017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0000.0000.000
5521/08/201782017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0000.0000.000
5613/09/201792017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0050.0050.005
5711/10/2017102017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0260.0170.026
5815/11/2017112017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0230.0180.023
5919/12/2017122017AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0160.0070.016
6017/01/201812018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0310.0180.031
6122/05/201852018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0500.0150.0150.050
6226/06/201862018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
6324/07/201872018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
6428/08/201882018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
6524/09/201892018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
6623/10/2018102018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
6727/11/2018112018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
6818/12/2018122018AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
6924/01/201912019AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
7026/02/201922019AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
7126/03/201932019AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0150.0150.0150.015
7226/04/201942019AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0600.0150.060
7303/06/201962019AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.1700.1400.0150.015
7425/06/201962019AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.0400.0900.0150.015
7530/07/201972019AnalaquaAnalaquaOrtofosfatosPuntualEspectofotometría (valor menor de 0.15 fuera de acreditación)0.4000.1700.1000.030
7602/10/2019102019LTLLTLOrtofosfatos
7710/10/2019102019LTLLTLOrtofosfatos
7817/10/2019102019LTLLTLOrtofosfatos
7923/10/2019102019LTLLTLOrtofosfatos
8030/10/2019102019LTLLTLOrtofosfatos0.1230.1010.0250.184
8105/11/2019112019LTLLTLOrtofosfatos
8213/11/2019112019LTLLTLOrtofosfatos
8320/11/2019112019LTLLTLOrtofosfatos
8427/11/2019112019LTLLTLOrtofosfatos0.1310.1320.0250.0250.131
8503/12/2019122019LTLLTLOrtofosfatos
8611/12/2019122019LTLLTLOrtofosfatos
8718/12/2019122019LTLLTLOrtofosfatos
8824/12/2019122019LTLLTLOrtofosfatos0.0250.0250.0250.117
8930/12/2019122019LTLLTLOrtofosfatos
9008/01/202012020LTLLTLOrtofosfatos
9114/01/202012020LTLLTLOrtofosfatos
9224/01/202012020LTLLTLOrtofosfatos
9328/01/202012020LTLLTLOrtofosfatos0.0250.0970.0250.025
9404/02/202022020LTLLTLOrtofosfatos
9512/02/202022020LTLLTLOrtofosfatos
9619/02/202022020LTLLTLOrtofosfatos0.1520.1620.0250.154
9727/02/202022020LTLLTLOrtofosfatos
9803/03/202032020LTLLTLOrtofosfatos
9911/03/202032020LTLLTLOrtofosfatos
10017/03/202032020LTLLTLOrtofosfatos0.3100.2350.1480.025
BD
 

Attachments

  • Captura.png
    Captura.png
    102.3 KB · Views: 6
Upvote 0
You weren't erasing the target area in the 'BD' sheet and I didn't notice. Here's an updated version with a bit of cleanup of useless lines of code (from Macro Recorder). Take another test.
VBA Code:
Option Explicit
Sub CopyFosforo()
    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

    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:M" & 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("B12"), Range("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
        For cont = firstRow To ultlinea           '<- changed2
            fecha = Sheets("BD").Cells(cont, 1)
            m = Month(fecha)
            y = Year(fecha)
            muestras = Application.VLookup(CLng(CDate(fecha)), rango, 2, False)
            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
            Sheets("BD").Cells(cont, 2) = m
            Sheets("BD").Cells(cont, 3) = y
            Sheets("BD").Cells(cont, 4) = muestras
            Sheets("BD").Cells(cont, 5) = analisis
            Sheets("BD").Cells(cont, 6) = parametro
            Sheets("BD").Cells(cont, 7) = tipo
            Sheets("BD").Cells(cont, 8) = metodo
            Sheets("BD").Cells(cont, 9) = influente
            Sheets("BD").Cells(cont, 10) = MAf
            Sheets("BD").Cells(cont, 11) = MBf
            Sheets("BD").Cells(cont, 12) = MCf
            Sheets("BD").Cells(cont, 13) = 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
 
Upvote 0
Solution
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0
Hi again 😊,
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
136118/09/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,9501,950CompuestaMEN-LMA-016
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
157129/12/2020Intercontrol Levante S.A.Intercontrol Levante S.A.1,9401,930CompuestaMEN-LMA-016
Conductividad
Cell Formulas
RangeFormula
A136:A157A136=COUNTIF($B$12:B136,B136)




VBA Code:
Option Explicit
Sub CopyFosforo()
    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


Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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