VBA - 过滤/剪切/粘贴

时间:2017-02-22 09:15:55

标签: excel-vba vba excel

我想知道如何将VBA语句写入

  1. I
  2. 中的过滤条件1(总销售额)
  3. 复制标准1
  4. 将数据粘贴到列K
  5. (净销售额)列I
  6. 中的过滤条件2
  7. 复制标准2
  8. 将数据粘贴到列L
  9. 目前我的代码如下:

    Range("A1:J1").AutoFilter Field:=9, Criteria1:="Gross Sale"
    Columns("2" & ":" & Range("J70000").End(xlUp).Row).Copy
    

    先谢谢你们。

    Screenshot of Data

1 个答案:

答案 0 :(得分:0)

尝试下面的代码(将“Sheet6”修改为工作表的名称):

Option Explicit

Sub CopyFiltResults()

Dim FiltRng As Range

With Worksheets("Sheet6") ' modify "Sheet6" to your sheet's name
    With .Range("A1:J" & .Cells(.Rows.Count, "I").End(xlUp).Row)
        ' set range filter column I to "Gross Sale"
        .AutoFilter Field:=9, Criteria1:="Gross Sale"

        Set FiltRng = .Columns(9).SpecialCells(xlCellTypeVisible)
        FiltRng.Copy .Range("K1") '<-- only copy visible cells to Column K
        Set FiltRng = Nothing '<-- clear range variable

        ' set range filter column I to "Net Sales"
        .AutoFilter Field:=9, Criteria1:="Net Sales"
        Set FiltRng = .Columns(9).SpecialCells(xlCellTypeVisible)
        FiltRng.Copy .Range("L1") '<-- only copy visible cells to Column L
    End With
End With

End Sub