Consulting

Results 1 to 4 of 4

Thread: Delete filter outcome

  1. #1
    VBAX Regular
    Joined
    Jun 2016
    Posts
    6
    Location

    Delete filter outcome

    Hey,

    I wrote a macro which first filters a table and then deletes these rows. That works fine, but the macro also deletes 2 more rows and I don't know why, since these two rows are not filtered if I use the filter itself without the deletion option.

    Attached you'll find the macro, I hope someone can help me with this, 'cause I really have no clue



    Sub AutofilterergebnisLoeschen()
    
    Range("L2").Copy _
    Destination:=Range("M2")
    
    Range("M2").Value = "USD in EUR"
    
    
    
    ActiveSheet.Rows(8).Delete
    ActiveSheet.Columns("H:H").Delete
    
    ActiveSheet.Rows(4).Delete
    ActiveSheet.Columns("D:D").Delete
    
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "=1.2"
    
    Range("K3").Select
        ActiveCell.FormulaR1C1 = "=IF(RC[-6]=""US-DOLLAR"",RC[-7]*R1C13,RC[-7])"
        Selection.AutoFill Destination:=Range("K3:K2500"), Type:=xlFillDefault
        Range("K3:K2500").Select
        ActiveWindow.SmallScroll Down:=-2500
    
    
    '** Dimensionierung der Variablen
    
    Dim rngFilterRange As Range
    Dim lngCriteriaCount As Long
    Dim arrCriteria() As String
     
    '** Anzahl der Kriterien festlegen
    lngCriteriaCount = 4
    
     
    '** Variable neu dimensionieren
    ReDim arrCriteria(0 To lngCriteriaCount - 1)
    
     
    '** Filterkriterien festlegen
    arrCriteria(0) = "1500"
    arrCriteria(1) = "1593"
    arrCriteria(2) = "1600"
    arrCriteria(3) = "9999999900"
     
    
    '** Objektvariable setzen
    Set rngFilterRange = ActiveSheet.Range("A1")
    
    '** Autofilter setzen/ausführen
    rngFilterRange.AutoFilter Field:=1, _
    Criteria1:=arrCriteria(), _
    Operator:=xlFilterValues
    
    
    
        'Löscht das Ergebnis des aktuellen Autofilters
        'also die aktuell angezeigten Zeilen !
        'Bereich der per Autofilter gefilterten Liste ist variabel !
        
        If ActiveSheet.AutoFilterMode Then
            If ActiveSheet.FilterMode Then
                If MsgBox("Sollen die Zeilen des aktuellen " & vbLf & _
                "Autofilterergebnisses komplett gelöscht werden ?", _
                    vbQuestion + vbYesNo, "Zeilen Löschen") = vbYes Then
                With ActiveSheet.AutoFilter.Range.Offset(1)
                    .Resize(ActiveSheet.AutoFilter.Range.Rows.Count - 1).EntireRow.Delete _
                        shift:=xlUp
                End With
            End If
        Else
            MsgBox "Es wurden keine Filterkriterien angegeben !"
        End If
        Else
        MsgBox "Es ist kein Autofilter aktiviert !"
        End If
        
        'Herausgefilterte Daten wieder anzeigen lassen
        ActiveSheet.Range("$A$2:$L$1310").AutoFilter Field:=1
        
        Columns("D:D").Select
        Selection.Style = "Comma"
        Columns("K:K").Select
        Selection.Style = "Comma"
        
      
      Range("K2").Select
        Selection.AutoFill Destination:=Range("K2:L2"), Type:=xlFillDefault
        Range("K2:L2").Select
        Range("L2").Select
        ActiveCell.FormulaR1C1 = "Art"
        
        
        
        
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Untested, and just at first glance. Instead of:
    With ActiveSheet.AutoFilter.Range.Offset(1)
      .Resize(ActiveSheet.AutoFilter.Range.Rows.Count - 1).EntireRow.Delete shift:=xlUp
    End With
    try:
    With ActiveSheet.AutoFilter.Range.Offset(1).Resize(ActiveSheet.AutoFilter.Range.Rows.Count - 1)
      .SpecialCells(xlCellTypeVisible).EntireRow.Delete 'shift:=xlUp no need for this when entire rows are being deleted.
    End With
    The use of EntireRow might be a little risky if there's data outside the Autofilter range, you could try instead:
      .SpecialCells(xlCellTypeVisible).Delete shift:=xlUp
    It might need a tweak if there are hidden columns in the Autofilter range.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Basically:

    Sub M_snb()
       with sheet1.cells(1).currentregion
          .autofilter 1, "aaa"
          .offset(1).specialcells(12).entirerow.delete
          .autofilter
       end with
    End Sub

  4. #4
    VBAX Regular
    Joined
    Jun 2016
    Posts
    6
    Location
    Thanks! it worked!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •