Consulting

Results 1 to 12 of 12

Thread: Solved: Extracting Data Based on Date Range

  1. #1

    Solved: Extracting Data Based on Date Range

    I am working on a project where I take the data from one worksheet, put it in another, clean up the data, calculate the days between steps in the process and then chart it. I've accomplished most of it (still need error trapping and some little tweaks), but now managment wants to be able to pull the data for a user specified date range. Meaning, although the data is for a whole year, they might just want the first six months.

    Now I'm not sure what my next step is since I see problems with all three ideas I had :
    Change how the data is copied and pasted (Already slow, would this slow it down more?)
    Filter the data I have (Filtering based on dates can be...unreliable)
    Delete rows based on cell values (I know it can be done but I don't know how to do it)

    If someone could give me a push in the right direction, it would be greatly appreciated.

    I've attached my spreadsheet with associated macros.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Which date is being referred to?

    Filtering on dates is okay, as long as you understand its quirks.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    We would be looking at data based on Lab End Date.

  4. #4
    I tried filtering the dates, but it doesn't affect the outcome.

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    To get it down to just being able to filter by the month:

    Brute force for column G Actual Lab ending date

    [VBA]Option Explicit
    Sub Macro4()
    Dim cell As Range
    Columns("H:H").Insert Shift:=xlToRight
    For Each cell In ActiveSheet.Range("G1", ActiveSheet.Range("G" & ActiveSheet.Rows.Count).End(xlUp))
    Worksheets("Metric Data").Columns("H").NumberFormat = "mm yyyy"
    If cell <> "" Then
    cell.Offset(0, 1) = "=" & cell.Address & ""
    End If
    Next cell

    ActiveSheet.Range("H:H").Copy
    ActiveSheet.Range("H:H").PasteSpecial Paste:=xlValues
    Columns("H:H").Columns.AutoFit

    Columns("G:G").Delete Shift:=xlToLeft
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Hi Lucas,
    Thanks for the reply. Unfortunately, I need to calculate the number of days between each step in the process and by changing the date to month and year, I can't get that information.

    I think what I would like to do is step through column G and delete rows based on the user input, but I'm not sure how to do that.

  7. #7
    Can anyone give me any other suggestions on this problem? Thanks!

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you repost your attachment?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Thanks, here it is.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add to a userform with two textboxes ands a button
    [VBA]
    Private Sub CommandButton1_Click()
    For Each cel In Intersect(Columns(7), ActiveSheet.UsedRange)
    If cel >= CDate(TextBox1.Text) And cel <= CDate(TextBox2.Text) Then
    cel.Offset(, 11) = "x"
    End If
    Next
    ActiveSheet.Columns(18).AutoFilter Field:=1, Criteria1:="<>"
    End Sub

    Private Sub UserForm_Initialize()
    TextBox1.Text = Format(Application.Max(Sheets("Metric Data").Columns(7)) - 180, "dd/mm/yy")
    TextBox2.Text = Format(Application.Max(Sheets("Metric Data").Columns(7)), "dd/mm/yy")
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    I haven't worked with UserForms before so forgive me if these are basic questions.
    How do I get the userform to open and close?
    Does the Userform_Initialize force the format of the text box?

    Thanks!

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by kathyb0527
    I haven't worked with UserForms before so forgive me if these are basic questions.
    How do I get the userform to open and close?
    In a macro, show it

    [vba]

    Userform1.Sow
    [/vba]

    Quote Originally Posted by kathyb0527
    Does the Userform_Initialize force the format of the text box?
    Not implicitly, you have to format it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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