Excel VBA to Read Range of Dates

whcmelvin

Board Regular
Joined
Jul 27, 2011
Messages
82
hi,

Could someone teach me on how to allow excel to to read a range of dates that is selected in 2 listbox using VBA?

Eg.

Listbox1 is 25/05/2015 and listbox2 is 30/05/2015.

Then the VBA code would allow excel to read the the dates from 25 to 30 May and sum the values.
Using the table below, excel will sum column 2 and return 21.
22/05/201510
23/05/201512
24/05/201514
25/05/20151
26/05/20152
27/05/20153
28/05/20154
29/05/20155
30/05/20156

<tbody>
</tbody>

Please Help guys

Thanks in advance!

Regards
Melvin
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Melvin,

Not sure of your setup but assuming Activex list boxes and dates A2:A?? then try something like..

Code:
Sub Date_Total()
lr = Cells(Rows.Count, "A").End(xlUp).Row
Strt = ListBox1.Value
Fin = ListBox2.Value
Set DtRng = Range("A2:A" & lr)
Set SumRng = DtRng.Offset(0, 1)


Range("C2") = WorksheetFunction.SumIfs(SumRng, DtRng, ">=" & Strt, DtRng, "<=" & Fin)


End Sub


Private Sub ListBox1_Change()
Call Date_Total
End Sub

Hope that helps.






Private Sub ListBox2_Change()
Call Date_Total
End Sub

[/code]
 
Upvote 0
Thank for your help.

I tried out the code you provided.
Do you mind if you could help me in optimizing the code? The sumifs code runs quite slowly.
Here is the code.
Code:
NextRow = Application.WorksheetFunction.CountA(Range("F:F")) + 1Set r = Range(Cells(2, 6), Cells(NextRow, 6))
For n = 2 To r.Rows.Count
    Cells(n, 7).Value = WorksheetFunction.SumIfs(SumRng, ReportType, "7F", DateSel, Cells(n, 6))
    Sheets("Sheet1").Select
Next n


Thanks and regards
Melvin
 
Upvote 0
I'm somewhat confused.
Can you explain how your above code relates to the data you posted and code I provided?
Why the looping?
ReportType?DateSel?
Column F?
 
Upvote 0
Below is the set up of my excel sheet and the code I am using. First, there are two listboxes(IOSDate1 and IOSDate2) and user will select the dates. E.g. IOSDate1 = 24/05/2015 and IOSDate2 = 30/05/2015

The code will then get the values between 2 dates and copy paste into F2(DateSel) Column.

I did this because Sumifs's criteria range need to be the same as the sum range.

The loop is because for every dates under the column DateSel, a sumifs will be required.

After the Loop, The code will then remove duplicates date as shown below.



DateRngSumRngReportTypeDateSel1F7F
24/5/2015 1001F24/5/2015
24/5/201517F25/05/2015
25/05/20152001F26/05/2015
25/05/201527F27/05/2015
26/05/20153001F28/05/2015
26/05/201537F29/05/2015
27/05/20154001F30/05/2015
27/05/201547F
28/05/2015001F
28/05/20157F
29/05/20156001F
29/05/201567F
30/05/20157001F
30/05/201577F
31/05/20158001F
31/05/201587F

<tbody>
</tbody>



Code:
Dim NextRow As Long    Dim r As Range
    Dim n As Long



    Sheets("Sheet1").Select


    Set FindIOSDate1 = Sheets("Sheet1").Columns("A").Find(IOSDate1, LookIn:=xlValues, lookat:=xlWhole)
   Set FindIOSDate2 = Sheets("Sheet1").Columns("A").Find(IOSDate2, LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlPrevious)


Worksheets("Sheet1").Range(Cells(FindIOSDate1.Row, 1), Cells(FindIOSDate2.Row, 1)).Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("F2")


Set DtRng = Range(Cells(FindIOSDate1.Row, 1), Cells(FindIOSDate2.Row, 1))
Set SumRng = DtRng.Offset(0, 1)
Set ReportType = DtRng.Offset(0, 2)
Set DateSel = DtRng.Offset(0, 5)


Cells(1, 6).Value = "Date"
NextRow = Application.WorksheetFunction.CountA(Range("F:F")) + 1
Set r = Range(Cells(2, 6), Cells(NextRow, 6))
For n = 2 To r.Rows.Count


        Cells(n, 8).Value = WorksheetFunction.SumIfs(SumRng, ReportType, "7F", DateSel, Cells(n, 6))
        Cells(n, 7).Value = WorksheetFunction.SumIfs(SumRng, ReportType, "1F", DateSel, Cells(n, 6))
    
    Sheets("Sheet1").Select
Next n
Worksheets("Sheet1").Range("F:H").RemoveDuplicates Columns:=Array(1), Header:=xlYes
Unload DateRange




End Sub

Thanks and regards
Melvin
 
Last edited:
Upvote 0
Melvin, I am not a user of Pivot Tables but is this not what you need to easily extract this data?
 
Upvote 0

Forum statistics

Threads
1,203,384
Messages
6,055,118
Members
444,763
Latest member
Jaapaap

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top