[英]Selecting Rows based on a Cell's Value and Pasting them into a New sheet excel vba
我正在嘗試根據A列中的值復制整個行。條件將是昨天的日期。 宏需要保持動態,因為該宏每天都會運行並使用前一天的數據。 這是我到目前為止的內容:
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-在您的Macro函數中使用類似這樣的方法來循環主工作表及其傳遞的數據
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
任何在任務中停止的評論我。
祝您好運,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.