【发布时间】:2014-03-30 06:09:55
【问题描述】:
希望使用 VBA 从除我的活动工作表之外的所有工作表中复制满足 J 列中特定条件的行。
没有用 VBA 编写代码的经验,所以我试图通过查看其他问题和答案将必要的部分组合在一起;
下面是我目前写的代码;
Sub CommandButton1_Click()
Dim lngLastRow As Long
Dim ws As Worksheet
Dim r As Long, c As Long
Dim wsRow As Long
Set Controlled = Sheets("Controlled") ' Set This to the Sheet name you want all Ok's going to
Worksheets("Controlled").Activate
r = ActiveSheet.Cells(Rows.Count, 2).End(x1up).Row
c = ActiveSheet.Cells(1, Columns.Count).End(x1ToLeft).Column
Range("J").AutoFilter
For Each ws In Worksheets
If ws.Name <> "Controlled" Then
ws.Activate
wsRow = ActiveSheet.Cells(Rows.Count, 2).End(x1up).Row + 1
Range("A" & r).AutoFilter Field:=10, Criteria1:="Y"
.Copy Controlled.Range("A3" & wsRow)
End If
Next ws
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
其中 Controlled 是我希望数据出现在其他工作表中的工作表,并搜索所有其他工作表以查看其列 J 是否符合条件 =“Y”
我不需要复制格式,因为所有表格的格式都完全相同,如果可能,我希望复制的行从第 3 行开始
【问题讨论】:
-
仅供参考 - 在 for 循环之后您有一个额外的
End If -
查看我的帖子,我假设您在所有工作表中的数据在第一行都有标题。另外,我假设您在第二行的
Controlled Sheet中有标题,因此数据将在第三行复制。
标签: vba excel autofilter