我在workbook1中有一个excel表格,我想使用VBA根据当天(星期日,星期一......)复制该表格中的数据行,但是要另一个工作簿(天)每天都有一张单独的表格。
我发现的所有示例都是从一个工作簿复制到另一个工作簿中的一个工作表
你能帮帮我吗?
我正在使用这段代码,但是当我试图重复其他日子时,我感到很困惑,特别是在使用open&保存方法
Sub myTest()
Dim LastRow As Integer, i As Integer, erow As Integer
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 2).Value = "Sunday" Then
Range(Cells(i, 1), Cells(i, 7)).Select
Selection.Copy
Workbooks.Open Filename:="C:\Users\User1\Documents\Days.xlsx"
Worksheets("Sunday").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
End Sub
答案 0 :(得分:1)
只需添加更多天数。 下面的代码也将在周一添加,在本周的其他时间执行相同的操作
Sub myTest()
Dim LastRow As Integer, i As Integer, erow As Integer
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 2).Value = "Sunday" Then
Range(Cells(i, 1), Cells(i, 7)).Select
Selection.Copy
Workbooks.Open Filename:="C:\Users\User1\Documents\Days.xlsx"
Worksheets("Sunday").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
If Cells(i, 2).Value = "Monday" Then
Range(Cells(i, 1), Cells(i, 7)).Select
Selection.Copy
Workbooks.Open Filename:="C:\Users\User1\Documents\Days.xlsx"
Worksheets("Monday").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
End Sub
您还可以通过仅打开其他工作簿一次并仅在之后保存它来提高效率。
答案 1 :(得分:0)
如果您正在查看的代码仅复制到另一张纸上,那么您可以重复其他日子的代码。