简体   繁体   English

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

[英]Clear the rows' content in the array - If with loop

I want to run the code that will clear the rows in specific ranges of my sheet. 我想运行将清除工作表中特定范围内的行的代码。 I have also the condition which rows' content should be cleared ie if the ID in the first column of my range matches the defined name with the first characters (ie if the ID in the column has more characters than a defined name but it matches with the first characters - the row content should be erased ) 我也有一个条件,应该清除行的内容,即,如果我范围的第一列中的ID与定义的名称与第一个字符相匹配(即,如果列中的ID具有比定义的名称更多的字符,但与前一个字符-应删除行内容)

I would like to that for a few ranges but for now, I am trying on one range as it doesn't come as it should. 我想在几个范围内使用该功能,但现在,我正在尝试一个范围,因为它没有达到应有的水平。

Here is how the case looks before running the code: 这是运行代码之前的情况: 在此处输入图片说明

This my DESIRED outcome => the rows in the array where ID matches to defined name clear in the range: 这是我期望的结果=>数组中ID与定义名称匹配的行在以下范围内清除: 在此处输入图片说明

My code does not repsond at all. 我的代码根本没有响应。 No error message, nothing and within that no outcome I expect: 没有错误消息,什么也没有,我期望没有结果:

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

Anybody who could help on that? 有人可以帮忙吗?

tl;dr TL;博士

Here is a version. 这是一个版本。 I have deliberately stuck with your idea of using constants to set up individual ranges to work with, setting up startRow and the like to help you see how your script could evolve into what is shown below. 我故意坚持使用常量来设置要使用的单个范围,设置startRow等以帮助您了解脚本如何演变为如下所示的想法。

You are working with 3 distinct ranges whose boundaries you set with constants. 您正在使用3个不同的范围,这些范围的边界设置为常数。 I put those into an array which is looped. 我将它们放入循环的数组中。 I access the items in start column and end column pairs, with Index and Index + 1, to set up each clear range. 我使用索引和索引+ 1访问开始列和结束列对中的项目,以设置每个清除范围。 I use step 2 in the loop so pairs don't overlap. 我在循环中使用了第2步,因此线对不会重叠。

I then test the first column of that range for the presence of the ID. 然后,我测试该范围的第一列是否存在ID。 If present, I gather that in to an union'd range which I resize to the number of columns in the clear range, eg If a row in column B has 1234 in it, I would resize the cell in that row in column B to B:E in that row and add it to the union'd range. 如果存在,我将其收集到一个并集范围内,然后将其调整为清除范围内的列数,例如,如果B列中的一行包含1234 ,则将B列中该行的单元格调整为B:E在该行中,并将其添加到并集范围内。 Bit like putting ranges into a basket to hold them to deal with later. 有点像把篮子放进篮子里,以备不时之需。

At the end I test if unionRng, the union'd ranges, is not nothing ie if the basket has something in it; 最后,我测试unionRng(union'd range)是否不是空,即篮子中是否有东西。 meaning matches found, and then clear the contents of those cells. 意思是找到匹配项,然后清除那些单元格的内容。

Note: 注意:

  1. It requires you to have a named range called "ID ", and that that corresponds with D3 in sheet1. 它要求您有一个名为"ID ”的命名范围,该范围与sheet1中的D3相对应。
  2. I have changed the notation to match with norms for Constants which is all upper case. 我已经更改了表示法以与常量的规范匹配,这些规范都是大写的。 I am not a fan of using "_" in variables/constants. 我不喜欢在变量/常量中使用“ _”。 I have put here for the sake of legibility. 为了清晰起见,我把这里放在这里。 Maybe consider different names. 也许考虑使用不同的名称。

Code: 码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM