Consulting

Results 1 to 4 of 4

Thread: A program to delete certain cells

  1. #1
    VBAX Newbie
    Joined
    Jun 2008
    Posts
    3
    Location

    A program to delete certain cells

    Hello All,
    I am new to the world of Excel VBA so this is quite the learning experience. Currently I am working with an excel spreadsheet that has a set of information which contains both correct and bogus information. The bogus information contains a higher number in a certain column. Here is an example:

    Col A Col B
    Value1 25
    Value1 126 <--bogus
    Value2 65
    Value2 654 <--bogus

    The goal of the macro I wish to create would be to compare the coresponding 'Col B' values of 'Col A' with identical values and delete the lower value row. In other words it would go something like this:

    1. Search Worksheet for Rows With Identical 'Col A' Values
    2. Compare coresponding 'Col B' values (ex. is Cell 1 > or < Cell 2)
    3. Delete Row With Lesser 'Col B' Value
    4. Find Next Set of Identical 'Col A' Values
    5. Repeat Steps 2-4

    Being an extreme novice to this as I said, I haven't the slightest idea as to how to do this. Any help would be much appreciated.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Popular request today

    [vba]

    Public Sub DeleteDuplicateRowsUsingAutofilter()
    Const TestColumn As Long = 3
    Dim cRows As Long

    cRows = Cells(Rows.Count, TestColumn - 1).End(xlUp).Row

    Columns(TestColumn).Insert
    With Cells(2, TestColumn)
    .FormulaArray = "=B2=MAX(IF($A$2:$A$" & cRows & "=A2,$B$2:$B$" & cRows & "))"
    .AutoFill Destination:=.Resize(cRows - 1)
    .Offset(-1, 0).EntireRow.Insert
    End With
    Columns(TestColumn).AutoFilter Field:=1, Criteria1:="FALSE", Operator:=xlAnd

    With Range(Cells(1, TestColumn), Cells(cRows + 1, TestColumn))
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With

    Columns(TestColumn).Delete

    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 Newbie
    Joined
    Jun 2008
    Posts
    3
    Location
    Thanks so much. That program worked flawlessly!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Don't forget to mark your thread solved using the thread tools dropdown
    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'

Posting Permissions

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