Remove rows that contain apartment addresses

kellman

New Member
Joined
Aug 25, 2012
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I would like to remove rows that contain any apartment style addressing, such as anything with a street number and unit number.

Example apartment address:
2 - 234 Smith Street

Example house address:
167 Jones Avenue



The data I'm importing into my excel sheet contains both apartment and house style addressing.

I need a way to display only house addresses, and not any apartments.

One thing in common about apartment addresses is they uniquely have a hyphen or dash - character.

Is there a way to tell excel to remove rows that contain addresses that have one or more hyphens?

I really appreciate your help!

Kelly
 
Try on a copy of your original first.
Code:
Sub Maybe()
Dim i As Long
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        If InStr(Cells(i, 1), "-") <> 0 Then
            If Asc(Mid(Cells(i, 1), InStr(Cells(i, 1), "-") - 2, 1)) > 47 And Asc(Mid(Cells(i, 1).Value, InStr(Cells(i, 1), "-") + 2, 1)) < 58 Then Cells(i, 1).Delete Shift:=xlUp
        End If
    Next i
End Sub

Oh wow, that works nicely.

Thank you for taking the time to help me!

Happy New Year
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Here is another macro that you can try...
VBA Code:
Sub Maybe2()
  Dim R As Long
  For R = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
    If Cells(R, "A").Value Like "*# - #* *" Then Rows(R).Delete
  Next
End Sub

Brilliant, this also works.

I don't have any VB experience so for me it's hard to tell which of the two solutions are better "maybe" or "maybe2".

Thank you so very much!!
 
Upvote 0
If you have a large range of data and you want to stay away from looping.
Code:
Sub Another_Possibility()
Dim cons, i As Long, rws As Range
cons = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value
    For i = LBound(cons) To UBound(cons)
        If InStr(cons(i, 1), "-") <> 0 Then
            If Asc(Mid(cons(i, 1), InStr(cons(i, 1), "-") - 2, 1)) > 47 And Asc(Mid(cons(i, 1), InStr(cons(i, 1), "-") + 2, 1)) < 58 Then
                If Not rws Is Nothing Then
                    Set rws = Union(rws, Cells(i + 1, 1).EntireRow)
                        Else
                    Set rws = Cells(i + 1, 1).EntireRow
                End If
            End If
        End If
    Next i
rws.Select    '<---- Change to delete if happy.
End Sub
To make it more up to date, replace part of above with Rick's (better) suggestion.
 
Upvote 0
Thank you, I will definitely give your suggestion a try.

The raw CSV data that I copy & paste into Excel each day has 112 Columns and anywhere from 20 to 70 Rows

I just learned how to use the Macro Recorder to trim down the columns down to about 5 columns (the ones with the data I need). What a fantastic tool and time saver!

The last part I need to figure out is text formatting, or rather how to automate the text formatting.

The Macro I recorded on the spreadsheet I used only had about 20 rows. But then what I noticed is, when I ran the same Macro on a sheet with 70 Rows, it stopped formatting my text at row 20.

I'm thinking I'll need something more specific for sheets with varying Rows of data

Perhaps VB Script is the solution again.

I wonder if I should post an example of my text formatting here, or if I should create a new topic/thread?

Regards

Nothing to eloborate, Column Heading in Bold and then each Row I'm using the format "bottom border" button.

GLB for Dec 8.xlsx
A
1Address
2215 GREENWAY CRES W
379 BERRY HILL RD
472 GREENSBORO BAY
538 OAKLEAF DR
6920 CHANCELLOR DR
7131 WINTERHAVEN DR - STVTL
815 OAKRIDGE BAY - STVTL
9375 CASTLEBURY MEADOWS DR - OKLD
10427 - 20 INNSBRUCK WAY - WPG
1138 FETTERLY WAY
12241 WALES AV - STVTL
1351 CURRY DR - HDLY
14115 BRIARLYNN RD - FTGRY
15190 LEAHCREST CRES
16119 RYERSON AV
17159 CREEKSIDE RD - FTGRY
1878 RUTGERS BAY - FTGRY
1977 GALBRAITH CRES - ASNBOIA
2014 GEORGE BARONE BAY - NKLD
2151 CAUSEWAY BAY - STBON
223403 GRANT AV - CHLSWD
2370 MARSEILLES CL - WPG
2465 COLCHESTER BAY - TUX
25115 BAYLOR AV - FTGRY
26147 GEMINI AV - NKLD
27737 SHEPPARD ST - WPG
28141 BAYLOR AV - FTGRY
2923 TIMMINS PL - WPG
3015 BAIRD PL - NKLD
31158 BRIXTON BAY - STVTL
3279 TARASKA BAY - WPG
337051 PR 247 E
3423 SCHAUBROECK RD
3520 VERONA DR
363373 PEMBINA HWY
3746 SERENITY COVE
38172 BERRY HILL RD - FTGRY
39344 NOVAVISTA DR - STVTL
40713 DE LA SEIGNEURIE BLVD - STBON
417 EDITH BAY - NKLD
42293 APPLEFORD GATE - FTGRY
4360 VAN HORNE RD
Sheet1
 
Last edited:
Upvote 0
Since it is a different problem, I would think that a new thread would be better.
If you need to refer to this thread, just put a hyperlink with the address of this thread in your post.
 
Upvote 0
For the original question, it is looking like a macro may be best for you but if you wanted to pursue a formula approach, you could try this version.
Formula still only required in the very top cell of the results area.

23 01 01.xlsm
ABC
1Address
2402 - 775 STERLING LYON PKWY - TUX140 KAIRISTINE LANE - WPG
3140 KAIRISTINE LANE - WPG133 LINDSAY ST
4133 LINDSAY ST26 KEYSTONE CRES - OKLD
526 KEYSTONE CRES - OKLD69 MICHAUD CRES - STVTL
669 MICHAUD CRES - STVTL2096 DE VRIES AV
72096 DE VRIES AV128 CALLUM CRES - NKLD
8103 - 1032 WILKES AV - TUX15 TUNIS BAY - FTGRY
9128 CALLUM CRES - NKLD9 RAPHAEL ST - FTGRY
1015 TUNIS BAY - FTGRY408 YALE AV E - TRANS
119 RAPHAEL ST - FTGRY322 CHURCH AV - FLR 2 - WPG
12202 - 707 STERLING LYON PKWY - TUX325 HARVARD AV W - TRANS
13412 - 2475 WAVERLEY ST - FTGRY15 HADLEY PL - FTGRY
14408 YALE AV E - TRANS93 EGESZ ST - WPG
15228 - 55 DAYLAN MARSHALL GATE - WPG41 ROSLYN CRES - WPG
16404 - 707 STERLING LYON PKWY - TUX428 REGENT AV E - TRANS
17322 CHURCH AV - FLR 2 - WPG10 ALDERWOOD RD - STBON
18325 HARVARD AV W - TRANS27 STREWCHUK BAY - OKLD
1915 HADLEY PL - FTGRY279 HAZELWOOD AV - STVTL
2093 EGESZ ST - WPG
2137 - 59 UNIVERSITY CRES - FTGRY
2241 ROSLYN CRES - WPG
23428 REGENT AV E - TRANS
2412 - 55 UNIVERSITY CRES - FTGRY
2510 ALDERWOOD RD - STBON
261517 - 1750 PEMBINA HWY - FTGRY
2727 STREWCHUK BAY - OKLD
28279 HAZELWOOD AV - STVTL
Addresses (2)
Cell Formulas
RangeFormula
C2:C19C2=FILTER(A2:A28,ISERROR(-SUBSTITUTE(MID(A2:A28,FIND(" - ",A2:A28)-1,5)," - ","")))
Dynamic array formulas.
 
Upvote 0
Brilliant, this also works.
The marked solution has been changed accordingly.
In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0
One last question still relating to my original question.
When I use the VB Code you provided on a sheet with more than 1 Column, it doesn't work.

I will provide a more accurate example of what my final project sheet will look like.

I'm assuming the Range will need to be changed in order for the VB script to work correctly.

There will be 5 fixed Columns but the number of Rows will need to be variable somehow.

I would appreciate if you could take one last look at my scenario.


FTTH GLB DEC 8TH.csv
ABCDE
1Start TimeAddressCityJob idSource Order ID
2
3
4
5
6
7
8
9
10
11
12
FTTH GLB DEC 8TH
 
Upvote 0
You forgot to specify which macro.
Post #9
Code:
Sub Maybe_Column_B()
Dim i As Long
    For i = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
        If InStr(Cells(i, 2), "-") <> 0 Then
            If Asc(Mid(Cells(i, 2), InStr(Cells(i, 2), "-") - 2, 1)) > 47 And Asc(Mid(Cells(i, 2).Value, InStr(Cells(i, 2), "-") + 2, 1)) < 58 Then Cells(i, 2).Offset(, -1).Resize(, 5).Delete Shift:=xlUp
        End If
    Next i
End Sub
Post #11
Code:
Sub Another_Possibility_Column_B()
Dim cons, i As Long, rws As Range
cons = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row).Value
    For i = LBound(cons) To UBound(cons)
        If InStr(cons(i, 1), "-") <> 0 Then
            If Asc(Mid(cons(i, 1), InStr(cons(i, 1), "-") - 2, 1)) > 47 And Asc(Mid(cons(i, 1), InStr(cons(i, 1), "-") + 2, 1)) < 58 Then
                If Not rws Is Nothing Then
                    Set rws = Union(rws, Cells(i + 1, 1).EntireRow)
                        Else
                    Set rws = Cells(i + 1, 1).EntireRow
                End If
            End If
        End If
    Next i
rws.Select    '<---- Change to delete if happy.
End Sub

For Rick's code, if he permits me
Code:
Sub Maybe2()
  Dim R As Long
  For R = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
    If Cells(R, "B").Value Like "*# - #* *" Then Rows(R).Delete
  Next
End Sub
 
Upvote 0
Lets go with Post # 9 Code

Sub Maybe_Column_B()
Dim i As Long
For i = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
If InStr(Cells(i, 2), "-") <> 0 Then
If Asc(Mid(Cells(i, 2), InStr(Cells(i, 2), "-") - 2, 1)) > 47 And Asc(Mid(Cells(i, 2).Value, InStr(Cells(i, 2), "-") + 2, 1)) < 58 Then Cells(i, 2).Offset(, -1).Resize(, 5).Delete Shift:=xlUp
End If
Next i
End Sub

VBA Code:
Sub Maybe_Column_B()
Dim i As Long
    For i = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
        If InStr(Cells(i, 2), "-") <> 0 Then
            If Asc(Mid(Cells(i, 2), InStr(Cells(i, 2), "-") - 2, 1)) > 47 And Asc(Mid(Cells(i, 2).Value, InStr(Cells(i, 2), "-") + 2, 1)) < 58 Then Cells(i, 2).Offset(, -1).Resize(, 5).Delete Shift:=xlUp
        End If
    Next i
End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,215,733
Messages
6,126,541
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top