es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Eliminar todo basado en otro valor de celda.

Necesito ayuda con el código VBA de Excel.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 And Target.Cells.Count = 1 Then
    If LCase(Target.Value) = "-1" Then
        With Target.EntireRow.Delete
        End With
    End If
End If
If Target.Column = 3 And Target.Cells.Count = 1 Then
    If LCase(Target.Value) = "1000" Then
        With Target.EntireRow
            .Copy Sheets("Week Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
            .Delete
        End With
    End If
End If
End Sub

Si ingresamos -1 en la tercera columna, se borrará la fila. Si ingresamos 1000, se copiará a otra hoja y se eliminará de la hoja actual.

El código anterior funciona bien. En lugar de borrar los datos de la fila, quiero eliminar esa fila. Por lo tanto, agregué

Línea 4 With Target.EntireRow.ClearContents a With Target.EntireRow.Delete

Pero muestra un error.

Tags:  , , ,

Answer

  1. Avatar for davy.ai

    El error probablemente se debe a un problema de sintaxis. En lugar de:

    With Target.EntireRow.ClearContents

    Debería ser:

    Target.EntireRow.Delete

    El código corregido sería:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 3 And Target.Cells.Count = 1 Then
            If LCase(Target.Value) = "-1" Then
                Target.EntireRow.Delete
            End If
        End If
        If Target.Column = 3 And Target.Cells.Count = 1 Then
            If LCase(Target.Value) = "1000" Then
                With Target.EntireRow
                    .Copy Sheets("Week Schedule").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                    .Delete
                End With
            End If
        End If
    End Sub
    

    Esto debería eliminar toda la fila en lugar de solo borrar su contenido.

Comments are closed.