Problem regarding VBA in excel - Combine 52 VBA codes for 52 weeks in a year to one VBA code
I am very new to VBA, hope that anyone can help me on this..
I have some problem with creating a code which can
Please refer to the code down there:
'the listbox will show week 1 to week 52 and let me choose one of the week.
Private Sub OKButton_Click()
If ListBox1.Text = "Week 26" Then Call week26
If ListBox1.Text = "Week 27" Then Call week27
' all together 52 weeks in a year
Unload Me
Sheet7.Activate
End Sub
Sub week26()
With Sheet7
.AutoFilterMode = False
.Range("A1:I1").AutoFilter
.Range("A1:I1").AutoFilter Field:=3, Criteria1:=">=26-Jun-06", _
Operator:=xlAnd, Criteria2:="<=2-Jul-06"
End With
End Sub
Sub week27()
With Sheet7
.AutoFilterMode = False
.Range("A1:I1").AutoFilter
.Range("A1:I1").AutoFilter Field:=3, Criteria1:=">=3-Jul-06", _
Operator:=xlAnd, Criteria2:="<=9-Jul-06"
End With
End Sub
Since there are 52 weeks in a year, i have to type the codes for 52
times for different weeks. can anyone of you help me on some code so
that i don't need to type all these codes 52 times, instead one time
with a loop or something else.
Thanks.
[1301 byte] By [
pippen] at [2007-12-23]
Hi.. thanks you for replying me.. however.. due to my limite knowledge. i still couldn't get what i want..although i tried very hard to manipulate the code. so.. i still need your help. i would be glad that you could help me once more. maybe i didn't describe my situation very clearly so that it ended up like this. let me simpify it a bit.
now i have an array of data in sheet1 as below:
Start Time |
Amount1 |
Amount2 |
26-Jun-06 |
776.95 |
720.82 |
28-Jun-06 |
747.84 |
729.43 |
30-Jun-06 |
755.49 |
745.8 |
3-Jul-06 |
800.12 |
775.75 |
4-Jul-06 |
794.96 |
775.03 |
7-Jul-06 |
787.62 |
758.65 |
9-Jul-06 |
800.77 |
783.94 |
10-Jul-06 |
786.89 |
750.06 |
12-Jul-06 |
769.86 |
762.24 |
14-Jul-06 |
811.5 |
761.3 |
14-Jul-06 |
783.47 |
727.94 |
18-Jul-06 |
758.03 |
727.25 |
20-Jul-06 |
825.73 |
753.2 |
20-Jul-06 |
825.73 |
753.2 |
20-Jul-06 |
825.73 |
753.2 |
20-Jul-06 |
825.73 |
753.2 |
20-Jul-06 |
825.73 |
753.2 |
20-Jul-06 |
825.73 |
753.2 |
and as you told me to to create another sheet for the weeks and dates which is sheet2 as below: (i name the column of criteria1 as criteria1 and this also applies to column2):
Week |
criteria1 |
criteria2 |
Week 1 |
2-Jan-06 |
8-Jan-06 |
Week 2 |
9-Jan-06 |
15-Jan-06 |
Week 3 |
16-Jan-06 |
22-Jan-06 |
Week 4 |
23-Jan-06 |
29-Jan-06 |
Week 5 |
30-Jan-06 |
5-Feb-06 |
Week 6 |
6-Feb-06 |
12-Feb-06 |
Week 7 |
13-Feb-06 |
19-Feb-06 |
Week 8 |
20-Feb-06 |
26-Feb-06 |
Week 9 |
27-Feb-06 |
5-Mar-06 |
Week 10 |
6-Mar-06 |
12-Mar-06 |
Week 11 |
13-Mar-06 |
19-Mar-06 |
Week 12 |
20-Mar-06 |
26-Mar-06 |
Week 13 |
27-Mar-06 |
2-Apr-06 |
Week 14 |
3-Apr-06 |
9-Apr-06 |
Week 15 |
10-Apr-06 |
16-Apr-06 |
Week 16 |
17-Apr-06 |
23-Apr-06 |
Week 17 |
24-Apr-06 |
30-Apr-06 |
Week 18 |
1-May-06 |
7-May-06 |
Week 19 |
8-May-06 |
14-May-06 |
Week 20 |
15-May-06 |
21-May-06 |
Week 21 |
22-May-06 |
28-May-06 |
Week 22 |
29-May-06 |
4-Jun-06 |
Week 23 |
5-Jun-06 |
11-Jun-06 |
Week 24 |
12-Jun-06 |
18-Jun-06 |
Week 25 |
19-Jun-06 |
25-Jun-06 |
Week 26 |
26-Jun-06 |
2-Jul-06 |
Week 27 |
3-Jul-06 |
9-Jul-06 |
Week 28 |
10-Jul-06 |
16-Jul-06 |
Week 29 |
17-Jul-06 |
23-Jul-06 |
Week 30 |
24-Jul-06 |
30-Jul-06 |
Week 31 |
31-Jul-06 |
6-Aug-06 |
Week 32 |
7-Aug-06 |
13-Aug-06 |
Week 33 |
14-Aug-06 |
20-Aug-06 |
Week 34 |
21-Aug-06 |
27-Aug-06 |
Week 35 |
28-Aug-06 |
3-Sep-06 |
Week 36 |
4-Sep-06 |
10-Sep-06 |
Week 37 |
11-Sep-06 |
17-Sep-06 |
Week 38 |
18-Sep-06 |
24-Sep-06 |
Week 39 |
25-Sep-06 |
1-Oct-06 |
Week 40 |
2-Oct-06 |
8-Oct-06 |
Week 41 |
9-Oct-06 |
15-Oct-06 |
Week 42 |
16-Oct-06 |
22-Oct-06 |
Week 43 |
23-Oct-06 |
29-Oct-06 |
Week 44 |
30-Oct-06 |
5-Nov-06 |
Week 45 |
6-Nov-06 |
12-Nov-06 |
Week 46 |
13-Nov-06 |
19-Nov-06 |
Week 47 |
20-Nov-06 |
26-Nov-06 |
Week 48 |
27-Nov-06 |
3-Dec-06 |
Week 49 |
4-Dec-06 |
10-Dec-06 |
Week 50 |
11-Dec-06 |
17-Dec-06 |
Week 51 |
18-Dec-06 |
24-Dec-06 |
Week 52 |
25-Dec-06 |
31-Dec-06 |
What i want is i created a listbox (in a userform) which shows me all the weeks in a year, from week1 till week 52. and after i clicked one of the week in the listbox and ok. After that the macro will autofilter the data in sheet 1 into only showing data of that specific week, for example week26, according to the start and end dates of that week which specified in sheet2. The macro i have tried (follow the way you taught me) is as below:
Private Sub OptionButton1_Click()
ListBox1.RowSource = "Sheet2!Week1"
End Sub
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub OKButton_Click()
MsgBox ListBox1.Text & vbCrLf & "ListIndex: " & ListBox1.ListIndex 'for info only
FilterWeek Sheets("Sheet2").Cells(ListBox1.ListIndex + 2, 2), Sheets("Sheet2").Cells(ListBox1.ListIndex + 2, 3)
Unload Me
Sheets("Sheet1").Activate
Unload Me
Sheet1.Activate
End Sub
Sub FilterWeek(strCriteria1 As String, strCriteria2 As String)
With Sheet1
.AutoFilterMode = False
.Range("A1:I1").AutoFilter
.Range("A1:I1").AutoFilter Field:=1, Criteria1:=">=" & strCriteria1, _
Operator:=xlAnd, Criteria2:="<=" & strCriteria2
End With
End Sub
It didn't work in the end. Could you please tell me what went wrong? Thanks. (erm.. do you understand what i meant? :) )
Thanks again.