1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

MACRO selecting rows that do not contain data in a particular row

Discussion in 'Business Applications' started by JeffSchwartz, Feb 1, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. JeffSchwartz

    JeffSchwartz Thread Starter

    Joined:
    Jan 31, 2013
    Messages:
    56
    In my quest to develop macros to manipulate our data I am trying to find a way to delete all rows that do not contain data in column J. Any ideas of how I could select rows without having the specific worksheet in the macro?

    Whenever I try recording a macro using the sort function, it specifies the one worksheet where I recorded it.


    This is what it recorded:
    ActiveWorkbook.Worksheets("Z011_15A").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Z011_15A").Sort.SortFields.Add Key:=ActiveCell. _
    Offset(-98, 0).Range("A1:A159"), SortOn:=xlSortOnValues, Order:=xlDescending _
    , DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Z011_15A").Sort
    .SetRange ActiveCell.Offset(-99, -9).Range("A1:X160")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
     
  2. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,553
    First Name:
    Hans
    Hi Jeff,
    A loop that starts with the last row and back up, I'm typing if directly into the post so the syntax may need correcting here and there but this would be the general idea.

    Make suer the active sheet is the one you want processed and I assume that A will be the column holding the max nr of rows with data, else you'll need another command to select the last filled cell and take that row.


    Sum DeleteRows()
    Dim lastrow as long
    Dim xrow as long
    lastrow = Range( "A" & Rows.Count).end(XlUp).row
    for xrow = lastrow to 2 step -1
    if len(trim(Range("J" & xrow).value)) = 0 then Range("J" & xrow).EntireRow.Delete
    next xrow
    End sub

    What this macro does is tart with the last row, chek if J has data in it, if not it delets the row.

    Happy coding :)

    BTW You should edit the recorded Sort macro.
     
  3. bomb #21

    bomb #21

    Joined:
    Jul 1, 2005
    Messages:
    8,546
    Hi Hans. :)

    I never got to grips with that "step" thing. Probably never will. :eek:

    Hi OP. Hope this (which "acts on" the active worksheet) helps.

    Sub test()
    Columns("J:J").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End Sub
     
  4. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,553
    First Name:
    Hans
    Hi Bomb, your code is faster, I am one that always forgets the SpecialCells()

    The stpe -1 is to go bottoms up

    Since the last row has been declared and you do it the other way around the lastrow will not comply and generate an error.

    The step command is a handy one, but your code is much fastre (y)

    I'll store it my folder witn hints and tips :)
     
  5. Garf13LD

    Garf13LD

    Joined:
    Apr 17, 2012
    Messages:
    455
    2 problem with special cells.

    1. if found nothing, will trigger error.
    2. if found only one, will deem as single cell selection (this is a problem for find after special cells)

    Sub test()
    dim found as range
    set found = Columns("J:J").SpecialCells(xlCellTypeBlanks)
    if not found is nothing then
    found.EntireRow.Delete
    end if
    End Sub
     
  6. bomb #21

    bomb #21

    Joined:
    Jul 1, 2005
    Messages:
    8,546
    OK. How about:


    Sub test()
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    If WorksheetFunction.CountA(Range("J1:J" & LastRow)) <> LastRow Then
    Range("J1:J" & LastRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
    End Sub


    ?
     
  7. Garf13LD

    Garf13LD

    Joined:
    Apr 17, 2012
    Messages:
    455
    Excel is smart enough to exclude blank cells outside of used range.;)
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1087841

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice