Consulting

Results 1 to 6 of 6

Thread: Solved: Delete part of a row based on a empty cell

  1. #1
    VBAX Regular
    Joined
    Jun 2008
    Posts
    52
    Location

    Solved: Delete part of a row based on a empty cell

    Hi,

    I need to have VBA code that checks if there is no value in column A.(like cell A4 is empty in this example - see attachment). If it is empty the row should be deleted from the range A4:E4.
    Is this possible to do in a loop untill row 10000 has been reached?

    Thanks,

    RM

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    '-----------------------------------------------------------------
    Public Sub DeleteDuplicateRowsUsingAutofilter()
    '-----------------------------------------------------------------
    ' Function: Conditionally remove rows
    ' Synopsis: Applies Autofilter to results
    ' Deletes visible rows
    ' Note: Change the 'TEST_COLUMN' variable if the data to
    ' be checked for uniqueness is not in column 1
    ' Author: Bob Phillips - © 2008 xlDynamic.com
    '-----------------------------------------------------------------
    Const TEST_COLUMN As Long = 1
    Dim Lastrow As Long

    With ActiveSheet

    'first, count the rows to operate on
    Lastrow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row

    'finally, apply the autofilter for al matching cells
    Columns(TEST_COLUMN).AutoFilter Field:=1, Criteria1:="=", Operator:=xlAnd

    'we now have only matching rows visible, so we can
    'delete these matching rows
    With .Range(.Cells(2, TEST_COLUMN), .Cells(Lastrow + 1, TEST_COLUMN))
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With

    'tidy up by deleting the column we added
    .Columns(TEST_COLUMN).Delete
    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

  3. #3
    VBAX Regular
    Joined
    Jun 2008
    Posts
    52
    Location
    Hi Xld,

    I get stuck on this part

    [VBA]
    Columns(TEST_COLUMN).AutoFilter Field:=1, Criteria1:="=", Operator:=xlAnd
    [/VBA]

    The error message is:
    Run-time error '1004'
    The command could not be completed by using the range specified. Select a single cell within the range and try the command again.

    Now, I do select cell A2, before I run the code.
    Besides that, what does the "criteria" part mean, does it mean that it filters where the value is "=" ?

    Please help?

    Regards,

    RM

  4. #4
    VBAX Regular
    Joined
    Jun 2008
    Posts
    52
    Location

    Delete part of a row based on a empty cell

    Hey,

    I don't want to be rude, but despite the fact, that the provided code contains a description for each action, I think that I didn't explain my problem correctly.

    In the example sheet I also described what the code should do.

    Basically this is the situation:
    In a sheet, column A till E contains data. Column A can ocassionally be empty but column B till E always contains data. There are also empty rows (column A till E)
    If the value of the cell in column A is empty, then the row should be deleted, and the rows below should be shifted up, so that it will not cause empty rows.

    I'm actually expecting a loop kind of formula.

    Can somebody help me?

    Regards,

    RM

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Change Test-Column to "A"
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    VBAX Regular
    Joined
    Jun 2008
    Posts
    52
    Location
    Hi, It still removes the complete first column. Which is not supposed to happen. I replaced "TEST_COLUMN" with "A"

Posting Permissions

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