2011-10-04 21 views
7

Actualmente estoy creando una macro para formatear una hoja de datos y para eliminar filas de datos inaplicables. Específicamente, estoy buscando eliminar filas donde Columna L = "ABC", así como eliminar filas donde la columna AA <> "DEF".¿Cómo se eliminan las filas en Excel según los criterios que usan VBA?

Hasta ahora he podido lograr el primer objetivo, pero no el segundo. El código existente es:

Dim LastRow As Integer 
Dim x, y, z As Integer 
Dim StartRow, StopRow As Integer 

For x = 0 To LastRow 
    If (Range("L1").Offset(x, 0) = "ABC") Then 
    Range("L1").Offset(x, 0).EntireRow.Delete 
    x = x - 1 

End If 

Respuesta

0

celular con el número 12 es "L" y el número 27 es "AA"

Dim x As Integer 

x = 1 

Do While x <= ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row 

    If (Cells(x, 12) = "ABC") Then 
    ActiveSheet.Rows(x).Delete 
    Else 
     If (Cells(x, 27) <> "DEF") And (Cells(x, 27) <> "") Then 
     ActiveSheet.Rows(x).Delete 
     Else 
     x = x + 1 
     End If 
    End If 

Loop 

End Sub 
6

El uso de un bucle :

Sub test() 
    Dim x As Long, lastrow As Long 
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row 
    For x = lastrow To 1 Step -1 
     If Cells(x, 12).Value = "ABC" or Cells(x, 27) <> "DEF" Then 
      Rows(x).Delete 
     End If 
    Next x 
End Sub 

Usando Autofiltro (no probado - probablemente más rápido):

Sub test2() 
    Range("a1").AutoFilter Field:=12, Criteria1:="ABC", Operator:=xlOr, _ 
          Field:=28, Criteria1:="<>""DEF""" 
    'exclude 1st row (titles) 
    With Intersect(Range("a1").CurrentRegion, _ 
        Range("2:60000")).SpecialCells(xlCellTypeVisible) 
     .Rows.Delete 
    End With 
    ActiveSheet.ShowAllData 
End Sub 
+0

+1 para la dirección, pero ¿dónde está DEF :)? – Fionnuala

+0

@Remou: editado. Gracias ! –

+0

Sí, pero, AA <> "DEF". :) – Fionnuala

0
Sub test() 

    Dim bUnion As Boolean 
    Dim i As Long, lastrow As Long 
    Dim r1 As Range 
    Dim v1 As Variant 

    lastrow = Cells(Rows.Count, 1).End(xlUp).Row 
    v1 = ActiveSheet.Range(Cells(1, 12), Cells(lastrow, 27)).Value2 
    bUnion = False 

    For i = 1 To lastrow 
     If v1(i, 1) = "ABC" Or v1(i, 16) <> "DEF" Then 
      If bUnion Then 
       Set r1 = Union(r1, Cells(i, 1)) 
      Else 
       Set r1 = Cells(i, 1) 
       bUnion = True 
      End If 
     End If 
    Next i 
    r1.EntireRow.Delete 

End Sub 
9

normalmente es mucho más rápido de utilizar Autofiltro en lugar de bucle Ranges

El código siguiente crea una columna de trabajo, a continuación, utilizar una fórmula para detectar eliminar criterios y luego AutoFiltro y borrar los registros de resultados

La columna de trabajo coloca una fórmula

=OR(L1="ABC",AA1<>"DEF") en la fila 1 de la primera columna en blanco y luego copia hacia abajo hasta el verdadero rango utilizado. Entonces, cualquier registro VERDADERO se elimina rápidamente con el Autofiltro

Sub QuickKill() 
    Dim rng1 As Range, rng2 As Range, rng3 As Range 
    Set rng1 = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious) 
    Set rng2 = Cells.Find("*", , xlValues, , xlByRows, xlPrevious) 
    Set rng3 = Range(Cells(rng2.Row, rng1.Column), Cells(1, rng1.Column)) 
    Application.ScreenUpdating = False 
    Rows(1).Insert 
    With rng3.Offset(-1, 1).Resize(rng3.Rows.Count + 1, 1) 
     .FormulaR1C1 = "=OR(RC12=""ABC"",RC27<>""DEF"")" 
     .AutoFilter Field:=1, Criteria1:="TRUE" 
     .EntireRow.Delete 
     On Error Resume Next 
     'in case all rows have been deleted 
     .EntireColumn.Delete 
     On Error GoTo 0 
    End With 
    Application.ScreenUpdating = True 
End Sub 
+0

¿por qué es más rápido el autofiltro? – lucidbrot

Cuestiones relacionadas