根据条件删除范围内的内容 - 与定义的名称

时间:2018-05-05 20:11:26

标签: vba excel-vba excel

如果范围的第一列中的ID与带有第一个字符的已定义名称匹配,则我想运行将擦除特定范围内的行的代码(即,如果ID具有更多字符但是它与第一个字符匹配定义的名称 - 行内容应该被删除)

对于多个范围,我想这样做,但就目前而言,我正在尝试一个范围,因为它没有做到。

以下是运行代码之前的样子:

enter image description here

结果如下: enter image description here

我的代码看起来像那样,但是我收到了一条消息错误,并且我没有包含第二个范围,因为我想尝试使用第一个范围。

Sub Erase()
Dim r As Long
Dim endRow As Long
Dim StartRow As Long
Dim TargetSheet As Worksheet

Const ColumnStart1 As Integer = 2
Const ColumnEnd1 As Integer = 5

Const ColumnStart2 As Integer = 7   'to add
Const ColumnEnd2 As Integer = 10    ' to add

Const ColumnStart3 As Integer = 12   'to add
Const ColumnEnd3 As Integer = 15     'to add


Const l_MyDefinedName As String = "ID"
Dim ColumnNo As Integer
Dim ClearingRange As Range



Set TargetSheet = ThisWorkbook.Sheets("Sheet1")


With TargetSheet

StartRow = 8
Dim lngLastRow As Long
        lngLastRow = .Cells(.Rows.Count, ColumnStart1).End(xlUp).Row   '
        Set ClearingRange = .Range(.Cells(StartRow, ColumnStart1), .Cells(lngLastRow, ColumnEnd1))

Dim ID As String

      ID = ThisWorkbook.Names(l_MyDefinedName).RefersToRange.Value


    With IDRange
        Dim MachID As String


        For StartRow = 15 To ClearingRange.Rows.Count

             MatchID = Left(.Cells(StartRow, ColumnStart1), ColumnStart1)

            If MatchID = ID Then

            For ColumnNo = ColumnStart1 To ColumnEnd1
'

                 '*********Clear what is inside********'

        TargetSheet.Cells(StartRow, ColumnNo).ClearContent



            Next ColumnNo

        StartRow = StartRow + 1

            End If
Next StartRow
End With
End With


End Sub

有人可以帮忙吗?

0 个答案:

没有答案