+ Reply to Thread
Results 1 to 5 of 5

Macro to select data within a date range.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-19-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    18

    Question Macro to select data within a date range.

    Hi All,

    I'm rather struggling with something and would really appreciate some help.

    I have a spreadsheet with up to 1000 rows. The data we are interested in is "leave date" and "confirmed".

    I need a macro or code to copy the whole row when the "leave date" was within 13 weeks and confirmed = no. If possible, the new data would appear on a new worksheet in the same book.

    Please see attached data sample.


    (Ideally, I would be using a database application however I don't currently have the resource)
    Attached Files Attached Files

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro to select data within a date range.

    when the "leave date" was within 13 weeks
    13 wks of what?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    06-28-2011
    Location
    New Delhi India
    MS-Off Ver
    Excel 2003 ,2007
    Posts
    23

    Re: Macro to select data within a date range.

    Try this code .. your output sheet will be sheet2


    Sub copyleave()
    
    Dim lastDate As Date
    Dim lastRow, counter, incRow As Integer
    
    incRow = 2
    lastDate = Date - 13 * 7 ' to fetch last 13 week date
    
    With ActiveSheet
    Sheets("sheet2").Range("A1:F1").Value = .Range("A1:F1").Value 'Copy the heading
        lastRow = .Range("A65536").End(xlUp).Row  ' count the how many row
    
        For counter = 2 To lastRow
            'Checking the condition for 13 week and confirmed No
            If CDate(.Range("D" & counter).Value) >= lastDate And UCase(.Range("F" & counter).Value) = "NO" Then
                
                Sheets("sheet2").Range("A" & incRow & ":" & "F" & incRow).Value = .Range("A" & counter & ":" & "F" & counter).Value
                incRow = incRow + 1
            
            End If
        
        Next
    
    End With
    
    End Sub
    Purushottam
    Excel, MS ACCESS 2007

  4. #4
    Registered User
    Join Date
    12-19-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro to select data within a date range.

    Hi arlu,

    Apologies , 13 weeks of todays date.

  5. #5
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Macro to select data within a date range.

    For a formula-based approach, see attachment.
    The "within 13 weeks" requirement was simplified to:difference between today and leave date is smaller than 13*7. Adapt if necessary.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1