在工作簿中五张纸上的三张(面包,花卉,杂货)的B列中,我想在B列中找到带有单词Flyer的行。每个工作表中将有多个带有单词在B列中。找到单词Flyer时,会将整个行粘贴到Sheet1中。

我将其用于一个选项卡,但希望使用相同的代码搜索所有三个选项卡(但不是全部五个...这是问题所在),然后将B列中带有单词Flyer的所有行粘贴到Sheet1中。

我的代码(有效,但仅在“面包店”选项卡上):

Sub CopyRowsFlyer()
'This looks in the Bakery tab and moves everything that has "Flyer" in     column B to Sheet 1
  Dim bottomB As Integer
  Dim x As Integer
  bottomB = Sheets("Bakery").Range("B" & Rows.Count).End(xlUp).Row: x = 1

  Dim c As Range
  For Each c In Sheets("Bakery").Range("B3:B" & bottomB)
    If c.Value = "Flyer" Then
        c.EntireRow.Copy Worksheets("sheet1").Range("A" & x)
        x = x + 1
    End If
  Next c
End Sub

最佳答案

与发布的其他解决方案类似。很简单替换范围检查的边界。最小的变量。没有执行中间尺寸。

Sub CopyRowsFlyer()
  Dim strSh As Variant, c As Range, x As Integer
  x = 1

  For Each strSh In Array("Bakery", "Floral", "Grocery")
    For Each c In Worksheets(strSh).Range("B:B")
      If c = "" and c.Row > 2 Then
        Exit For
      ElseIf c = "Flyer" and c.Row > 2 Then
        c.EntireRow.Copy Worksheets("Sheet1").Range("A" & x)
        x = x + 1
      End If
    Next
  Next
End Sub

10-07 12:26
查看更多