key in date and filter the data in a week

Hi all,
I have question on VBA which is quite difficult for me. I need a VBA code that can filter out all data in a specific week after i type in a date, any date within that week in a textbox which i have previously created. For example i have a table of dates corresponding to number of week:

Week

1

02-Jan-200609-Jan-2006
Week 209-Jan-200616-Jan-2006
Week 316-Jan-200623-Jan-2006
Week 423-Jan-200630-Jan-2006
Week 530-Jan-200606-Feb-2006
Week 6
06-Feb-200613-Feb-2006
Week 7
13-Feb-200620-Feb-2006
Week 820-Feb-200627-Feb-2006
Week 927-Feb-200606-Mar-2006

and a source data which i want to filter:

DateInputOutput
05-Jan-2006776.95720.82
07-Jan-2006747.84729.43
08-Jan-2006755.49745.80
25-Jan-2006800.12775.75
30-Jan-2006794.96775.03
12-Feb-2006787.62758.65
14-Feb-2006800.77783.94
19-Feb-2006786.89750.06
22-Feb-2006769.86762.24

Now if i type in a date, for example 06-Jan-2006 in the textbox ( in a userform), i want the macro to help me to filter out the data of the week of 06-Jan-2006, which is week1, in the following form:

DateInputOutput
05-Jan-2006776.95720.82
*07-Jan-2006747.84729.43
08-Jan-2006755.49745.80
25-Jan-2006800.12775.75

Is this possible?
I would like to thank in advance to anyone who willing to help me on this. THANKS a lot!! :)

[7151 byte] By [pippen] at [2007-12-23]
# 1

Hi

Which office product are you working with? If you are in Access this is quite straight forward, if you are in excel the approach will be different

Regards

ADG

ADG at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 2
ADG wrote:

Hi

Which office product are you working with? If you are in Access this is quite straight forward, if you are in excel the approach will be different

Regards

ADG

I am using excel. That's why i think i need a macro to accomplish this difficult task. :) Thanks..

pippen at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 3

Hi Pippen

I copied your weeks table into Sheet1 starting at A1, I created a dropdown selection box in K1 ( with various dates), and copied your sample data into sheet2 starting in F1. The below code picks up any change to Sheet1 K1 (the dropdown box) and tries to find the range of dates, if the range is found it applies the Excel filter to the data on sheet 2

Hope this helps

Private Sub Worksheet_Change(ByVal Target As Range)
Dim X As Long
Dim DateFound As Boolean
Dim SelectedDate, StartDate, EndDate As Date
If Intersect(Target, Sheet1.Range("K1")) Then
X = 1
DateFound = False
SelectedDate = Sheet1.Cells(1, 11).Value
While Left$(Sheet1.Cells(X, 1).Value, 4) = "Week" And Not DateFound
If ((SelectedDate >= Sheet1.Cells(X, 2).Value) And (SelectedDate <= Sheet1.Cells(X, 3).Value)) Then
StartDate = Sheet1.Cells(X, 2).Value
EndDate = Sheet1.Cells(X, 3).Value
DateFound = True
Else
X = X + 1
End If
Wend
If DateFound Then
Sheet2.Activate
Sheet2.Range("F1:H1").Select
Sheet2.Range(Selection, Selection.End(xlDown)).Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=">=" & Format(StartDate, "dd-mmm-yyyy"), Operator:=xlAnd _
, Criteria2:="<=" & Format(EndDate, "dd-mmm-yyyy")
End If
End If
End Sub

ADG at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 4
Hi,
Am i only need to copy this code into the vba and not doing any stuff other than that? including creating a list box. I have problem creating a drop down selection box. Please help.
Thanks!
pippen at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 5
I managed to create a drop down list. However, the sheet2 doesn't filter itself. why?
Thanks and really appreciate your time for replying me. Thanks again.

ADG wrote:

Hi Pippen

I copied your weeks table into Sheet1 starting at A1, I created a dropdown selection box in K1 ( with various dates), and copied your sample data into sheet2 starting in F1. The below code picks up any change to Sheet1 K1 (the dropdown box) and tries to find the range of dates, if the range is found it applies the Excel filter to the data on sheet 2

Hope this helps

Private Sub Worksheet_Change(ByVal Target As Range)
Dim X As Long
Dim DateFound As Boolean
Dim SelectedDate, StartDate, EndDate As Date
If Intersect(Target, Sheet1.Range("K1")) Then
X = 1
DateFound = False
SelectedDate = Sheet1.Cells(1, 11).Value
While Left$(Sheet1.Cells(X, 1).Value, 4) = "Week" And Not DateFound
If ((SelectedDate >= Sheet1.Cells(X, 2).Value) And (SelectedDate <= Sheet1.Cells(X, 3).Value)) Then
StartDate = Sheet1.Cells(X, 2).Value
EndDate = Sheet1.Cells(X, 3).Value
DateFound = True
Else
X = X + 1
End If
Wend
If DateFound Then
Sheet2.Activate
Sheet2.Range("F1:H1").Select
Sheet2.Range(Selection, Selection.End(xlDown)).Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=">=" & Format(StartDate, "dd-mmm-yyyy"), Operator:=xlAnd _
, Criteria2:="<=" & Format(EndDate, "dd-mmm-yyyy")
End If
End If
End Sub

pippen at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 6
Do the filters appear at the top of the columns on sheet2? If so go into the custom option on the date column and see what is in there. If the filters do not appear then I suspect that the data in you dropdown is not being found in your table, in which case check all your dates are actually date formats
ADG at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 7
ADG wrote:
Do the filters appear at the top of the columns on sheet2? If so go into the custom option on the date column and see what is in there. If the filters do not appear then I suspect that the data in you dropdown is not being found in your table, in which case check all your dates are actually date formats

Hi,
Thanks! Sorry.. the reason that it couldn't filter out itself was i didn't link the sheets properly and i didn't copy the code to the correct work sheet. sorry.. now i have solved this problem with your help. I have changed and improve the code a bit, so that i can fit my work better. Anyway, thanks a lot! really thank you. thanks for your time. :)

pippen at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...