Question

MS Excel random date and time simulator

Asked by: Tlogix

Hi,
I need to populate a spreadsheet with data that would simulate events over the course of a work week, this would meet generaing random numbers with conditions, the following is an example:

Condition 1: Work week is M-F 8am-5pm
Condition 2: The events can have a range between 3 mins to 1 hour
Condition 3: The interval between events can range between 1 min and 40 mins.

So the example would be this

Event          |           Date          |           Time          |           Duration
1                |   03/03/2003        |        08:35:15       |           0:04:45
2                |   03/03/2003        |        08:41:00       |           0:08:00
3                |   03/03/2003        |        08:54:26       |           0:48:16

> Event one lasted 4m 45sec, there was a 1 min interval before event two
> Event two lasted 8 mins, there was a 5min 26 sec interval before event 3 which lasted 48min 16sec.
> The events would continue like this based on the above conditons until 17:00 was reached (which can be exceeded for the last entry).
> The next events would start on the next calendar day randomly also from 8:00am.

I would prefer this in a VBA maro solution and notes about how I can  change the the variables. If you require more columns or something, that would be fine..I just need the values really to plug into a DB.

(newbie here...thansk in advance).





                     

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-05-31 at 03:35:32ID21441658
Tags

excel

Topic

Microsoft Excel Spreadsheet Software

Participating Experts
2
Points
500
Comments
29

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. simulation
    What are the principles of computer network simulation in C?
  2. duration
    Hi Experts, Please advise how to go about this: I have the following fields for each row in excel (i have placed figures as example): B jan. 6 material requirement 10 C jan. 7 material requirement 11 D jan. 8 material requirement ...
  3. Simulation
    Hello, I'm how to build a simulation that would simulate an ocean with sea animals it. Within the simulation I want certain ocean critters to eat each other, reproduce, and die. Now my question is I want to use threads in order to do this but I was wondering if there is anoth...
  4. simulating 500 results
    Hi, my excel sheet : http://www.generalya.com/6.xls My question is how can i simulate 500 Net Revenue results for each column? Each column has a unique reservation number and a unique random number. I can't figure out a way to easily simulate 500 net revenue results so tha...
  5. Problems with some durations when created from XML
    Good morning, I am dynamically creating some XML to be used in MSProject 2003. My calendar is set to be a 24 hour calendar. From my database I basically have two fields of interest: a start date and a duration (in days). In MSProject I want this start date to be the &quo...
  6. Using VBA to create a linear simulation
    It has been about 3 years since I did any code development in VBA and I am stumped on a linear simualtion problem. I have a product that is inserted into a series of processes and it remains in each process for a designated period of time (determined by a variable) So what...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: byundtPosted on 2005-05-31 at 05:23:34ID: 14112531

Hi Tlogix,
I would solve the problem using the RAND() function and four variables in named ranges: MinDur (Minimum duration), MaxDur (Maximum duration),MinInt (Minimum Interval) and MaxInt(MaxInterval).

The formulas for a random duration and interval between two extremes are:
=MinDur+(MaxDur-MinDur)*RAND()
=MinInv+(MaxInv-MinInv)*RAND()

The formula in cell C2 (to start things out) will be a little different from the rest:
=TIMEVALUE("8:00")+MinInv+(MaxInv-MinInv)*RAND()

The formulas for cell C3 and D2 may be copied down. They are:
=IF(C2+D2<=TIMEVALUE("5:00"),C2+D2+MinInv+(MaxInv-MinInv)*RAND(),"")
=IF(C2="","",MinDur+(MaxDur-MinDur)*RAND())

You can code the above into a macro--but I don't see that it gives you any more flexibility. Note that the VBA equivalent of RAND() is Rnd().
Cheers!
Brad

 

by: TlogixPosted on 2005-05-31 at 05:27:48ID: 14112561

Hi Brad..
Sounds smashing....I would apprecaite the VBA is possible and some pointers about what I would need to contruct ahead of tiome work book wise.
I was hoping you'd takle a peak at this...:)

atb
B

 

by: TlogixPosted on 2005-05-31 at 06:22:49ID: 14112945

byundt ,
Hi..sorry, I misread your comment, and thought mentioned 'You could' write this in a VBA as apposed to me...

I'm not that profecient in XL to do this setting ranges etc..if you can provide a VBA, that would be great or step by step to impliment this through formulas - I would appreciate that too...

Sounds painful..sorry :)
B

 

by: byundtPosted on 2005-05-31 at 06:24:59ID: 14112963

Tlogix,
If nobody else solves it before then, I'll revisit your problem in about four hours.
Brad

 

by: TlogixPosted on 2005-05-31 at 06:29:28ID: 14112993

byundt ,
Thats one of my favorite RTFM comments...
thanks..

 

by: matthewspatrickPosted on 2005-05-31 at 11:49:42ID: 14115792

Hi B,

The following sub will randomly generate your events.  It assumes you have headers in Sheet1!A1:D1.

Sub GenerateEvents()
   
    Dim Dest As Worksheet
    Dim LastRow As Long
    Dim Counter As Long
    Dim StartDate As Date, EndDate As Date
    Dim MinDuration As Date, MaxDuration As Date
    Dim MinGap As Date, MaxGap As Date
    Dim Current As Date, QuitTime As Date, TimeLeft As Date, Gap As Date, Duration As Date
   
    MinDuration = TimeValue("12:03 am")
    MaxDuration = TimeValue("1:00 am")
    MinGap = TimeValue("12:01 am")
    MaxGap = TimeValue("12:40 am")
   
    Set Dest = ThisWorkbook.Worksheets("Sheet1")
   
    StartDate = InputBox("What is the start date for your event range", "Start Date")
    EndDate = InputBox("What is the end date for your event range", "End Date")
   
    LastRow = Dest.Cells(65536, 1).End(xlUp).Row
    If LastRow > 1 Then Range(Dest.Cells(2, 1), Dest.Cells(65536, 1)).EntireRow.Delete
   
    Current = DateAdd("h", 8, StartDate)
    Counter = 1
   
    Do Until Current >= EndDate
        If Weekday(Current, vbSunday) = 1 Then
            Current = DateAdd("h", 8, Int(Current) + 1)
        ElseIf Weekday(Current, vbSunday) = 7 Then
            Current = DateAdd("h", 8, Int(Current) + 2)
        ElseIf Hour(Current) < 7 Then
            Current = DateAdd("h", 8, Int(Current))
        ElseIf Hour(Current) > 15 Then
            Current = DateAdd("h", 8, Int(Current) + 1)
        Else
            QuitTime = DateAdd("h", 17, Int(Current))
            TimeLeft = QuitTime - Current
            Gap = MinGap + Rnd * (MaxGap - MinGap)
            Current = Current + Gap
            If Gap <= TimeLeft Then
                Duration = MinDuration + Rnd * (MaxDuration - MinDuration)
                Counter = Counter + 1
                With Dest
                    .Cells(Counter, 1) = Counter - 1
                    .Cells(Counter, 2) = Format(Current, "mm/dd/yyyy")
                    .Cells(Counter, 3) = Format(Current, "hh:mm:ss")
                    .Cells(Counter, 4) = Format(Duration, "hh:mm:ss")
                End With
                Current = Current + Duration
            End If
        End If
    Loop
   
End Sub


Regards,

Patrick

 

by: matthewspatrickPosted on 2005-05-31 at 12:48:46ID: 14116246

B,

Here is an improved routine, with some minor tweaks:

Sub GenerateEvents()
   
    Dim Dest As Worksheet
    Dim LastRow As Long
    Dim Counter As Long
    Dim StartDate As Date, EndDate As Date
    Dim MinDuration As Date, MaxDuration As Date
    Dim MinGap As Date, MaxGap As Date
    Dim Current As Date, QuitTime As Date, TimeLeft As Date, Gap As Date, Duration As Date
   
    MinDuration = TimeValue("12:03 am")
    MaxDuration = TimeValue("1:00 am")
    MinGap = TimeValue("12:01 am")
    MaxGap = TimeValue("12:40 am")
   
    Set Dest = ThisWorkbook.Worksheets("Sheet1")
   
    StartDate = InputBox("What is the start date for your event range", "Start Date")
    EndDate = InputBox("What is the end date for your event range", "End Date")
    EndDate = DateAdd("h", 17, EndDate)
   
    LastRow = Dest.Cells(65536, 1).End(xlUp).Row
    If LastRow > 1 Then Range(Dest.Cells(2, 1), Dest.Cells(65536, 1)).EntireRow.Delete
   
    Current = DateAdd("h", 8, StartDate)
    Counter = 1
   
    Randomize
   
    Do Until Current >= EndDate
        If Weekday(Current, vbSunday) = 1 Then
            Current = DateAdd("h", 8, Int(Current) + 1)
        ElseIf Weekday(Current, vbSunday) = 7 Then
            Current = DateAdd("h", 8, Int(Current) + 2)
        ElseIf Hour(Current) > 16 Then
            Current = DateAdd("h", 8, Int(Current) + 1)
        Else
            QuitTime = DateAdd("h", 17, Int(Current))
            TimeLeft = QuitTime - Current
            Gap = MinGap + Rnd * (MaxGap - MinGap)
            Current = Current + Gap
            If Gap <= TimeLeft Then
                Duration = MinDuration + Rnd * (MaxDuration - MinDuration)
                Counter = Counter + 1
                With Dest
                    .Cells(Counter, 1) = Counter - 1
                    .Cells(Counter, 2) = Format(Current, "mm/dd/yyyy")
                    .Cells(Counter, 3) = Format(Current, "hh:mm:ss")
                    .Cells(Counter, 4) = Format(Duration, "hh:mm:ss")
                End With
                Current = Current + Duration
            End If
        End If
    Loop
   
End Sub


Regards,

Patrick

 

by: byundtPosted on 2005-05-31 at 13:19:26ID: 14116525

Here's a sample workbook showing both formula and VBA methods: http://home.mchsi.com/~byundt/RandomTimesQ21441658.xls

 

by: TlogixPosted on 2005-05-31 at 13:30:35ID: 14116616

Hi Guys..Patrick..thanks for the code..so I ran it  but beyond the 2 input box for start date and end date, I get nothing..
Perhaps you can confirm the requirments for the worksheet, I have a blank sheet and 4 headers in A1-D1, do they need to be named differntly than above example?

 

by: byundtPosted on 2005-05-31 at 13:34:04ID: 14116660

With the code in my sample workbook, you put in headers in cells A1:D1, starting values in A2:B2, then let the VBA macro do the rest. If there is existing data, it will be eradicated. Note that the code references named ranges on the worksheet for the min and max duration and interval, plus the start and end of the workday. You set these with the Insert...Names...Define menu item.

Sub RandomWorkweek()
Dim i As Long
Dim temp As Double
Application.ScreenUpdating = False
Rows("3:1000").ClearContents
[C2].FormulaR1C1 = "=RC[-1]+Start+MinInv+(MaxInv-MinInv)*RAND()"
[D2].FormulaR1C1 = "=IF(RC[-1]="""","""",MinDur+(MaxDur-MinDur)*RAND())"
Cells(2, 3).Resize(1, 2).FormulaR1C1 = Cells(2, 3).Resize(1, 2).Value
For i = 3 To 1000
    temp = [MinInv+(MaxInv-MinInv)*RAND()]
    Cells(i, 1).FormulaR1C1 = "=IF(RC[1]="""","""",R[-1]C+1)"
    Cells(i, 2).FormulaR1C1 = "=IF(R[-1]C[1]+R[-1]C[2]-R[-1]C<=Finish,R[-1]C,IF(WEEKDAY(R[-1]C,2)>=5,"""",R[-1]C+1))"
    Cells(i, 3).FormulaR1C1 = "=IF(RC[-1]="""",""""," & temp & "+IF(R[-1]C+R[-1]C[1]-R[-1]C[-1]+" _
        & temp & "<=Finish,R[-1]C+R[-1]C[1],RC[-1]+Start))"
    Cells(i, 4).FormulaR1C1 = "=IF(RC[-1]="""","""",MinDur+(MaxDur-MinDur)*RAND())"
    Cells(i, 1).Resize(1, 4).FormulaR1C1 = Cells(i, 1).Resize(1, 4).Value
    If Cells(i, 1) = "" Then Exit Sub
Next
Application.ScreenUpdating = True
End Sub

Brad

 

by: TlogixPosted on 2005-05-31 at 13:41:55ID: 14116732

BYundt,
Your example is excellent as always, could'nt workout the temp thing. Also, your example shows one work week..how can I run it for an entire year or more?'
Thanks,

 

by: matthewspatrickPosted on 2005-05-31 at 13:48:44ID: 14116795

Tlogix,
> Hi Guys..Patrick..thanks for the code..so I ran it  but beyond the 2
> input box for start date and end date, I get nothing..
> Perhaps you can confirm the requirments for the worksheet, I have a
> blank sheet and 4 headers in A1-D1, do they need to be named differntly
> than above example?

That's odd--it works OK for me.  I'll post an example.

Regards,

Patrick

 

by: matthewspatrickPosted on 2005-05-31 at 13:50:47ID: 14116817

 

by: byundtPosted on 2005-05-31 at 14:00:37ID: 14116907

Tlogix,
I changed the formula in cell B3 in the formula method to:
=IF(C2+D2-B2<=Finish,B2,IF(WEEKDAY(B2,2)>=5,B2+3,B2+1))
As a result, 8 AM Monday follows 5 PM Friday from the previous week. Copy down as needed.


The macro will take a while. Enjoy some coffee.

Sub RandomWorkweek()
Dim i As Long
Dim temp As Double
Application.ScreenUpdating = False
Rows("3:65536").ClearContents
[C2].FormulaR1C1 = "=RC[-1]+Start+MinInv+(MaxInv-MinInv)*RAND()"
[D2].FormulaR1C1 = "=IF(RC[-1]="""","""",MinDur+(MaxDur-MinDur)*RAND())"
Cells(2, 3).Resize(1, 2).FormulaR1C1 = Cells(2, 3).Resize(1, 2).Value
For i = 3 To 5000
    temp = [MinInv+(MaxInv-MinInv)*RAND()]
    Cells(i, 1).FormulaR1C1 = "=IF(RC[1]="""","""",R[-1]C+1)"
    Cells(i, 2).FormulaR1C1 = "=IF(R[-1]C[1]+R[-1]C[2]-R[-1]C<=Finish,R[-1]C,IF(WEEKDAY(R[-1]C,2)>=5,R[-1]C+3,R[-1]C+1))"
    Cells(i, 3).FormulaR1C1 = "=IF(RC[-1]="""",""""," & temp & "+IF(R[-1]C+R[-1]C[1]-R[-1]C[-1]+" _
        & temp & "<=Finish,R[-1]C+R[-1]C[1],RC[-1]+Start))"
    Cells(i, 4).FormulaR1C1 = "=IF(RC[-1]="""","""",MinDur+(MaxDur-MinDur)*RAND())"
    Cells(i, 1).Resize(1, 4).FormulaR1C1 = Cells(i, 1).Resize(1, 4).Value
    If Cells(i, 1) = "" Then Exit Sub
Next
Application.ScreenUpdating = True
End Sub

Brad

 

by: TlogixPosted on 2005-05-31 at 14:22:46ID: 14117056

Thanks alot Guys--Brad...awesome...I'm going to go out ona limb here, but if I have a slight incling -you'd probably want this to be done this way...
Thanks as always...

 

by: TlogixPosted on 2005-05-31 at 14:28:33ID: 14117084

Brad...
When you have a sec..could you kindly comment the lines the vba where I can chnage the variables:
Max interval, duration, hours, days in weeks etc.
Thanks.
B

 

by: byundtPosted on 2005-05-31 at 14:37:06ID: 14117146

Tlogix,
I don't know if you noticed, but I updated the sample workbook in my posted link. It's a lot bigger now ~660KB http://home.mchsi.com/~byundt/RandomTimesQ21441658.xls

The min and max interval, starting time, ending time, min and max duration are all set on the worksheet "Using formulas" cells H1:H6. I thought for a simulation you might prefer to avoid tweaking the code all the time.

The restriction to Monday through Friday is done here:
    Cells(i, 2).FormulaR1C1 = "=IF(R[-1]C[1]+R[-1]C[2]-R[-1]C<=Finish,R[-1]C,IF(WEEKDAY(R[-1]C,2)>=5,R[-1]C+3,R[-1]C+1))"

Translated into A1 notation:
=IF(C2+D2-B2<=Finish,B2,IF(WEEKDAY(B2,2)>=5,B2+3,B2+1))

If the previous task ended after 5:00 PM (the value in named range Finish), then test the day of the week. If it is Friday or later, then add 3 to get the next day of the week--in other words the following Monday.

Brad

 

by: matthewspatrickPosted on 2005-05-31 at 14:47:54ID: 14117204

Brad,

This might be the first time ever I came up with a more robust solution than you did.

Double :)

Regards,

Patrick

 

by: TlogixPosted on 2005-05-31 at 15:34:28ID: 14117429

Hi Guys

So Patrick  - thanks very much for the solution, I'm not really that qualified to determine the most effective method of impimentation for this solution....I needed a bunch of data and there they were...coool! (duh!)..and ofcourse ol'yoda over there was the first to respond...

What I'd like to do enhance this a little further and post a follow-up question (not sure how to do that..I guess I just post and link here or something) which would skew the randomness of the results:

Assume that events in the range of 3-15mins occur 4/10, 15-30 mins 3/10, 30-45 mins 1.5/10, 45-1hr 1/10, 1hr-2hrs 0.5/10
and then tie this into the current code to generate more simulated data based on my enviroment.

Please give me your feedback on this guys and let me know if I should proceed with new Q.

Regards
B.

 

by: matthewspatrickPosted on 2005-05-31 at 18:28:33ID: 14118212

B,

Go ahead and post a follow-up question.  Just be very specific about what you need to be produced.

To be safe, it would be wise to post back here with a link to the new question--that way, Brad and I will
be sure to see it.

Patrick

 

by: byundtPosted on 2005-05-31 at 20:46:20ID: 14118716

B.
Patrick got me going with his teasing. I've modified my code to use array transfer, and it will post an entire year's worth of random dates and times with about two seconds effort.

And to raise the ante, I've added the requested feature on making the duration vary according to a probability curve. With each turn of the loop, the code picks a random number. It then finds the bin that number belongs to in a cumulative probability based on your brackets. Finally, it chooses a random number within that bin.

As before, the code puts the parameters for the random model on the worksheet where they're easy to tweak. And I liked Patrick's feature of asking for starting and stopping dates, and so incorporated it into the sub. Here's the sample workbook link: http://home.mchsi.com/~byundt/RandomTimesQ21441658.xls and the new code:

Sub RandomWorkweek()
Dim i As Long, j As Long, WkStart As Long, WkEnd As Long, nDur As Long
Dim temp As Double, tempDur As Double, MinDur As Double, MaxDur As Double, MinInv As Double, MaxInv As Double, _
    Start As Double, Finish As Double
Dim StartDate As Date, EndDate As Date
Dim CumDur As Variant, X As Variant, tbDur As Variant

[A2:D65536].ClearContents
Randomize

StartDate = DateValue(Application.InputBox("Please specify a starting date", Type:=2))
EndDate = DateValue(Application.InputBox("Please specify an ending date", Type:=2))
tbDur = Range("tbDur").Value
MinInv = Range("MinInv").Value
MaxInv = Range("MaxInv").Value
Start = Range("Start").Value
Finish = Range("Finish").Value
WkStart = Range("WkStart").Value
WkEnd = Range("WkEnd").Value

nDur = UBound(tbDur)
ReDim CumDur(1 To nDur)
For i = 1 To nDur
    CumDur(i) = tbDur(i, 3)
Next

ReDim X(1 To 65535, 1 To 4)
X(1, 1) = 1
X(1, 2) = StartDate
X(1, 3) = X(1, 2) + Start + MinInv + (MaxInv - MinInv) * Rnd()
X(1, 4) = MinDur + (MaxDur - MinDur) * Rnd()
For i = 2 To 65535
    temp = MinInv + (MaxInv - MinInv) * Rnd()
    tempDur = Rnd()
    j = Application.Match(tempDur, CumDur, 1)
    MinDur = tbDur(j, 1)
    MaxDur = tbDur(j + 1, 1)
    If X(i - 1, 3) + X(i - 1, 4) - X(i - 1, 2) + temp > Finish Then
        If Weekday(X(i - 1, 2), 2) = WkEnd Then
            X(i, 2) = X(i - 1, 2) + 7 - WkEnd + WkStart
        Else
            X(i, 2) = X(i - 1, 2) + 1
        End If
        X(i, 3) = X(i, 2) + Start + temp
    Else
        X(i, 2) = X(i - 1, 2)
        X(i, 3) = X(i - 1, 3) + X(i - 1, 4) + temp
    End If
    X(i, 4) = MinDur + (MaxDur - MinDur) * Rnd()
    X(i, 1) = X(i - 1, 1) + 1
    If X(i, 3) + X(i, 4) > EndDate + Finish Then Exit For
Next

[A2:D65536].Value = X
Columns("B").NumberFormat = "dddd m/d/yyyy"
Range("C:D").NumberFormat = "h:mm:ss"
If i > 20 Then Range(i & ":65536").EntireRow.Delete
i = ActiveSheet.UsedRange.Rows.Count

End Sub

Brad

 

by: TlogixPosted on 2005-06-01 at 05:57:57ID: 14121028

Hi Guys...
Brad..thanks alot for the above...very nice of you..will run it now ands give feedback...but I really think we should create another Q to be fair.

I will be creating a question with the next part of the requirement...this can be standalone.
Here's the task and I'll link in next post to the question:

I need to hold a memo field in my Db and I would like to create this somewhat randomly in terms of it's length, so here's what I came up with;

I have a block of text with 137 spaces. What if we randomly select a space count (with a skewed probability scale of say 10 levels and then select all the text from the start upto that space count and then copy it to a cell? I would also need something like a unqiue value in each memo field so I can track searches and queries...so something like an alpha permutation on for levels: xxxxaaaa (the 'aaaa' would be the permutated values, the xxxx is to avoid permutation that result in duplicated words in the text line 'code').

// So it would work like this where the text block is:
Patrick never temp the gods of VBA
// and the space count is 4, the text would copied text would be
Patrick never temp the
// and if this were the 3rd random generated text the perm would be
xxxaaac
// the entire copied value would be
Patrick never temp the xxxxaaac
// If we generated a zero space random count and this was the 5th perm, the result would be
xxxxaaae

I was thinking we put the text block in A1 on sheet 1 and generate the results in sheet 2.
The text block will hold a-z, 0-9 and %$!@>?*& chrs etc.

Sounds like a bunch of nonsense..but I need to test from alot of angles and this is what i came up with. Can you guys help? Please let me know.
Regards,
B

 

by: TlogixPosted on 2005-06-01 at 06:16:33ID: 14121183

 

by: matthewspatrickPosted on 2005-06-01 at 08:29:47ID: 14122679

B,

Kevin seems to have a pretty good handle on the new question, and I must respond to Brad's challenge :)

Here is a version of the sub that will do the revised distribution:

Sub GenerateEvents2()
   
    Dim Dest As Worksheet
    Dim LastRow As Long
    Dim Counter As Long
    Dim StartDate As Date, EndDate As Date
    Dim MinDuration, MaxDuration
    Dim MinGap As Date, MaxGap As Date
    Dim Current As Date, QuitTime As Date, TimeLeft As Date, Gap As Date, Duration As Date
    Dim DurationRnd As Double
    Dim DurationFilter As Long
   
    MinDuration = Array(#12:03:00 AM#, #12:15:00 AM#, #12:30:00 AM#, #12:45:00 AM#, #1:00:00 AM#)
    MaxDuration = Array(#12:15:00 AM#, #12:30:00 AM#, #12:45:00 AM#, #1:00:00 AM#, #2:00:00 AM#)
   
    MinGap = #12:01:00 AM#
    MaxGap = #12:40:00 AM#
   
    Set Dest = ThisWorkbook.Worksheets("Sheet1")
   
    StartDate = InputBox("What is the start date for your event range", "Start Date")
    EndDate = InputBox("What is the end date for your event range", "End Date")
    EndDate = DateAdd("h", 17, EndDate)
   
    LastRow = Dest.Cells(65536, 1).End(xlUp).Row
    If LastRow > 1 Then Range(Dest.Cells(2, 1), Dest.Cells(65536, 1)).EntireRow.Delete
   
    Current = DateAdd("h", 8, StartDate)
    Counter = 1
   
    Randomize
   
    Do Until Current >= EndDate
        If Weekday(Current, vbSunday) = 1 Then
            Current = DateAdd("h", 8, Int(Current) + 1)
        ElseIf Weekday(Current, vbSunday) = 7 Then
            Current = DateAdd("h", 8, Int(Current) + 2)
        ElseIf Hour(Current) > 16 Then
            Current = DateAdd("h", 8, Int(Current) + 1)
        Else
            QuitTime = DateAdd("h", 17, Int(Current))
            TimeLeft = QuitTime - Current
            Gap = MinGap + Rnd * (MaxGap - MinGap)
            Current = Current + Gap
            If Gap <= TimeLeft Then
                DurationRnd = Rnd
                Select Case DurationRnd
                    Case Is < 0.4: DurationFilter = 0
                    Case Is < 0.7: DurationFilter = 1
                    Case Is < 0.85: DurationFilter = 2
                    Case Is < 0.95: DurationFilter = 3
                    Case Else: DurationFilter = 4
                End Select
                Duration = MinDuration(DurationFilter) + Rnd * _
                    (MaxDuration(DurationFilter) - MinDuration(DurationFilter))
                Counter = Counter + 1
                With Dest
                    .Cells(Counter, 1) = Counter - 1
                    .Cells(Counter, 2) = Format(Current, "mm/dd/yyyy")
                    .Cells(Counter, 3) = Format(Current, "hh:mm:ss")
                    .Cells(Counter, 4) = Format(Duration, "hh:mm:ss")
                End With
                Current = Current + Duration
            End If
        End If
    Loop
   
End Sub


Patrick

 

by: TlogixPosted on 2005-06-02 at 03:35:12ID: 14129171

Hi Brad...
I a slight mod to the code please, is there anyway we can make this repeat 'x' number of times and for each repeat renumerate the events - so the result would be a repeated run on the same ranges with events 1 thru X, 1a thru Xa, 1b thru Xb....based on the number of runs specified?

Much appreciated, please let me know if I can post a question for this.
Thanks,
B

 

by: byundtPosted on 2005-06-03 at 18:01:40ID: 14144190

B,
I've uploaded a revised sample workbook showing revised code with the ability to generate up to 256 data sets (if they fit on the worksheet). As before, the control parameters are set on the worksheet, with VBA code doing the random date and time generation. http://home.mchsi.com/~byundt/RandomTimesQ21441658.xls

Sub RandomWorkweek()
Dim i As Long, ii As Long, j As Long, WkStart As Long, WkEnd As Long, nDur As Long, NumSets As Long
Dim temp As Double, tempDur As Double, MinDur As Double, MaxDur As Double, MinInv As Double, MaxInv As Double, _
    Start As Double, Finish As Double
Dim StartDate As Date, EndDate As Date
Dim rg As Range
Dim CumDur As Variant, X As Variant, tbDur As Variant
Dim suffix As String

[A2:D65536].ClearContents
Randomize

On Error Resume Next
StartDate = DateValue(Application.InputBox("Please specify a starting date", Type:=2))
EndDate = DateValue(Application.InputBox("Please specify an ending date", Type:=2))
On Error GoTo 0
If StartDate * EndDate = 0 Then Exit Sub
tbDur = Range("tbDur").Value
MinInv = Range("MinInv").Value
MaxInv = Range("MaxInv").Value
Start = Range("Start").Value
Finish = Range("Finish").Value
WkStart = Range("WkStart").Value
WkEnd = Range("WkEnd").Value
NumSets = Range("NumSets").Value

nDur = UBound(tbDur)
ReDim CumDur(1 To nDur)
For i = 1 To nDur
    CumDur(i) = tbDur(i, 3)
Next

For ii = 1 To NumSets
    If ii > 1 Then suffix = LCase(Mid(Cells(1, ii - 1).Address, 2, InStr(2, Cells(1, ii - 1).Address, "$") - 2))
    ReDim X(1 To 65535, 1 To 4)
    X(1, 1) = 1 & suffix
    X(1, 2) = StartDate
    X(1, 3) = X(1, 2) + Start + MinInv + (MaxInv - MinInv) * Rnd()
    X(1, 4) = MinDur + (MaxDur - MinDur) * Rnd()
    i = 1
    Do Until i = 65535
        i = i + 1
        temp = MinInv + (MaxInv - MinInv) * Rnd()
        tempDur = Rnd()
        j = Application.Match(tempDur, CumDur, 1)
        MinDur = tbDur(j, 1)
        MaxDur = tbDur(j + 1, 1)
        If X(i - 1, 3) + X(i - 1, 4) - X(i - 1, 2) + temp > Finish Then
            If X(i - 1, 2) = EndDate Then Exit Do
            If Weekday(X(i - 1, 2), 2) = WkEnd Then
                X(i, 2) = X(i - 1, 2) + 7 - WkEnd + WkStart
            Else
                X(i, 2) = X(i - 1, 2) + 1
            End If
            X(i, 3) = X(i, 2) + Start + temp
        Else
            X(i, 2) = X(i - 1, 2)
            X(i, 3) = X(i - 1, 3) + X(i - 1, 4) + temp
        End If
        X(i, 4) = MinDur + (MaxDur - MinDur) * Rnd()
        X(i, 1) = Val(X(i - 1, 1)) + 1 & suffix
    Loop
    Set rg = Cells(65536, 1).End(xlUp).Offset(1, 0)
    Range(rg, rg.Offset(i - 1, 3)).Value = X
Next

Columns("B").NumberFormat = "dddd m/d/yyyy"
Range("C:D").NumberFormat = "h:mm:ss"
i = ActiveSheet.UsedRange.Rows.Count

End Sub


Brad

 

by: TlogixPosted on 2005-06-05 at 12:30:08ID: 14149426

Hi Brad,
Excuse the delay in responding, thanks very much again the additional support - would have like to award points for the above and if there is a way to do so, please let me know.

Best wishes,
B

btw: I posted a question for C# programming - is related to excel..wondering if you have some input....application in C# basically exports a dataset to XLS, I want to have it create a hyperlink for a value (the value is currently a path and file name) it can be clicked in xl to open the file. HAven't had too much luck setting it as a hyperlink.

 

by: TlogixPosted on 2005-06-05 at 12:59:17ID: 14149500

Rather a nice position for you Mr. B...Judge, jury and treasurer....good training from presidential position in latin american country. Think big!
:)

B (little)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...