清除数组中行的内容 - 如果有循环

时间:2018-05-07 18:56:04

标签: vba excel-vba excel

我想运行代码来清除工作表特定范围内的行。我还有条件应该清除行的内容,即如果我的范围的第一列中的ID与定义的名称与第一个字符匹配(即,如果列中的ID具有比定义的名称更多的字符但是它匹配第一个字符 - 行内容应该删除)

我想在一些范围内,但就目前而言,我正在尝试一个范围,因为它没有应有的范围。

以下是运行代码之前的案例: enter image description here

这是我渴望的结果=>数组中的行与ID匹配的行在以下范围内清除: enter image description here

我的代码根本没有代表。没有任何错误消息,没有任何内容,并且我没有预料到这一点:

Option Explicit
Sub EraseArray()
Dim r As Long
Dim endRow As Long
Dim StartRow As Long
Dim TargetSheet As Worksheet

Const ColumnStart1 As Long = 2
Const ColumnEnd1 As Long = 5

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

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


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



Set TargetSheet = ThisWorkbook.Sheets("Sheet1")


With TargetSheet

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

Dim ID As String

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


    With ClearRange
        Dim MatchID As String


        For StartRow = 15 To ClearRange.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

有人可以提供帮助吗?

1 个答案:

答案 0 :(得分:1)

<强> TL;博士

这是一个版本。我故意坚持使用常量设置各个范围来设置,设置startRow等,以帮助您了解脚本如何演变成下面显示的内容。

您正在使用3个不同的范围,其边界使用常量设置。我将它们放入一个循环的数组中。我使用Index和Index + 1访问开始列和结束列对中的项目,以设置每个清除范围。我在循环中使用第2步,因此对不重叠。

然后我测试该范围的第一列是否存在 ID。如果存在,我将其收集到一个联合范围,我将其调整为清晰范围内的列数,例如如果列B中的行中包含1234,我会将该行中该行的单元格调整为该行中的B:E并将其添加到union'd范围。有点像将范围放入一个篮子中以便稍后处理它们。

最后,我测试unionRng,联合范围,并不是什么,即篮子里面有什么东西;找到意义匹配,然后清除那些单元格的内容。

注意:

  1. 它要求您有一个名为"ID的命名范围,并且与sheet1中的D3相对应。
  2. 我已经改变了符号与常量的规范相匹配,这是大写的。我不喜欢在变量/常量中使用“_”。为了易读起见,我放在这里。也许考虑不同的名字。
  3. <强>代码:

    Option Explicit 
    Public Sub ClearCells()
    
        Const COLUMN_START1 As Long = 2
        Const COLUMN_END1 As Long = 5
        Const COLUMN_START2 As Long = 7
        Const COLUMN_END2 As Long = 10
        Const COLUMN_START3 As Long = 12
        Const COLUMN_END3 As Long = 15
        Const START_ROW As Long = 8
        Const L_MY_DEFINED_NAME As String = "ID"
    
        Dim loopRanges()
    
        loopRanges = Array(COLUMN_START1, COLUMN_END1, COLUMN_START2, COLUMN_END2, COLUMN_START3, COLUMN_END3)
    
        Dim targetSheet As Worksheet, index As Long, unionRng As Range, 
        Dim id As Long                               'Or , ID As String?
    
        Set targetSheet = ThisWorkbook.Sheets("Sheet1")
        id = ThisWorkbook.Names(L_MY_DEFINED_NAME).RefersToRange.Value
    
        Application.ScreenUpdating = False
    
        With targetSheet
    
            For index = LBound(loopRanges) To UBound(loopRanges) Step 2
    
                Dim lngLastRow As Long, ClearRange As Range,rng As Range
    
                lngLastRow = .Cells(.Rows.Count, loopRanges(index)).End(xlUp).Row '
                If lngLastRow < START_ROW Then lngLastRow = START_ROW
    
                Set ClearRange = .Range(.Cells(START_ROW, loopRanges(index)), .Cells(lngLastRow, loopRanges(index + 1)))
    
                For Each rng In ClearRange.Columns(1).Cells
                    If Not IsEmpty(rng) Then
                        If Left$(rng.Value, Len(id)) = id Then '<== match found
                            If Not unionRng Is Nothing Then
                                Set unionRng = Union(unionRng, rng.Resize(1, ClearRange.Columns.Count)) '<== gather all matches into a union range
                            Else
                                Set unionRng = rng.Resize(1, ClearRange.Columns.Count)
                            End If
                        End If
                    End If
                Next rng
            Next index
        End With
    
        If Not unionRng Is Nothing Then Debug.Print unionRng.Address '<== or unionRng.ClearContents
        Application.ScreenUpdating = True
    End Sub