Macro que se ejecute automáticamente

En una pregunta anterior me ayudaste una macro, para ejecutarla debo hacerlo manualmente, me podrías por favor ayudar para que esta se ejecute automáticamente sin necesidad de crear un botón, solo al seleccionar el estado "cerrado".

Feliz día

Sub Borrar_Filas()
'Por Dante Amor
    Application.ScreenUpdating = False
    '
    Set h1 = Sheets("Hoja1")
    Set h3 = Sheets("Hoja3")
    col = "H"
    If h1.AutoFilterMode Then h1.AutoFilterMode = False
    h1.Range("A1").CurrentRegion.AutoFilter Field:=Columns(col).Column, Criteria1:="Cerrado"
    u1 = h1.Range(col & Rows.Count).End(xlUp).Row
    If u1 > 1 Then
        h1.Rows("2:" & u1).Copy
        u3 = h3.Range(col & Rows.Count).End(xlUp).Row + 1
        h3.Range("A" & u3).PasteSpecial xlValues
        If h1.AutoFilterMode Then h1.AutoFilterMode = False
        For i = h1.Range(col & Rows.Count).End(xlUp).Row To 2 Step -1
            If h1.Cells(i, col).Value = "Cerrado" Then
                h1.Rows(i).Delete
            End If
        Next
        msj = "Registros copiados y borrados"
    Else
        msj = "No existen registros a copiar"
    End If
    If h1.AutoFilterMode Then h1.AutoFilterMode = False
    Application.ScreenUpdating = True
    MsgBox msj
End Sub

1 Respuesta

Respuesta
2

Para que funcione de forma automática pon la siguiente macro en los eventos de tu hoja.

Private Sub Worksheet_Change(ByVal Target As Range)
'Por Dante Amor
    col = "H"
    If Not Intersect(Target, Columns(col)) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Row < 2 Then Exit Sub
        If Target.Value <> "Cerrado" Then Exit Sub
        Application.ScreenUpdating = False
        '
        Set h1 = Sheets("Hoja1")
        Set h3 = Sheets("Hoja3")
        h1.Rows(Target.Row).Copy
        u3 = h3.Range(col & Rows.Count).End(xlUp).Row + 1
        h3.Range("A" & u3).PasteSpecial xlValues
        h1.Rows(Target.Row).Delete
        '
        Application.ScreenUpdating = True
        MsgBox "Registro copiado y borrado"
    End If
End Sub

Sigue las Instrucciones para poner la macro en los eventos de worksheet

  1. Abre tu libro de excel
  2. Para abrir Vba-macros y poder pegar la macro, Presiona Alt + F11
  3. Del lado izquierdo dice: VBAProject, abajo dale doble click a worksheet(tu hoja)
  4. En el panel del lado derecho copia la macro

'.[Sal u dos. Dante Amor. No olvides valorar la respuesta. 
'.[Avísame cualquier duda

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas