Consulting

Results 1 to 4 of 4

Thread: Solved: Copy rows with todays dates

  1. #1
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location

    Solved: Copy rows with todays dates

    I have a sheet of spread trades with a start date in column G. I would like to copy any spreads that fall within 7 days from today or was less than 7 days ago. So I need to select the row with the date and the row below it. heres a sample of what I have, any pointers would be much appreciated.

    Thank you

  2. #2
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Am I able to utilize the following example?

    [VBA]
    Sub MoveDataToSheet(2)
    'Determine how many rows in Sheet1
    lastListRow = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
    'Loop through rows
    For myItems = 1 To lastListRow
    'Determine next empty row in Sheet2
    destRow = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1


    If ... DATE STATEMENT HERE....
    Rows(myItems).EntireRow.Copy _
    Destination:=Sheets(2).Range("A" & destRow)
    'Loop
    Next
    End Sub
    [/VBA]

  3. #3
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    Try this:
    [VBA]Sub moveEm()
    Dim i As Long, lRow As Long, curSht As Worksheet, dstSht As Worksheet
    Set curSht = Worksheets(1)
    Set dstSht = Worksheets(2)
    lRow = curSht.Range("b" & Rows.Count).End(xlUp).Row

    curSht.Range("1:2").Copy _
    Destination:=dstSht.Range("a1")
    For i = 3 To lRow Step 2
    If curSht.Range("G" & i) >= Date - 7 Then
    curSht.Range("a" & i & ":a" & i + 1).EntireRow.Copy _
    Destination:=dstSht.Range("a" & dstSht.Range("b1").End(xlDown).Row + 1)
    End If
    Next


    End Sub[/VBA]

  4. #4
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Thanks mbarron, added the second part to the if statement and it works a charm


    [VBA]

    Sub moveEm()
    Dim i As Long, lRow As Long, curSht As Worksheet, dstSht As Worksheet
    Set curSht = Worksheets(1)
    Set dstSht = Worksheets(2)
    lRow = curSht.Range("b" & Rows.Count).End(xlUp).Row

    curSht.Range("1:2").Copy _
    Destination:=dstSht.Range("a1")
    For i = 3 To lRow Step 2
    If curSht.Range("G" & i) >= Date - 7 And curSht.Range("G" & i) <= Date + 7 Then
    curSht.Range("a" & i & ":a" & i + 1).EntireRow.Copy _
    Destination:=dstSht.Range("a" & dstSht.Range("b1").End(xlDown).Row + 1)
    End If
    Next


    End Sub
    [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •