Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Loop - All posible combinations into an array

Posted on 2009-04-09
5
Medium Priority
?
281 Views
Last Modified: 2012-05-06
Hi,
Im trying to write some results into an array for working out all possible combinations of days, weeks and months, between 2 dates.

For exmaple, between 1st Jan and the 20th jan, i want to know all combinations of days, weeks, months you could have

Total Days = 21
Day = 1
Week = 7
Month = 31

Combinations to an array, should be something like:
1 = 21day, 0 Weeks, 0 Months
2 = 14day, 1 week,0 months
3 = 7day, 2 week,0 months
4 = 0day, 3 week,0 months
sub CalCombinations()
 
StartDate = "1/01/2009"
EndDate = "31/01/2009"
 
TotalDays = DateDiff("d", StartDate, EndDate) + 1 'add 1 day to cover for the start day
TotalFullWeeks = DateDiff("w", StartDate, EndDate)
TotalFullMonths = DateDiff("m", StartDate, EndDate)
 
End sub

Open in new window

0
Comment
Question by:jamiepryer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 24113751
I made some assumptions:
- A month has 4 weeks
- A month has 30 days (average)

    Dim possibleCombinations() As String
    ReDim possibleCombinations(0)
    StartDate = "1/01/2009"
    EndDate = "28/02/2009"
    
    totalDays = DateDiff("d", StartDate, EndDate) + 1 'add 1 day to cover for the start day
    TotalFullWeeks = DateDiff("w", StartDate, EndDate)
    totalFullMonths = DateDiff("m", StartDate, EndDate)
 
    Dim strToAdd As String
    For j = 0 To totalFullMonths
        For i = 0 To (TotalFullWeeks + (totalFullMonths * 4))
            If (totalDays - ((j * 30) + (i * 7))) > 0 Then
                strToAdd = (totalDays - ((j * 30) + (i * 7))) & " days, " & i & " Weeks, " & j & " Months"
                possibleCombinations(UBound(possibleCombinations)) = strToAdd
                ReDim Preserve possibleCombinations(UBound(possibleCombinations) + 1) As String
            End If
        Next i
    Next j
    ReDim Preserve possibleCombinations(UBound(possibleCombinations) - 1) As String

Open in new window

0
 

Author Comment

by:jamiepryer
ID: 24136360
thanks, that works really well
can you just talk me through your thinking for the

"    For I = 0 To (TotalFullWeeks + (totalFullMonths * 4))"

and

"If (totalDays - ((j * 30) + (I * 7))) > 0 Then"

please..
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24136628
1. For I = 0 To (TotalFullWeeks + (totalFullMonths * 4))"
We need to loop for each week (but a month contains 4 weeks, that's why you have
"number of weeks" + ("number of months" * 4 = number of weeks for these months)

2. If (totalDays - ((j * 30) + (I * 7))) > 0 Then
j represents a month (so x-monts * 30 days to have the number of days
l represents a week (so x-weeks * 7 days) to have the number of days

Totaldays is a number that gives a an number of days between start and end.
When we have for example: 1 week and 1 day -> we'll have also the possibility to have 8 days
If we don't use formula above, we'll get the following result
1 week - 1 days
0 week - 8 days
0 week - 1 days
The last is incorrect off-course
0
 

Author Comment

by:jamiepryer
ID: 24137155
thanks!
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24137221
Glad I could help you...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question