2015-06-26 28 views
1

我想根據列A中的值複製整個行。條件將是昨天的日期。宏需要保持動態,因爲每天這個宏將運行並使用前一天的數據。這是我到目前爲止:基於單元格的值選擇行並將它們粘貼到新工作表excel vba

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 
+2

請不要在MS Office或VBA中使用[宏]。 [宏標記wiki](http://stackoverflow.com/tags/macros/info) –

+0

你能指定什麼不工作? – Alex

+0

是的,我陷入了If語句。我也不確定如何選擇行後將其複製到不同的工作表。 謝謝! – stanjo

回答

0

您的代碼有幾個問題。首先是你需要每個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。

我希望這會有所幫助。

0


讓我們在前面的任務中將其作爲一個想法逐步完成。
1在您的微距功能使用這樣的循環主片和其提供的數據

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 


在任務停止任何評論我。
對你很好,

相關問題