VBA-选择特定的列

时间:2019-02-22 09:19:01

标签: excel vba

我正在寻找此代码的替代方法。我只需要选择第一列和第五列

2019-03-01

在这一部分中,我写了Sub Copy() Dim Source As Worksheet Dim Target As Worksheet Dim valsArray As Variant valsArray = Array("A,B,C") '<--| define your values to be filtered on Source sheet column A ' Change worksheet designations as needed Set Source = ActiveWorkbook.Worksheets("Sheet1") Set Target = ActiveWorkbook.Worksheets("Sheet2") With Source '<--| reference Source sheet With .Range("A1:A1000") '<--| reference its range from A1 to A1000 .AutoFilter Field:=1, Criteria1:=valsArray, Operator:=xlFilterValues '<--| filter referenced range on its first column with values stored in valsArray If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any cell filtered other than .Resize(.Rows.Count - 1, 5).Offset(1).SpecialCells(xlCellTypeVisible).Copy Target.Range("A1") '<--|copy filtered cells skipping headers and paste in target sheet from cell A1 End If End With .AutoFilterMode = False End With End Sub ,它从第一列到第五列进行选择,但是我只需要第一列和第五列。

有什么办法只能获取那些列?

谢谢

1 个答案:

答案 0 :(得分:1)

使用2条单独的。将行的大小调整为A1和B1

  .Resize(.Rows.Count - 1, 1).Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Target.Range("A1")
  .Resize(.Rows.Count - 1, 1).Offset(1, 4).SpecialCells(xlCellTypeVisible).Copy Target.Range("B1")