在单元格中删除特定单词和所有内容

时间:2016-04-15 03:23:01

标签: excel vba excel-vba replace excel-2010

我在地址栏

上删除 APT STE 后的所有内容时遇到问题

如果你看一下结果,我的vba代码就是删除字母 ste 2121 STE VENSON LN )或的易于

在此之后删除APT或STE以及所有内容的最佳方法是什么?

以下是我的代码,

Option Explicit
Sub Remove()
    Dim Sht As Worksheet

    Set Sht = ActiveWorkbook.Sheets("Data")
    With Sht.Range("E:E")
        .Replace "APT*", "", xlPart
        .Replace "STE*", "", xlPart
    End With

End Sub

我的数据

+-------+-----+-----+----------+----------------------------------+-------+------+-----+
| Route | Pcs | Wgt | Location |             Address              | Suite | City | Zip |
+-------+-----+-----+----------+----------------------------------+-------+------+-----+
| SD-26 |   1 |   3 |          | 5555 SOUTHWESTERN BLVD           |       |      |     |
| SD-26 |   1 |   7 |          | 6666 EASTERN AVE APT 100         |       |      |     |
| SD-05 |   1 |   1 |          | 161112 HOMESTEAD ST              |       |      |     |
| SD-05 |   2 |   8 |          | 2221 STEVENSON LN                |       |      |     |
| SD-04 |   1 |   8 |          | 4040 OLD DENTON RD APT 2104      |       |      |     |
| SD-04 |   1 |   3 |          | 15811 E FRANKFORD RD APT 1507    |       |      |     |
| SD-04 |   1 |   1 |          | 835 WESTMINSTER DR               |       |      |     |
| SD-03 |   1 |   5 |          | 9001 LAKESIDE CIR APT 5203       |       |      |     |
| SD-03 |   1 |   3 |          | 8880 UNION STATION PKWY APT 2104 |       |      |     |
| SD-03 |   1 |   1 |          | 420 E MAIN ST STE E              |       |      |     |
+-------+-----+-----+----------+----------------------------------+-------+------+-----+

结果

+-------+-----+-----+----------+--------------------------+-------+------+-----+
| Route | Pcs | Wgt | Location |         Address          | Suite | City | Zip |
+-------+-----+-----+----------+--------------------------+-------+------+-----+
| SD-26 |   1 |   3 |          | 5555 SOUTHWE             |       |      |     |
| SD-26 |   1 |   7 |          | 6666 EA                  |       |      |     |
| SD-05 |   1 |   1 |          | 161112 HOME              |       |      |     |
| SD-05 |   2 |   8 |          | 2221                     |       |      |     |
| SD-04 |   1 |   8 |          | 4040 OLD DENTON RD       |       |      |     |
| SD-04 |   1 |   3 |          | 15811 E FRANKFORD RD     |       |      |     |
| SD-04 |   1 |   1 |          | 835 WESTMIN              |       |      |     |
| SD-03 |   1 |   5 |          | 9001 LAKESIDE CIR        |       |      |     |
| SD-03 |   1 |   3 |          | 8880 UNION STATION PKWY  |       |      |     |
| SD-03 |   1 |   1 |          | 420 E MAIN ST            |       |      |     |
+-------+-----+-----+----------+--------------------------+-------+------+-----+

1 个答案:

答案 0 :(得分:3)

在我看来,你可以在APT / STE之前和之后但在通配符之前加一个空格。

Sub RemoveAptSte()
    Dim Sht As Worksheet

    Set Sht = ActiveWorkbook.Sheets("Data")
    With Sht.Range("E:E")
        .Replace " APT *", vbNullString, xlPart
        .Replace " STE *", vbNullString, xlPart
    End With

End Sub

这应该从考虑中消除任何误报。

apt_ste