+ Reply to Thread
Results 1 to 4 of 4

Delete row if contains City, State, or Zip

Hybrid View

IFeelYourPain Delete row if contains City,... 10-31-2014, 11:33 AM
alansidman Re: Delete row if contains... 10-31-2014, 11:40 AM
stnkynts Re: Delete row if contains... 10-31-2014, 11:42 AM
IFeelYourPain Re: Delete row if contains... 10-31-2014, 12:00 PM
  1. #1
    Registered User
    Join Date
    10-31-2014
    Location
    TN
    MS-Off Ver
    2010
    Posts
    15

    Delete row if contains City, State, or Zip

    I don't really know what I am doing here, but I need to search Column B for 3 different strings. I need it to search for city, state, and zip, and if any of the columns have matching city, state, or zips, then I need that row removed.

    I also have it set up to remove a row if there is a 0 at the beginning of a field in column B, but I couldn't get this to work, and actually I now need it for column D.

    Here is the code I have thus far:
    Sub EOD()
    '
    ' ZIP Macro
    '
    ' Keyboard Shortcut: Ctrl+e
    '
        Rows("1:3").Select
        Selection.Delete Shift:=xlUp
        Columns("B:B").Select
        Selection.Delete Shift:=xlToLeft
        Columns("D:D").Select
        Selection.Delete Shift:=xlToLeft
        Columns("E:X").Select
        Selection.Delete Shift:=xlToLeft
        Range("A1").Select
        '37546
        Sheets("37546").Select
        Rows("1:3").Select
        Selection.Delete Shift:=xlUp
        Columns("B:B").Select
        Selection.Delete Shift:=xlToLeft
        Columns("D:D").Select
        Selection.Delete Shift:=xlToLeft
        Columns("E:X").Select
        Selection.Delete Shift:=xlToLeft
        Range("A1").Select
        '39201
        Sheets("39201").Select
        Rows("1:3").Select
        Selection.Delete Shift:=xlUp
        Columns("B:B").Select
        Selection.Delete Shift:=xlToLeft
        Columns("D:D").Select
        Selection.Delete Shift:=xlToLeft
        Columns("E:X").Select
        Selection.Delete Shift:=xlToLeft
        Range("A1").Select
        '36241
        Sheets("36241").Select
        Rows("1:3").Select
        Selection.Delete Shift:=xlUp
        Columns("B:B").Select
        Selection.Delete Shift:=xlToLeft
        Columns("D:D").Select
        Selection.Delete Shift:=xlToLeft
        Columns("E:X").Select
        Selection.Delete Shift:=xlToLeft
        Range("A1").Select
        'Delete row containing City State and Zip
        Dim r As Integer
          Dim cityStateZip
    
          cityStateZip = InputBox("Input the city, state, or zip code from " & vbNewLine & _
            "column C for which you want rows removed.")
        
          For r = 1 To GetLastRow("37546", "B")
            If Left(CStr(Cells(r, 6).Value), 1) = "0" Or (Len(Orlando, Fl, 32803) > 0 And InStr(1, Cells(r, 3).Value, cityStateZip) > 0) Then
                Rows(r).EntireRow.Delete
                r = r - 1
            End If
          Next
    End Sub

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,005

    Re: Delete row if contains City, State, or Zip

    Your delete portion of the code should run from the bottom to the top of the list.

    So replace this:

     For r = 1 To GetLastRow("37546", "B")
    With this:

    For r = GetLastRow("37546", "B") to 1 Step -1
    I don't think you need the line

    r=r - 1
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Delete row if contains City, State, or Zip

    I have a feeling that your post doesn't accurately reflect what you really need. Here is what you asked for.

    Sub RunMe()
    Dim i As Long
    
    For i = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1
        If Left(Range("D" & i), 1) = "0" Then
            Rows(i).Delete
        Else
            Select Case LCase(Range("B" & i))
                Case Is = "city", "zip", "state"
                    Rows(i).Delete
            End Select
        End If
    Next i
    
    End Sub

  4. #4
    Registered User
    Join Date
    10-31-2014
    Location
    TN
    MS-Off Ver
    2010
    Posts
    15

    Re: Delete row if contains City, State, or Zip

    Quote Originally Posted by stnkynts View Post
    I have a feeling that your post doesn't accurately reflect what you really need. Here is what you asked for.

    Sub RunMe()
    Dim i As Long
    
    For i = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1
        If Left(Range("D" & i), 1) = "0" Then
            Rows(i).Delete
        Else
            Select Case LCase(Range("B" & i))
                Case Is = "city", "zip", "state"
                    Rows(i).Delete
            End Select
        End If
    Next i
    
    End Sub
    That's exactly what I need, but it didn't work. I run the macro, and it just sits there.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to get rid of the city and state in the address?
    By excel1212 in forum Excel General
    Replies: 2
    Last Post: 09-08-2012, 05:30 PM
  2. Excel 2007 : city - state formula
    By aj_coolguy in forum Excel General
    Replies: 3
    Last Post: 04-30-2010, 02:52 AM
  3. identify city, state zip
    By Dave B in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-17-2005, 11:05 AM
  4. city, state, zip in same cell
    By whs2002 in forum Excel General
    Replies: 2
    Last Post: 04-29-2005, 02:06 AM
  5. [SOLVED] Splitting City State Zip
    By Pete Provencher in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-07-2005, 06:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1