我的工作表“重新计算的FS”包含列:A,B,C,D,E ..AI 如果AI =“YES”,我想过滤我的工作表,然后在新的工作表“Sheet2”中复制列B和D,我有这个代码复制所有列,我不知道如何为列B和D工作, Ps:我还要重命名“Sheet2”的标题,B - > columnB和C - > columnC
Sub tgr()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Set wsData = Sheets("Recalculated FS")
Set wsDest = Sheets("Sheet2")
With wsData.Range("AI2", wsData.Cells(Rows.Count, "AI").End(xlUp))
.AutoFilter 1, "YES"
.CurrentRegion.Copy wsDest.Range("A1")
.AutoFilter
End With
End Sub
请帮忙吗? 现在我需要添加到过滤器1(AI列),另一个过滤器2(列AK),所以我该如何在此代码中执行此操作:
Option Explicit
Sub tgr()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Set wsData = Sheets("Recalculated FS")
Set wsDest = Sheets("Sheet2")
' set wsData sheet to be active, to allow filterring
wsData.Select
With wsData.Range("AI2", wsData.Cells(Rows.Count, "AI").End(xlUp))
' Column AI is col num 35
.AutoFilter 35, "YES"
End With
wsData.Columns("B:B").Select
Selection.Copy
wsDest.Select
Columns("B:B").Select
ActiveSheet.Paste
wsData.Select
wsData.Columns("D:D").Select
Selection.Copy
wsDest.Select
Columns("D:D").Select
ActiveSheet.Paste
wsDest.Range("B1").Value = "ColumnB"
wsDest.Range("D1").Value = "ColumnD"
End Sub
答案 0 :(得分:1)
当列AI =是时,下面的代码将 B列和 D列复制到工作表 Sheet2 。
修改1 :将 B列和 D列逐个复制到 Sheet 2 (不复制列C)
编辑2 :添加第二列过滤条件AK =是
Option Explicit
Sub tgr()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Set wsData = Sheets("Recalculated FS")
Set wsDest = Sheets("Sheet2")
' set wsData sheet to be active, to allow filterring
wsData.Select
With wsData.Range("AI2", wsData.Cells(Rows.Count, "AI").End(xlUp))
' Column AI is col num 35
.AutoFilter 35, "YES"
.AutoFilter 37, "YES"
End With
wsData.Columns("B:B").Select
Selection.Copy
wsDest.Select
Columns("B:B").Select
ActiveSheet.Paste
wsData.Select
wsData.Columns("D:D").Select
Selection.Copy
wsDest.Select
Columns("D:D").Select
ActiveSheet.Paste
wsDest.Range("B1").Value = "ColumnB"
wsDest.Range("D1").Value = "ColumnD"
End Sub