删除两个变量字符串值之间的动态行数?

时间:2016-12-21 14:28:59

标签: excel

我有一个包含太多变量的文件供我处理。我想在文件中删除标题行之间的所有行(以H开头)。

每个标题行在之前和之后都有一致的行(T ...之后和E ...之前。)

我需要将以T开头的行删除到以E开头的行。

H100000

T100000

S100000

E100000

H100001

T100001

S100001

S100001

C100001

N100001

E100001

H100002

T100002

S100002

M100002

U100002

U100002

C100002

M100002

Y100002

E100002

H100003

T100003

S100003

N100003

E100003

H100004

如果有人可以帮助我,我将非常感激!我在另一个网站上收集了以下表格的旧帖子,但字符串限定符必须准确。我无法使用通配符。

Sub DeletebyBookends()
Dim strStart As String, strEnd As String
Dim DELETEMODE As Boolean
Dim DelRng As Range
    strStart = "H100000"
    strEnd = "H100001"

    DELETEMODE = False
    For r = 1 To Range("C" & Rows.Count).End(xlUp).Row  'first to last used row

        If Range("C" & r).Value = strEnd Then DELETEMODE = False

        If DELETEMODE Then
            'Create a Delete Range that will be used at the end
            If DelRng Is Nothing Then
                Set DelRng = Range("A" & r)
            Else
                Set DelRng = Application.Union(DelRng, Range("A" & r))
            End If
        End If

        If Range("C" & r).Value = strStart Then DELETEMODE = True
    Next r

    'Delete the Range compiled from above
    If Not DelRng Is Nothing Then DelRng.EntireRow.Delete xlShiftUp

End Sub

0 个答案:

没有答案