Get all dates between 2 dates in vba

Shantanu_4612

New Member
Joined
Oct 3, 2016
Messages
27
I am a newbie in vba and I am trying to get in vba all dates between 2 dates, for example I will call the function with the parameters 01-01-2015 and 15-01-2015, and I will get in return an array with all the dates possibles, i.e :

This is the Data that I have;

IDStart DateEnd DateCode
123456703-10-201615-10-2016ABC_987654321
345678910-09-201620-09-2016ABC_123456789

<tbody>
</tbody>

The Result should be as below, and should stop when finds blanks in start date

IDDateCode
123456703-10-2016ABC_987654321
123456704-10-2016ABC_987654321
123456705-10-2016ABC_987654321
345678910-09-2016ABC_123456789
345678911-09-2016ABC_123456789
345678912-09-2016ABC_123456789
345678913-09-2016ABC_123456789
345678914-09-2016ABC_123456789
345678915-09-2016ABC_123456789
345678916-09-2016ABC_123456789
345678917-09-2016ABC_123456789
345678918-09-2016ABC_123456789
345678919-09-2016ABC_123456789
345678920-09-2016ABC_123456789

<tbody>
</tbody>

Please help me with this its a bit urgent, my job is on stake
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this for results starting "F1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Oct07
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Dt [COLOR="Navy"]As[/COLOR] Date
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
c = 1
Range("F1:H1") = Array("ID", "Date", "Code")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] Dt = Dn.Value To Dn.Offset(, 1).Value
        c = c + 1
        Cells(c, "F") = Dn.Offset(, -1).Value
        Cells(c, "G") = Dt
        Cells(c, "H") = Dn.Offset(, 2).Value
    [COLOR="Navy"]Next[/COLOR] Dt
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thank You for your reply,

I am a bit new to this,

So could you please do few changes to the Script,

Start Data and End Data is in Sheet 1 (Named as UX_Dump)
Start Date(Leave From Date) is in BM Column and End Date (Leave to Date) is in BO Column,

ID (Employee No.) is in AQ Column and Code (Leave Application No.) is in BK Column.

I will be assigning the Button in Sheet 3 (Named Report) and Macro has to executed in Sheet 2 (Raw)

Please help
 
Upvote 0
Try this :-
Data in sheet "UX_Dump", Results in sheet "Raw",Starting "A1"
Code:
[COLOR=navy]Sub[/COLOR] MG03Oct48
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, c [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Dt [COLOR=navy]As[/COLOR] Date
[COLOR=navy]With[/COLOR] Sheets("UX_Dump")
        [COLOR=navy]Set[/COLOR] Rng = .Range(.Range("BM2"), .Range("BM" & Rows.Count).End(xlUp))
[COLOR=navy]End[/COLOR] With
ReDim ray(1 To 3, 1 To 1): c = 1
ray(1, 1) = "ID": ray(2, 1) = "Date": ray(3, 1) = "Code"
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    [COLOR=navy]For[/COLOR] Dt = Dn.Value To Dn.Offset(, 2).Value
        c = c + 1
        ReDim Preserve ray(1 To 3, 1 To c)
        ray(1, c) = Dn.Offset(, -22).Value
        ray(2, c) = Dt
        ray(3, c) = Dn.Offset(, -2).Value
    [COLOR=navy]Next[/COLOR] Dt
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]With[/COLOR] Sheets("Raw").Range("A1").Resize(c, 3)
    .Value = Application.Transpose(ray)
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR=navy]End[/COLOR] [COLOR=navy]With[/COLOR]
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
It is not working,

In RAW sheet, I want Employee No. in Column A, Date in Column B and Leave Application No. in Column C

If I run the Macro in Sheet 3 (Named Report)

So Sheet 1 is UX_Dump
Sheet 2 is Raw
Sheet 3 is WFM_Dump (Macro to be Run)

And if possible can be add a timer of 5 Mins and executing the Macro,
i.e. We run the Macro, Timer Displays, So we give it a time of
 
Upvote 0
Sorry for late reply,

For the second scripts that you gave me, it just gave me an error that it cannot be run,

and the first script, it is working, but it is killing my computer and is taking ages to run,

I have a data of, 5,00,000 entries, and have to run this macro on my office computer which does not have a good config,

It is killing it, its config is so bad, that its taking 2 mins to complete entry in one cell.

Please help,
 
Upvote 0
If you have 1/2 million lines in your data and there at 10 dates in each that is 5 million rows in a worksheet with just over 1 million rows.
Is 1/2 million rows correct ???
 
Upvote 0
If you have 1/2 million lines in your data and there at 10 dates in each that is 5 million rows in a worksheet with just over 1 million rows.
Is 1/2 million rows correct ???

Yes, approximately!
But the dates are varying from 0.5(half day) to 90 days,
So yes it's a huge data
So please help me with a solution
 
Upvote 0
Try this on a small sample to make sure you getting the right results on sheet "Raw".
NB:- If the results in "Raw" get over one million rows the results Move 3 columns across and starting again with row1.
I have tried this on 200K rows giving a result of approx. 2.4 million rows.
The code took about 3Minutes to run.
Code:
[COLOR="Navy"]Sub[/COLOR] MG05Oct57
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Dt [COLOR="Navy"]As[/COLOR] Date, ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] t
ac = 1
t = Timer
Application.ScreenUpdating = False
[COLOR="Navy"]With[/COLOR] Sheets("UX_Dump")
        [COLOR="Navy"]Set[/COLOR] Rng = .Range(.Range("BM2"), .Range("BM" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
c = 1
[COLOR="Navy"]With[/COLOR] Sheets("Raw")
.Range("A1:C1").Value = Array("ID", "Date", "Code")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] Dt = Dn.Value To Dn.Offset(, 2).Value
        c = c + 1
        [COLOR="Navy"]If[/COLOR] c >= 1000000 [COLOR="Navy"]Then[/COLOR] ac = ac + 3: c = 1
        .Cells(c, ac) = Dn.Offset(, -22).Value
        .Cells(c, ac + 1) = Dt
        .Cells(c, ac + 2) = Dn.Offset(, -2).Value
    [COLOR="Navy"]Next[/COLOR] Dt
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] With
Application.ScreenUpdating = True
MsgBox Timer - t
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,003
Members
449,203
Latest member
Daymo66

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