Consulting

Results 1 to 5 of 5

Thread: Conditional date copying

  1. #1
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location

    Conditional date copying

    I have a worksheet where column A is populated by dates. I am trying to have the macro pull all of the dates that fall within a given month (then 3 months previous, 6 months previous, etc.) it works for all of the previous months and it works for the first month unless it happens to be the last month in column A. To account for that I added the following:

    [VBA]temp = InputBox("Enter date of first day of month to forecast (mm/dd/yyyy): ")
    wbSrc.Sheets("Incoming").Activate
    i = Range("A36000").End(xlUp).Row
    'Dates of current month
    sD = CDate(temp)
    eD = DateSerial(Year(sD), Month(sD) + 1, 0)
    eD = CDate(eD)
    MsgBox (sD & vbCrLf & eD)
    For count = i To 2 Step -1
    cellD1 = CDate(Range("A" & count).Value)
    cellD2 = CDate(Range("A" & (count + 1)).Value)
    cellD3 = CDate(Range("A" & (count - 1)).Value)

    If cellD1 <= eD And cellD2 > eD Then
    eR = count
    ElseIf cellD1 <= eD And Range("A" & (count + 1)).Value = "" Then
    eR = count
    ElseIf cellD3 < sD And cellD1 >= sD Then
    sR = count
    End If
    Next count
    Range("A" & sR & ":A" & eR).Select
    Selection.Copy
    ThisWorkbook.Sheets(1).Activate
    Range("M1").Select
    ActiveSheet.Paste[/VBA]

    The dates of current months work, and the whole code works if the date is not followed by an empty row but almost always with this report the date to forecast will be the last 20 or so rows of column A.
    ------------------------------------------------
    Happy Coding my friends

  2. #2
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    here is my complete code (month-3, month-6, month-9 and month-12 all pull correctly)

    [VBA]Sub pullData()
    Dim wbSrc As Workbook
    Dim wb As Workbook
    Dim sD As Date, eD As Date, cellD1 As Date, cellD2, cellD3
    Dim i As Long, count As Long, sR As Long, eR As Long
    Dim temp As String
    Set wbSrc = ActiveWorkbook
    temp = InputBox("Enter date of first day of month to forecast (mm/dd/yyyy): ")
    'temp = "2/1/2012"
    wbSrc.Sheets("Incoming").Activate
    i = Range("A36000").End(xlUp).Row
    'Dates of current month
    sD = CDate(temp)
    eD = DateSerial(Year(sD), Month(sD) + 1, 0)
    eD = CDate(eD)
    MsgBox (sD & vbCrLf & eD)
    For count = i To 2 Step -1
    cellD1 = CDate(Range("A" & count).Value)
    cellD2 = CDate(Range("A" & (count + 1)).Value)
    cellD3 = CDate(Range("A" & (count - 1)).Value)

    If cellD1 <= eD And cellD2 > eD Then
    eR = count
    ElseIf cellD1 <= eD And Range("A" & (count + 1)).Value = "" Then
    eR = count
    ElseIf cellD3 < sD And cellD1 >= sD Then
    sR = count
    End If
    Next count
    Range("A" & sR & ":A" & eR).Select
    Selection.Copy
    ThisWorkbook.Sheets(1).Activate
    Range("M1").Select
    ActiveSheet.Paste
    wbSrc.Sheets("Incoming").Activate
    'Dates of month - 3
    sD = DateSerial(Year(sD), Month(sD) - 3, 1)
    eD = DateSerial(Year(eD), Month(eD) - 2, 0)
    sD = CDate(sD)
    eD = CDate(eD)
    For count = i To 2 Step -1
    cellD1 = CDate(Range("A" & count).Value)
    cellD2 = CDate(Range("A" & (count + 1)).Value)
    cellD3 = CDate(Range("A" & (count - 1)).Value)

    If cellD1 <= eD And (cellD2 > eD Or cellD2 = Null) Then
    eR = count
    ElseIf cellD3 < sD And cellD1 >= sD Then
    sR = count
    End If
    Next count
    Range("A" & sR & ":C" & eR).Copy
    ThisWorkbook.Sheets(1).Activate
    Range("J1").Select
    ActiveSheet.Paste
    wbSrc.Sheets("Incoming").Activate
    'Dates of month - 6
    sD = DateSerial(Year(sD), Month(sD) - 3, 1)
    eD = DateSerial(Year(eD), Month(eD) - 2, 0)
    sD = CDate(sD)
    eD = CDate(eD)
    For count = i To 2 Step -1
    cellD1 = CDate(Range("A" & count).Value)
    cellD2 = CDate(Range("A" & (count + 1)).Value)
    cellD3 = CDate(Range("A" & (count - 1)).Value)

    If cellD1 <= eD And (cellD2 > eD Or cellD2 = Null) Then
    eR = count
    ElseIf cellD3 < sD And cellD1 >= sD Then
    sR = count
    End If
    Next count
    Range("A" & sR & ":C" & eR).Copy
    ThisWorkbook.Sheets(1).Activate
    Range("G1").Select
    ActiveSheet.Paste
    wbSrc.Sheets("Incoming").Activate
    'Dates of month - 9
    sD = DateSerial(Year(sD), Month(sD) - 3, 1)
    eD = DateSerial(Year(eD), Month(eD) - 2, 0)
    sD = CDate(sD)
    eD = CDate(eD)
    For count = i To 2 Step -1
    cellD1 = CDate(Range("A" & count).Value)
    cellD2 = CDate(Range("A" & (count + 1)).Value)
    cellD3 = CDate(Range("A" & (count - 1)).Value)

    If cellD1 <= eD And (cellD2 > eD Or cellD2 = Null) Then
    eR = count
    ElseIf cellD3 < sD And cellD1 >= sD Then
    sR = count
    End If
    Next count
    Range("A" & sR & ":C" & eR).Copy
    ThisWorkbook.Sheets(1).Activate
    Range("D1").Select
    ActiveSheet.Paste
    wbSrc.Sheets("Incoming").Activate
    'Dates of month - 12
    sD = DateSerial(Year(sD), Month(sD) - 3, 1)
    eD = DateSerial(Year(eD), Month(eD) - 2, 0)
    sD = CDate(sD)
    eD = CDate(eD)
    For count = i To 2 Step -1
    cellD1 = CDate(Range("A" & count).Value)
    cellD2 = CDate(Range("A" & (count + 1)).Value)
    cellD3 = CDate(Range("A" & (count - 1)).Value)

    If cellD1 <= eD And (cellD2 > eD Or cellD2 = Null) Then
    eR = count
    ElseIf cellD3 < sD And cellD1 >= sD Then
    sR = count
    End If
    Next count
    Range("A" & sR & ":C" & eR).Copy
    ThisWorkbook.Sheets(1).Activate
    Range("A1").Select
    ActiveSheet.Paste
    End Sub[/VBA]

  3. #3
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    and my workbook
    Attached Files Attached Files
    ------------------------------------------------
    Happy Coding my friends

  4. #4
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    After a little debugging:

    The values for start date (sD) and end date (eD) in the first check are 2/1/2012 and 2/29/2012 which is correct

    The first cell checked ("A" & i) is cell A449 which is correct

    The values for start row (sR) and end row (eR) are 429 (which is what i expected) and 439 (which omits the last 10 dates in column A even though their values are less than the end date specified)

    no idea what is causing this strange behaviour...
    ------------------------------------------------
    Happy Coding my friends

  5. #5
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    no ideas? i'm really in a pinch here
    ------------------------------------------------
    Happy Coding my friends

Posting Permissions

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