Excel - Macros para fechas

 
Vista:
sin imagen de perfil
Val: 16
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

Macros para fechas

Publicado por Ana Karen (7 intervenciones) el 30/04/2021 20:34:41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//Se  necesita implementar en la siguiente macros un filtro de fecha inicio y fecha final
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$5" Then
        Call consulta
 
    End If
End Sub
 
Sub consulta()
    Dim hoja_origen As String
    Dim hoja_destino As String
 
    hoja_origen = "RUTAS DIARIAS TOBE"
    hoja_destino = "FORMATO CUENTAS TOBE"
 
    Dim cantidad As String
    Dim clave As String
    Dim clave_aux As String
    Dim clave_viaje As String
    Dim fecha As String
    Dim tarifaUnitaria As String
    Dim ruta As String
    Dim total As String
 
    Dim ultimafilaorigen As Long
    Dim aux As Long
    Dim count As Long
    Dim palabra As String
    palabra = Sheets(hoja_destino).Cells(5, 1)
    inicial = 12
 
    condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Do Until (condicion Like "END")
        Rows(inicial).EntireRow.Delete
        condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Loop
 
    ultimafilaorigen = Sheets(hoja_origen).Range("A" & Rows.count).End(xlUp).Row
 
    For count = 2 To ultimafilaorigen
        If Sheets(hoja_origen).Cells(count, 1) Like palabra Then
            Rows(inicial).EntireRow.Insert
            cantidad = Sheets(hoja_origen).Cells(count, 10)
            clave_aux = Sheets(hoja_origen).Cells(count, 2)
            clave = palabra & clave_aux
            fecha = Sheets(hoja_origen).Cells(count, 5)
            tarifaUnitaria = Sheets(hoja_origen).Cells(count, 16)
            total = Sheets(hoja_origen).Cells(count, 16)
 
            Sheets(hoja_destino).Cells(inicial, 1) = cantidad
            Sheets(hoja_destino).Cells(inicial, 2) = clave_aux
            Sheets(hoja_destino).Cells(inicial, 3) = clave
            Sheets(hoja_destino).Cells(inicial, 4) = fecha
            Sheets(hoja_destino).Cells(inicial, 5) = tarifaUnitaria
            Sheets(hoja_destino).Cells(inicial, 6) = total
            inicial = inicial + 1
 
        End If
    Next count
 
End Sub
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Antoni Masana
Val: 4.908
Oro
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

Macros para fechas

Publicado por Antoni Masana (2477 intervenciones) el 30/04/2021 21:27:54
Te cuento algunas mejoras

En la hoja __ la fórmula de la columna P cambia esto:

1
=SI(D3=Producto!$F$1;BUSCARV(B3;Precios!A:D;4;0);BUSCARV(B3;Precios!A:D;3;0))
por esto
1
=SI.ERROR(SI(D3=Producto!$F$1;BUSCARV(B3;Precios!A:D;4;0);BUSCARV(B3;Precios!A:D;3;0)); "")

El la misma hoja columna Q cambia esto:

1
=BUSCARV(B3;Rutas!A:C;3;0)
por esto
1
=SI.ERROR(BUSCARV(B3;Rutas!A:C;3;0); "")

La macro te hago un cambio que esta mejor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Sub consulta()
    Dim hoja_origen As String
    Dim hoja_destino As String
 
    hoja_origen = "RUTAS DIARIAS TOBE"
    hoja_destino = "FORMATO CUENTAS TOBE"
 
    Dim clave_aux As String
    Dim clave_viaje As String
    Dim ruta As String
 
    Dim ultimafilaorigen As Long
    Dim aux As Long
    Dim count As Long
    Dim palabra As String
    palabra = Sheets(hoja_destino).Cells(5, 1)
    inicial = 12
 
    condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Do Until (condicion Like "END")
        Rows(inicial).EntireRow.Delete
        condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Loop
 
    ultimafilaorigen = Sheets(hoja_origen).Range("A" & Rows.count).End(xlUp).Row
 
    For count = 2 To ultimafilaorigen
        If Sheets(hoja_origen).Cells(count, 1) Like palabra Then
            Rows(inicial).EntireRow.Insert
 
            Sheets(hoja_destino).Cells(inicial, 1) = Sheets(hoja_origen).Cells(count, 10)  ' - cantidad
            Sheets(hoja_destino).Cells(inicial, 2) = Sheets(hoja_origen).Cells(count, 2)   ' - clave_aux
            Sheets(hoja_destino).Cells(inicial, 3) = palabra & clave_aux                   ' - clave
            Sheets(hoja_destino).Cells(inicial, 4) = Sheets(hoja_origen).Cells(count, 5)   ' - fecha
            Sheets(hoja_destino).Cells(inicial, 5) = Sheets(hoja_origen).Cells(count, 16)  ' - tarifaUnitaria
            Sheets(hoja_destino).Cells(inicial, 6) = Sheets(hoja_origen).Cells(count, 16)  ' - total
            inicial = inicial + 1
        End If
    Next count
End Sub

Sobre la macro ¿por que tienes dos veces la misma macro en el libro?

Sobre la consulta:

¿Donde esta las fechas fecha inicio y fecha final?
¿Donde se tiene que aplicar el filtro de fechas?

Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 16
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

Macros para fechas

Publicado por Ana Karen (7 intervenciones) el 30/04/2021 21:38:11
EN FORMATO CUENTAS TOBE HAY UN ESPACIO PARA LAS FECHAS INICIO Y FIN, AL SELECCIONAR EL TRACTO ME TRAE LOS VIAJES REALIZADOS PERO QUIERO QUE DE ESOS VIAJES ME TRAIGA SOLO LOS QUE HAIGA ENTRE ESAS DOS FECHAS, TENGO LA MISMA MACRO YA QUE ESTUVE HACIENDO VARIAS PRUEBAS PERO LA QUE FUNCIONA ES LA DE LA HOJA 17
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Antoni Masana
Val: 4.908
Oro
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

Macros para fechas

Publicado por Antoni Masana (2477 intervenciones) el 30/04/2021 21:52:39
Como no has respuesto a mis preguntas y no se en que celdas estan las fecha ni que columna se tiene que filtrar lo dejo a tu discreción

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Sub consulta()
    Dim hoja_origen As String
    Dim hoja_destino As String
 
    hoja_origen = "RUTAS DIARIAS TOBE"
    hoja_destino = "FORMATO CUENTAS TOBE"
 
    Dim clave_aux As String
    Dim clave_viaje As String
    Dim ruta As String
 
    Dim ultimafilaorigen As Long
    Dim aux As Long
    Dim count As Long
    Dim palabra As String
 
    Dim Fecha_Inico As Long, _
        Fecha_Final As Long
 
    palabra = Sheets(hoja_destino).Cells(5, 1)
    inicial = 12
 
    condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Do Until (condicion Like "END")
        Rows(inicial).EntireRow.Delete
        condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Loop
 
    Fecha_Inici = heets(hoja_destino).Cells(??, ??)
    Fecha_Final = heets(hoja_destino).Cells(??, ??)
 
    ultimafilaorigen = Sheets(hoja_origen).Range("A" & Rows.count).End(xlUp).Row
 
    For count = 2 To ultimafilaorigen
        If Sheets(hoja_origen).Cells(count, 1) Like palabra Then
 
            If Sheets(hoja_origen).Cells(count, ??) >= Fecha_Inici And _
               Sheets(hoja_origen).Cells(count, ??) <= Fecha_Final Then
 
                Rows(inicial).EntireRow.Insert
 
                Sheets(hoja_destino).Cells(inicial, 1) = Sheets(hoja_origen).Cells(count, 10)  ' - cantidad
                Sheets(hoja_destino).Cells(inicial, 2) = Sheets(hoja_origen).Cells(count, 2)   ' - clave_aux
                Sheets(hoja_destino).Cells(inicial, 3) = palabra & clave_aux                   ' - clave
                Sheets(hoja_destino).Cells(inicial, 4) = Sheets(hoja_origen).Cells(count, 5)   ' - fecha
                Sheets(hoja_destino).Cells(inicial, 5) = Sheets(hoja_origen).Cells(count, 16)  ' - tarifaUnitaria
                Sheets(hoja_destino).Cells(inicial, 6) = Sheets(hoja_origen).Cells(count, 16)  ' - total
                inicial = inicial + 1
            End If
        End If
    Next count
End Sub

Las fechas dan un poco de problemas, y como no hay datos tampoco lo puedo probar por lo que puede que se tenga que hacer algún ajuste.

Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Antoni Masana
Val: 4.908
Oro
Ha mantenido su posición en Excel (en relación al último mes)
Gráfica de Excel

Macros para fechas

Publicado por Antoni Masana (2477 intervenciones) el 04/05/2021 11:40:51
La macro corregida:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Sub consulta()
    Dim hoja_origen As String
    Dim hoja_destino As String
 
    hoja_origen = "RUTAS DIARIAS TOBE"
    hoja_destino = "FORMATO CUENTAS TOBE"
 
    Dim clave_aux As String
    Dim clave_viaje As String
    Dim ruta As String
 
    Dim ultimafilaorigen As Long
    Dim aux As Long
    Dim count As Long
    Dim palabra As String
 
    Dim Fecha_Inico As Long, _
        Fecha_Final As Long
 
    palabra = Sheets(hoja_destino).Cells(5, 1)
    inicial = 12
 
    condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Do Until (condicion Like "END")
        Rows(inicial).EntireRow.Delete
        condicion = Sheets(hoja_destino).Cells(inicial, 1)
    Loop
 
    Fecha_Inici = Sheets(hoja_destino).Cells(4, "F")
    Fecha_Final = Sheets(hoja_destino).Cells(5, "F")
 
    ultimafilaorigen = Sheets(hoja_origen).Range("A" & Rows.count).End(xlUp).Row
 
    For count = 2 To ultimafilaorigen
        If Sheets(hoja_origen).Cells(count, 1) Like palabra Then
 
            If Sheets(hoja_origen).Cells(count, "E") >= Fecha_Inici And _
               Sheets(hoja_origen).Cells(count, "E") <= Fecha_Final Then
 
                Rows(inicial).EntireRow.Insert
 
                Sheets(hoja_destino).Cells(inicial, 1) = Sheets(hoja_origen).Cells(count, 10)  ' - cantidad
                Sheets(hoja_destino).Cells(inicial, 2) = Sheets(hoja_origen).Cells(count, 2)   ' - clave_aux
                Sheets(hoja_destino).Cells(inicial, 3) = palabra & clave_aux                   ' - clave
                Sheets(hoja_destino).Cells(inicial, 4) = Sheets(hoja_origen).Cells(count, 5)   ' - fecha
                Sheets(hoja_destino).Cells(inicial, 5) = Sheets(hoja_origen).Cells(count, 16)  ' - tarifaUnitaria
                Sheets(hoja_destino).Cells(inicial, 6) = Sheets(hoja_origen).Cells(count, 16)  ' - total
                inicial = inicial + 1
            End If
        End If
    Next count
End Sub


Saludos.
\\//_
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar