Consulting

Results 1 to 5 of 5

Thread: Solved: Small macro, simpler way of writing it?

  1. #1

    Solved: Small macro, simpler way of writing it?

    Hi,

    Is there a more elegant way of writing this? It does the job (filtering for blanks in cols L to R, then deleting those rows), but I sense a learning opportunity is being missed.

    Thanks

    [vba]Sub DelBlanks()
    Selection.AutoFilter Field:=12, Criteria1:="="
    Selection.AutoFilter Field:=13, Criteria1:="="
    Selection.AutoFilter Field:=14, Criteria1:="="
    Selection.AutoFilter Field:=15, Criteria1:="="
    Selection.AutoFilter Field:=16, Criteria1:="="
    Selection.AutoFilter Field:=17, Criteria1:="="
    Selection.AutoFilter Field:=18, Criteria1:="="

    Rows("1:1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

    ActiveSheet.AutoFilterMode = False


    End Sub[/vba]

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You could put it in a loop:
    Dim n as long
    for n = 12 to 18
        Selection.AutoFilter Field:=n, Criteria1:="=" 
    next n

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

    [vba]


    With Selection

    .AutoFilter Field:=12, Criteria1:="="
    .AutoFilter Field:=13, Criteria1:="="
    .AutoFilter Field:=14, Criteria1:="="
    .AutoFilter Field:=15, Criteria1:="="
    .AutoFilter Field:=16, Criteria1:="="
    .AutoFilter Field:=17, Criteria1:="="
    .AutoFilter Field:=18, Criteria1:="="
    End With

    Set Rng = Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row)
    If Not Rng Is Nothing Then

    Rng.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

    ActiveSheet.AutoFilterMode = False
    [/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
    Both work and are informative, thank you.

    Quickie - is there any disadvantage to using:

    [VBA]Set Rng = Range("A1").CurrentRegion[/VBA]

    instead of

    [VBA]Set Rng = Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row)[/VBA]

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No disadvantage as long as you don't have discontiguous blocks of data, in which case you can use UsedRange.

    The former will also use all columns, not just A.
    ____________________________________________
    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
  •