Consulting

Results 1 to 6 of 6

Thread: Solved: Delete rows based on values in an array

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Solved: Delete rows based on values in an array

    I need to delete rows from sheet1 if the value is found anywhere in the array on sheet2. So lets say that my value I am searching for is in column B. The array is in column A of sheet2. Can you use a function to do that or do you have to use a macro?

    will this do what I want?

    [vba] Sub compare_delete()
    FinalRowSh1 = Worksheets("Sheet1").Range("G65536").End(xlUp).Row
    FinalRowSh2 = Worksheets("Sheet2").Range("F65536").End(xlUp).Row
    For i = FinalRowSh2 To 1 Step -1
    For j = FinalRowSh1 To 1 Step -1
    If Worksheets("DHT ALL").Cells(j, 7) = Worksheets("GHT ALL").Cells(i, 6) Then
    Worksheets("Sheet1").Cells(j, 1).EntireRow.Delete
    End If
    Next j
    Next i
    End Sub [/vba]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Austen
    Rather than looping both sets of data, use Find to determine if the data exists.
    [VBA]
    Option Explicit
    Sub DelRows()
    Dim Cel As Range, c As Range
    With Sheets(1)
    For Each Cel In Range(.Cells(1, 2), .Cells(Rows.Count, 2).End(xlUp))
    Set c = Sheets(2).Columns(1).Find(Cel.Text)
    If Not c Is Nothing Then Cel.EntireRow.Delete
    Next
    End With
    End Sub

    [/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'

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thank you my friend. I shall give it a go.

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Hi Malcomb,

    I tried your code on a sample spreadsheet which I am attaching. Some of the rows are still left on sheet1 after you run the macro. Any ideas?

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    We need to delete from the bottom up!
    Try
    [VBA]
    Sub DelRows()

    Dim c As Range
    Dim Rw As Long, i As Long, tmp As Long
    Rw = Sheets(1).Cells(Rows.Count, 2).End(xlUp).Row
    For i = Rw To 1 Step -1
    tmp = Sheets(1).Cells(i, 2).Value
    Set c = Sheets(2).Columns(1).Find(tmp)
    If Not c Is Nothing Then Sheets(1).Rows(i).Delete
    Next
    End Sub
    [/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'

  6. #6
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks Malcomb. I realized that after fiddling with your code. Just had not had a chance to post back. Works great with the modification!! Many thanks. 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
  •