根据单元格的值select行并将其粘贴到新工作表excel vba中

我正在尝试根据列A中的值复制整个行。标准将在昨天的date。 macros需要保持dynamic,因为每天这个macros将运行并使用前一天的数据。 这是我到目前为止:

Sub SelectRowsByDate() Dim WS1 As Worksheet Set WS1 = ThisWorkbook.Sheets("Test") Dim YesterdayDate As Date Dim loopCounter As Long YesterdayDate = Date - 1 For loopCounter = 1 To Rows.Count If Cells(i, 1).Value = YesterdayDate Then Rows(i).Select End If End Sub 

你的代码有几个问题。 首先是你需要每个For的下一个。 你可以试试这个:

 Sub improved() Dim irow As Integer Dim x As Integer Dim dDate As Date x = 1 For irow = 1 To WorksheetFunction.CountA(Columns(1)) dDate = Cells(irow, 1).Value If dDate = Date - 1 Then Debug.Print Worksheets("Sheet1").Cells(irow, 1).Value Worksheets("Sheet1").Cells(irow, 1).EntireRow.Copy Sheet2.Range("A" & x).PasteSpecial xlPasteAll x = x + 1 End If Next End Sub 

它使用EntireRow函数,这是你需要的。 您需要将正在处理的图纸名称更改为Sheet1和Sheet2。

我希望这有帮助。

让我们把它作为我之前做的任务的一个想法。
1-在你的macrosfunction中使用这样的东西来循环主工作表及其交付的数据

 For I = 4 To Worksheets(Isheet).Rows.Count If Worksheets(Isheets).Cells(I, 7).Value <> "" Then For N = 1 To Worksheets.Count-1 If Worksheets(N).Name = Worksheets(Isheets).Cells(I, 7).Value Then // Insert the Current row in this sheet Exit For End If Next N If N >= Worksheets.Count // in this case the is new, so here will make a new Sheet for his rows and then insert this row in it. Worksheets.Add ,, End IF End If Next N 

任何停止在任务评论我。
祝你好,