0

我有几个关于 VBA 的问题,希望你们能帮助我。我是 VBA 的新手,因此非常感谢您提供的任何帮助。

目标- 如果在“valueRange”中找到类似的值,则从“cellRange”中删除所有行

到目前为止的代码

Sub DeleteRows()

Set valueRange = Worksheets("Delete Rows").Range("A4:A65000")
Set cellRange = Worksheets("Load File").Columns(Worksheets("Delete Rows").Range("F1").Value)

For Each Cel In cellRange.Cells
    For Each Value In valueRange.Cells
        If Cel.Value = Value.Value Then
            Cel.EntireRow.Delete
        End If
    Next Value
Next Cel
End Sub

问题 1: valueRange 并不总是填充所有 65000 行。我怎样才能做到这一点,以便范围只从 A4 中获取那些:(直到它碰到一个空列)

问题 2:与问题 1 类似,但 cellRange

问题3:每当删除一行时,似乎都会影响范围的设置方式。这意味着如果它删除第 10 行,则循环转到第 11 行,而不再次检查第 10 行。我怎样才能告诉外观做第二遍或再次浏览文件。

4

3 回答 3

2

P1:这里有两个选项
a)如果Cel.Value是空的,退出对于
b)正确的范围选择,请在这里参考这个人:Excel:选择所有行直到空单元格

P2:同上

P3:由于 For-Each 不能“向后”,您可以做的最好的事情是
a) 不要删除该行,而是将它的数字存储在一个 Long 数组中,然后添加一个 For-Next 并删除“标记”行如:

For x = UBound(myLongArray)-1 To 0 Step -1
    cel(x).EntireRow.Delete
Next x

b)而不是 For-Each,将行数(通过ROWS函数)存储在变量中,并使用“Step -1”循环遍历行

于 2013-04-19T13:56:23.197 回答
1

正如其他人所提到的,删除时您必须后退一步。

此外,我进行了修改以避免对ValueRange中的每个单元格进行不必要的迭代,而是使用该Match()函数检查.Cel.ValueValueRange

Sub DeleteRows()
Dim r as Long
Dim valueRange as Range, cellRange as Range
Dim Cel as Range

Set valueRange = Worksheets("Delete Rows").Range("A4:A65000").End(xlUp) '<~~ Get the last unused row
Set cellRange = Worksheets("Load File").Columns(Worksheets("Delete Rows").Range("F1").Value)

For r = cellRange.Cells.Count to 1 Step -1  '<~~ When deleting rows you must step backwards through the range to avoid the error you are encountering.'
    Set Cel = cellRange.Cells(r)

    'Check to see if Cel.Value exists in the ValueRange using the "Match" function'
    If Not IsError(Application.Match(Cel.Value,ValueRange,False) Then
        Cel.EntireRow.Delete
    End If
Next r
End Sub
于 2013-04-19T13:57:08.547 回答
0

干得好。

' Declare your variables to get intellisense
Dim rngDelete As Range
Dim cellRange As Range
Dim valueRange As Range

' Get only the rows with data
Set valueRange = Worksheets("Delete Rows").Range("A4")
If valueRange.Offset(1, 0) <> "" Then
    Set valueRange = Worksheets("Delete Rows").Range(valueRange, valueRange.End(xlDown))
End If

' Get only the rows with data
Set cellRange = Worksheets("Load File").Cells(Worksheets("Delete Rows").Range("F1").value,1)
If cellRange.Offset(1, 0) <> "" Then
    Set cellRange = Worksheets("Load File").Range(cellRange, cellRange.End(xlDown))
End If

Dim cel As Range
Dim value As Range

' make cel your outer since it has more rows
For Each cel In cellRange.Cells
    For Each value In valueRange.Cells
        If value.value = cel.value Then
            ' Don't delete it yet but store it in a list
            If rngDelete Is Nothing Then
                Set rngDelete = cel.EntireRow
            Else
                Set rngDelete = Union(rngDelete, cel.EntireRow)
            End If
            ' no need to look further
            Exit For
        End If
    Next
Next

' Wipe them out all at once
rngDelete.Delete
于 2013-04-19T21:28:13.467 回答