如何在不同工作表的循环中引用活动单元格?

问题描述 投票:0回答:1

我被困在正确读取的地方,我有两行满足“如果”条件,但它在不同的纸张上复制了 A 行两次,而不是第 8 行和第 10 行。有没有办法让它识别一个一次一个单元格,如果满足条件则复制它。

我尝试过以下代码

Sub RunReport()
    Dim srcSheet As Worksheet
    Dim rptSheet As Worksheet
    Dim i As Range
    Dim srcSheets As Variant
    
        Set rptSheet = ThisWorkbook.Sheets("Report")
        Set srcSheets = Worksheets(Array("SheetA", "SheetB", "SheetC", "SheetD"))
        
        For Each srcSheet In srcSheets
            For Each i In srcSheet.Range("A5:W358")
                If srcSheet.Range(i, "B").Value <> "" And srcSheet.Range(i, "O").Value = "" Then
                    srcSheet.Cells(i).EntireRow.Copy Destination = rptSheet.Cells(rptSheet.Rows.Count, "A").End(xlUp).Offset(1, 0)
                End If
            Next i
        Next srcSheet
        
End Sub
excel vba loops reference copy
1个回答
0
投票
  • i
    是一个范围对象,代码应该如下所示。
i.EntireRow.Copy Destination:= rptSheet.Cells(rptSheet.Rows.Count, "A").End(xlUp).Offset(1, 0)
© www.soinside.com 2019 - 2024. All rights reserved.