【发布时间】:2019-01-07 19:10:36
【问题描述】:
我有一个大约 15 张工作表的 Excel 工作簿。我正在寻找一种根据 K 列中的日期范围将行复制到新工作表的方法。
例子:
工作表 1: 日期范围 (1/1/15 - 1/1/18) -> 将时间范围内的所有行复制到工作表 4
工作表 2: 日期范围 (1/1/15 - 1/1/18) -> 将时间范围内的所有行复制到工作表 5
工作表 3: 日期范围 (1/1/15 - 1/1/18) -> 将时间范围内的所有行复制到工作表 6
等等
一次完成一张工作的代码,但我希望它一次完成:
Sub Date_Sample()
Application.ScreenUpdating = False
On Error GoTo M
Dim i As Long
Dim ans As Date
Dim anss As Date
Dim Lastrow As Long
Dim Lastrowa As Long
ans = InputBox("Start Date Is")
anss = InputBox("End Date Is")
Lastrowa = Sheets("Sheet1").Cells(Rows.Count, "K").End(xlUp).Row
Lastrowb = Sheets("Sheet4").Cells(Rows.Count, "K").End(xlUp).Row + 1
For i = 1 To Lastrowa
If Cells(i, "K").Value >= ans And Cells(i, "K").Value <= anss Then
Rows(i).Copy Destination:=Sheets("Sheet4").Rows(Lastrowb)
Lastrowb = Lastrowb + 1
Rows(i).EntireRow.Delete
i = i - 1
End If
Next i
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "Wrong Date"
Application.ScreenUpdating = True
End Sub
我尝试为其他工作表添加另一个 For 语句,但没有成功。
【问题讨论】:
-
1) 使用自动筛选来获取数据。 2) 循环遍历每个工作表。 3) 在工作表名称上使用 Select Case 来确定将在哪个工作表上复制数据。