VBA过滤表和副本

时间:2017-04-04 12:02:16

标签: vba excel-vba excel

我在excel中有一个名为“RFQ_selector”的3列表。第二列包含是/否。

  1. 我需要一个宏来过滤表格,只对第二列中包含“是”的行进行过滤。
  2. 然后宏应该将包含yes的行的左侧的每个单元格复制到同一工作表上的新位置。将它们粘贴在从单元格F25开始的列表中
  3. 我遇到了困难,请有人帮忙。 感谢

    Sub CopyYes()
        Dim c As Range
        Dim j As Integer
        Dim Source As Worksheet
        Dim Target As Worksheet
    
        ' Change worksheet designations as needed
        Set Source = ActiveWorkbook.Worksheets("Trader")
        Set Target = ActiveWorkbook.Worksheets("Sheet2")
    
        j = 1     ' Start copying to row 1 in target sheet
        For Each c In Source.Range("C8:C22")   ' Do 30 rows
            If c = "yes" Then
               Source.Rows(c.Row).Copy Target.Rows(j)
               j = j + 1
            End If
        Next c
    End Sub
    

1 个答案:

答案 0 :(得分:0)

我已修改您的子广告以反映您所需的更改:

  
      
  • 将每个单元格复制到同一张上的新位置的包含“是”的行的左侧。将它们粘贴在列表中   在单元格F25
  •   

它没有过滤,您提供的代码中没有发生过滤,但输出只包含"是"列

Sub CopyYes()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    'Target worksheet not needed, pasting to source worksheet

    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets("Sheet1")

    j = 25                                  'Start copying to F25
    For Each c In Source.Range("B2:B30")    'Change the range here to fit the range in which your data for Yes/No is stored
        If c = "Yes" Then                   'Verify capitalization here, difference between "Yes" and "yes"
           c.Offset(0, -1).Copy Source.Range("F" & j)   'Copy the cell to the left of the Yes/No column and paste on same sheet starting at row F25
           j = j + 1
        End If
    Next c
End Sub