仅循环通过可见细胞

时间:2017-10-03 19:19:12

标签: excel vba excel-vba

我有一个代码可以在另一个工作表中找到新值(sh1)并将它们添加到sheet2(sh2),但是,在此之前,宏会过滤一些特定的值(" CONSULTANT I",& #34; PROMOTER I"," PROMOTER II"," SALES PROMOTER I")。我只需要考虑过滤结果(可见行)作为新值,我该如何实现?

按照我的代码 -

 ActiveSheet.ListObjects("TabLE1").Range.AutoFilter Field:=14, Criteria1:= _
        Array("CONSULTANT I", "PROMOTER I", "PROMOTER II", _
        "SALES PROMOTER I"), Operator:=xlFilterValues


   Set Sh2 = ThisWorkbook.Worksheets("STAFF")
   Set Sh1 = Workbooks("MIS25044.xlsb").Sheets(1)

ThisWorkbook.Activate
   Lastrow = Sh2.Range("a65536").End(xlUp).Row

   iRow1 = 2

   'Loop through values in column A of Sheet2
   Do
      Set FindCell = Sh2.Range("a2", Sh2.Cells(Lastrow, "A")).Find(What:=Sh1.Cells(iRow1, "E"), _
             After:=Sh2.Range("A2"), LookIn:=xlValues, LookAt:=xlWhole)
      If FindCell Is Nothing Then
         'add to bottom of list
         Lastrow = Lastrow + 1
         Sh2.Cells(Lastrow, "A") = Sh1.Cells(iRow1, "E")
      End If
      iRow1 = iRow1 + 1
   Loop Until IsEmpty(Sh1.Cells(iRow1, "E"))

1 个答案:

答案 0 :(得分:0)

Find()已经跳过了使用过滤器隐藏行的任何值。

如果您手动隐藏了行,请注意:例如,可以使用Find()

找到手动隐藏的行上的值