使用偏移转到下一个可见单元格

时间:2016-12-11 10:37:24

标签: excel excel-vba macros offset autofilter vba

我在excel中有一个自动过滤的表。我必须根据特定条件复制粘贴值,我必须在特定列中的所有可见单元格上执行此操作。我编写了代码并且它运行良好,但唯一的事情是它需要很多时间,因为有很多行。谁能帮助我如何捶打所需的时间?这是代码。谢谢!

body,
html,
p {
  margin: 0;
  padding: 0;
}

html {
  background-color: #666;
  overflow:hidden; /* <---------------add this----------*/
}

body {
  margin: 0 auto;
  width: 780px;
  background-color: #99ccff;
  padding: 10px;
  overflow: hidden;
}

div {
  background-color: #b57c12;
  padding: 10px;
  border: 1px solid black;
  width: 820px;
}

p {
  background-color: #f7f0b7;
  border: 1px solid whitesmoke;
}

1 个答案:

答案 0 :(得分:1)

范围复制,剪切和删除会自动仅选择已过滤范围的可见单元格。

enter image description here

Sub CopyFilteredColumn()
    Dim Target As Range

    'Size the Target range to fit the table
    'Define the starting row "C1:J19"
    'Extend the Target range to the last row .Range("C" & .Rows.Count).End(xlUp)
    'Column C is used because it will never have blank cells
    With Worksheets("Source Sheet")
        Set Target = .Range("C1:J19", .Range("C" & .Rows.Count).End(xlUp))
    End With

    Target.AutoFilter Field:=1, Criteria1:=">40", Operator:=xlAnd

    'Header and data
    'Copy the visible cells of the 3rd column of the table
    Target.Columns(3).Copy Worksheets("Target Sheet").Range("A1")
    'Data only - Includes 1 blank cell at the end
    Target.Offset(1).Columns(3).Copy Worksheets("Target Sheet").Range("C1")

End Sub