简体   繁体   English

遍历多个工作表时在Union上出错

[英]Error on Union when looping through multiple worksheets

I'm trying to delete rows in one sheet that match criteria from rows taken from multiple other worksheets. 我正在尝试从多个其他工作表中获取的行中删除匹配条件的一个表中的行。 I need to cycle through all the other worksheets in my workbook, and each time I find something that matches, I delete the entire row in the first sheet. 我需要循环浏览工作簿中的所有其他工作表,并且每次找到匹配的内容时,都会删除第一张工作表中的整个行。 I'm getting Error 1004 on Union. 我在联盟上收到错误1004。 I thought this might be caused because you can't use Union across sheets, so I set it to Nothing after each sheet. 我认为可能是由于您无法跨工作表使用Union所致,因此我将其设置为每张工作表之后为Nothing。 I'm still getting the same error. 我仍然遇到相同的错误。

Here's the code: 这是代码:

Sub findRemaining()
Dim rngToDel As Range
Dim fRng As Range 'Fund range
Dim wCell As Range 'Working sheet cell
Dim wRng As Range 'Working sheet range
Dim WS_Count As Integer
Dim I As Integer

Set fRng = Worksheets("All").Range("B2:B1495")
WS_Count = ActiveWorkbook.Worksheets.Count

For I = 2 To WS_Count
    Set rngToDel = Nothing
    Set wRng = Worksheets(I).Range("B2:B200")
    For Each wCell In wRng 'Loop through all working cells
        ' if wCell found in Fund range then delete row
        If Not IsError(Application.Match(Trim(wCell.Value), fRng, 0)) Then
            If rngToDel Is Nothing Then
                Set rngToDel = wCell
            Else
                Set rngToDel = Union(rngToDel, wCell)
            End If
        End If
    Next wCell
    If Not rngToDel Is Nothing Then rngToDel.EntireRow.Delete
Next I


End Sub

The problem was I set range to delete as wCell in wRng , but really what I wanted to delete was cells in fRng . 问题是我在wRng中将范围设置为wCell删除,但实际上我要删除的是fRng中的单元fRng By fixing that the rest of the code worked perfectly. 通过修复,其余代码可以完美运行。 (thanks chris) (感谢克里斯)

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

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