我一直试图在excel中建立一个待办事项列表。我决定写一个宏来检查特定条件,如果满足则复制待办事项。
我是VBA初学者,所以花时间寻找和学习并将以下代码整理在一起。请您查看一下,并在这些方面提供一些帮助吗?
.End(xlUp)
更改该行以粘贴到特定的单元格I20,并向下填充每个项目。感谢您的帮助,
Sub today()
Dim StartDate As Long
Dim EndDate As Long
StartDate = DateSerial(Year(Date), Month(Date), Day(Date))
EndDate = DateSerial(Year(Date), Month(Date), Day(Date) + 6)
For Row = 1 To 100
If Worksheets("sheet1").Cells(Row, 6).Value >= StartDate And Worksheets("sheet1").Cells(Row, 6).Value <= EndDate And Worksheets("sheet1").Cells(Row, 4).Value <> "Complete" Then
Worksheets("sheet1").Cells(Row, 2).Copy
Worksheets("Sheet1").Cells(Rows.Count, "I").End(xlUp).Offset(1, 0).PasteSpecial
ActiveSheet.Paste
Application.CutCopyMode = False
End If
Next Row
End Sub
答案 0 :(得分:0)
代替
Worksheets("sheet1").Cells(Row, 2).Copy
Worksheets("Sheet1").Cells(Rows.Count, "I").End(xlUp).Offset(1, 0).PasteSpecial
ActiveSheet.Paste
Application.CutCopyMode = False
您可以尝试
Worksheets("Sheet1").Cells(Rows.count,"I").End(xlup).offset(1,0).value = worksheets("Sheet1").Cells(Row,2).Value
我认为您引用的是正确的单元格(最后一个单元格+ 1行),但是ActiveSheet.Paste
弄乱了它。在复制/粘贴旁边比获取值慢。
编辑:如果要从I20开始,但未填充I19,则可以先确定行,然后再设置值:
Dim rowNum as Long
rowNum = application.worksheetfunction.max(20,Worksheets("Sheet1").Cells(Rows.count,"I").End(xlup).offset(1,0).Row)
Worksheets("Sheet1").Cells(rowNum,"I").Value = Worksheets("Sheet1").Cells(Row,2).Value