Consulting

Results 1 to 11 of 11

Thread: Solved: Delete function failing

  1. #1
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    Solved: Delete function failing

    I have this routine which has worked fine until I added avriable for the worksheet to use.

    [vba]Sub DeleteRows(ws As Worksheet, criteria As String, col As Long)
    Dim rTable As Range
    Application.ScreenUpdating = False
    On Error Resume Next
    'Determine the table range

    With Selection
    If ws.Cells.Count > 1 Then
    Set rTable = Selection
    Else
    Set rTable = .CurrentRegion
    On Error GoTo 0
    End If
    End With
    'Remove any existing AutoFilters
    ws.AutoFilterMode = False

    'Filter table based on vCriteria using the relative column position stored in lCol.
    rTable.AutoFilter Field:=col, Criteria1:=criteria

    'Delete all rows that are NOT hidden by AutoFilter.
    rTable.Offset(2, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    'Remove AutoFilters
    ws.AutoFilterMode = False
    On Error GoTo 0

    End Sub[/vba]

    I have tried calling the routine linke this
    (Where 'parent' is correctly defined)

    [vba]DeleteRows(Worksheets("Treeview"), parent, 2)[/vba]

    And I have tried calling it like this

    [vba]DeleteRows(wsTree,parent,2)[/vba]

    Where the worksheet is defined as
    [vba]Public wsTree as worksheet
    Set wsTree = thisworkbook.worksheets("TreeView")[/vba]

  2. #2
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Ok
    I think I have spotted it
    [vba]Sub DeleteRows(ws As Worksheet, criteria As String, col As Long)
    Dim rTable As Range
    Application.ScreenUpdating = False
    Dim name As String
    'added this and it seems to work
    ws.Select

    On Error Resume Next
    'Determine the table range

    With Selection
    If ws.Cells.Count > 1 Then
    Set rTable = Selection
    Else
    Set rTable = .CurrentRegion
    On Error GoTo 0
    End If
    End With
    'Remove any existing AutoFilters
    ws.AutoFilterMode = False

    'Filter table based on vCriteria using the relative column position stored in lCol.
    rTable.AutoFilter Field:=col, Criteria1:=criteria

    'Delete all rows that are NOT hidden by AutoFilter.
    rTable.Offset(2, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    'Remove AutoFilters
    ws.AutoFilterMode = False
    On Error GoTo 0

    End Sub[/vba]

    But I still have a problem with the routine that I would like help with please.

    When it runs it deletes all the rows correctly but also deletes the header row which I would like to keep

    I thought this bit
    [VBA]rTable.Offset(2, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete[/VBA]
    Should mean the header (top) row should remain but obviously doesn't work

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this

    [vba]

    Intersect(rTable.SpecialCells(xlCellTypeVisible), rTable.Offset(1, 0)).EntireRow.Delete
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]
    With Selection
    If ws.Cells.Count > 1 Then
    Set rtable = Selection
    Else
    Set rtable = .CurrentRegion
    On Error GoTo 0
    End If
    End With
    Dim DelTable as range
    Set DelTable = rtable.Offset(2)
    'Remove any existing AutoFilters
    ws.AutoFilterMode = False

    'Filter table based on vCriteria using the relative column position stored in lCol.
    rtable.AutoFilter Field:=col, Criteria1:=criteria

    'Delete all rows that are NOT hidden by AutoFilter.
    DelTable.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    'Remove AutoFilters
    ws.AutoFilterMode = False
    On Error GoTo 0

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Thanks guys but no joy.
    Tried XLD's - this option did not delete anything

    Tried mdm's and this gave the same results ie the header row was deleted as well.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post your workbook then, mine worked in my tests.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    OK, but it will take some time to build a demo

  8. #8
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Quicker than I thought to compile demo

    I thought I had cracked the original problem but I haven't

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this

    [vba]

    Sub DeleteRows(ws As Worksheet, criteria As Variant, col As Long)
    Dim rTable As Range
    Dim LastRow As Long

    On Error Resume Next
    'Determine the table range

    With ws

    .Select
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    Set rTable = .Range("A1").Resize(LastRow, 4)

    'Remove any existing AutoFilters
    .AutoFilterMode = False

    'Filter table based on vCriteria using the relative column position stored in lCol.
    rTable.AutoFilter Field:=col, Criteria1:=criteria

    'Delete all rows that are NOT hidden by AutoFilter.
    Intersect(rTable.SpecialCells(xlCellTypeVisible), rTable.Offset(1, 0)).EntireRow.Delete

    'Remove AutoFilters
    .AutoFilterMode = False
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    As usual thanks XLD
    Thats the second time I have noticed you have used the .resize option to define a range, looks quite handy

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You must have missed most of my posts <vbg> - I use it all the time.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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