MS-Excel 筛选数据后复制新工作表中的特定单元格
MS-Excel copy specific cells in new worksheet after filtreing data
我的工作表 "Recalculated FS" 包含以下列:A、B、C、D、E ..AI
如果 AI = "YES",我想过滤我的工作表,然后在新工作表 "Sheet2" 中复制 B 列和 D 列,我有这段代码可以复制所有列,但我不知道该怎么做B 和 D 列,
Ps :我还想重命名 header 的 "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
下面的代码将B列和D列复制到工作表Sheet2,当列 AI = 是时。
编辑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
我的工作表 "Recalculated FS" 包含以下列:A、B、C、D、E ..AI 如果 AI = "YES",我想过滤我的工作表,然后在新工作表 "Sheet2" 中复制 B 列和 D 列,我有这段代码可以复制所有列,但我不知道该怎么做B 和 D 列, Ps :我还想重命名 header 的 "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
下面的代码将B列和D列复制到工作表Sheet2,当列 AI = 是时。
编辑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