Combinar una macro que realize acción a base de dos criterios

Tengo esta macro que dante realizo correctamente ala necesidad

Set h1 = Sheets("history")
Set h2 = Sheets("temp")
h2.Cells.Clear
j = 2
For I = 2 To h1.Range("B" & Rows.Count).End(xlUp).Row
If h1.Cells(I, "B") = (Label10.Caption) And _
(h1.Cells(I, "K")) = "ENTREGADO" Then
folio = h1.Cells(I, "C")
Set b = h2.Columns("A").Find(folio, lookat:=xlWhole)
If b Is Nothing Then
h2.Cells(j, "A") = folio
j = j + 1
End If
End If
Next
u = h2.Range("A" & Rows.Count).End(xlUp).Row
res = WorksheetFunction.Count(h2.Range("A2:A" & u))
HISTORIAL.Label37.Caption = HISTORIAL.Label10.Caption & " SERVICIOS ENTREGADOS: " & res

Cuenta los "ENTREGADOS" a base de dos criterios lo que necesito es algo como esto:

Set h1 = Sheets("history")
Set h2 = Sheets("temp")
h2.Cells.Clear
j = 2
For I = 2 To h1.Range("B" & Rows.Count).End(xlUp).Row
If h1.Cells(I, "B") = (Label10.Caption) And _
(h1.Cells(I, "K")) = "ENTREGADO" Then
folio = h1.Cells(I, "C")
Set b = h2.Columns("A").Find(folio, lookat:=xlWhole)
If b Is Nothing Then
h2.Cells(j, "A") = folio
j = j + 1
End If
End If
Next
u = h2.Range("A" & Rows.Count).End(xlUp).Row
res = WorksheetFunction.Count(h2.Range("A2:A" & u))
HISTORIAL.Label37.Caption = HISTORIAL.Label10.Caption & " SERVICIOS ENTREGADOS: " & res
Set h1 = Sheets("history")
Set h2 = Sheets("temp")
h2.Cells.Clear
j = 2
For I = 2 To h1.Range("B" & Rows.Count).End(xlUp).Row
If h1.Cells(I, "B") = (Label10.Caption) And _
Not (h1.Cells(I, "K")) = "ENTREGADO" Then
folio = h1.Cells(I, "C")
Set b = h2.Columns("A").Find(folio, lookat:=xlWhole)
If b Is Nothing Then
h2.Cells(j, "A") = folio
j = j + 1
End If
End If
Next
u = h2.Range("A" & Rows.Count).End(xlUp).Row
res1 = WorksheetFunction.Count(h2.Range("A2:A" & u))
HISTORIAL.Label38.Caption = HISTORIAL.Label10.Caption & " SERVICIOS NO ENTREGADOS: " & res1
HISTORIAL.Label39.Caption = HISTORIAL.Label10.Caption & " TOTAL DE SERVICIOS: " & res1 + res

Si funciona, pero quisiera saber como unificarla para aprender y asi no me de problemas a futuro respecto a lentitud.

2 respuestas

Respuesta
2

Estos ejemplos quizás te aporten algo más

https://youtu.be/OHCDASXYG70

https://youtu.be/qtkrkNVxsuQ

https://youtu.be/HjuSns2xJ5Y

Respuesta
1

Te anexo la macro actualizada.

Private Sub CommandButton1_Click()
'Por.Dante Amor
    Set h1 = Sheets("history")
    Set h2 = Sheets("temp")
    h2.Cells.Clear
    '
    For i = 2 To h1.Range("B" & Rows.Count).End(xlUp).Row
        folio = h1.Cells(i, "C")
        If h1.Cells(i, "B") = Label10.Caption And h1.Cells(i, "K") = "ENTREGADO" Then
            'agrega los folios en la columna A
            Set b = h2.Columns("A").Find(folio, lookat:=xlWhole)
            If b Is Nothing Then
                j = h2.Range("A" & Rows.Count).End(xlUp).Row + 1
                h2.Cells(j, "A") = folio
            End If
        ElseIf h1.Cells(i, "B") = Label10.Caption And h1.Cells(i, "K") = "NO ENTREGADO" Then
            'agrega los folios en la columna C
            Set b = h2.Columns("C").Find(folio, lookat:=xlWhole)
            If b Is Nothing Then
                j = h2.Range("C" & Rows.Count).End(xlUp).Row + 1
                h2.Cells(j, "C") = folio
            End If
        End If
    Next
    '
    u1 = h2.Range("A" & Rows.Count).End(xlUp).Row
    res1 = WorksheetFunction.Count(h2.Range("A2:A" & u1))
    u2 = h2.Range("C" & Rows.Count).End(xlUp).Row
    res2 = WorksheetFunction.Count(h2.Range("C2:C" & u2))
    HISTORIAL.Label37.Caption = HISTORIAL.Label10.Caption & " SERVICIOS ENTREGADOS: " & res1
    HISTORIAL.Label38.Caption = HISTORIAL.Label10.Caption & " SERVICIOS NO ENTREGADOS: " & res2
    HISTORIAL.Label39.Caption = HISTORIAL.Label10.Caption & " TOTAL DE SERVICIOS: " & res1 + res2
End Sub

sal u dos

Hola dan me puedes enseñar como y cuando usar esto:

ElseIf 

en vez de solo 

ELSE

Una pregunta a la vez.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas