delete row if excel 2007 vba

Posted on 2011-04-27
Last Modified: 2012-08-13
Hello all

I need to be able to delete rows in sheet (" Fleet") if the name in column D starts with one of the names in sheet("3P carrier list") column A.

This would be done by clicking on the delete row if button.

How can i do this please?
Thanks again for your help.

Question by:Wilder1626
    LVL 30

    Accepted Solution

    Is this what you want?

    Please paste this in a module.

    Sub DeleteRows()
        Dim i As Long, j As Long
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim ws1LastRow As Long, ws2LastRow As Long
        Dim Rng As Range
        Set ws1 = Sheets("Fleet")
        Set ws2 = Sheets("3P carrier list")
        ws1LastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
        ws2LastRow = ws2.Range("A" & Rows.Count).End(xlUp).Row
        For i = 2 To ws2LastRow
            For j = ws1LastRow To 4 Step -1
                If InStr(1, ws1.Range("D" & j).Value, ws2.Range("A" & i).Value, vbTextCompare) Then
                    If Rng Is Nothing Then
                        Set Rng = ws1.Rows(j)
                        Set Rng = Union(Rng, ws1.Rows(j))
                    End If
                End If
            Next j
        Next i
        Rng.Delete shift:=xlUp
    End Sub

    Open in new window

    LVL 11

    Author Closing Comment

    This is perfect.

    Thanks again for your help.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Sometimes we don't want to show zeros in our Excel spreadsheets. This is sometimes most evident in our charts. Look at the chart below, all the zero values are visible. I think that all will agree with the fact that zero values are not looking nice …
    Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
    The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now