How to generate random weekday or weekend date in Excel?
This tutorial is talking about generate only random weekdays or only random weekend dates in Excel, follow below steps to get more details.
In Excel, there are two methods can help you randomize only weekdays in a date range as you need.
Method 1 generate random weekday with formula
Select a cell and type this formula =DATE(2014, 1, 1)+LARGE(IF(WEEKDAY(DATE(2014, 1, 1)+ROW($1:$365)-1, 2)<6, ROW($1:$365)-1, ""), RANDBETWEEN(1, SUM(--(WEEKDAY(DATE(2014, 1, 1)+ROW($1:$365)-1, 2)<6)))), then press Shift + Ctrl + Enter keys simultaneously, then drag the fill handle to a range you want to generate weekday. See screenshot:
1. If you want to generate unique random weekdays, you can use this formula =DATE(2014, 1, 1)+(LARGE(IF(WEEKDAY((DATE(2014, 1, 1)+ROW($1:$365)-1)*((COUNTIF($A$1:A1, DATE(2014, 1, 1)+ROW($1:$365)-1))=0), 2)<6, ROW($1:$365)-1, ""), RANDBETWEEN(1, SUM(--(WEEKDAY((DATE(2014, 1, 1)+ROW($1:$365)-1)*((COUNTIF($A$1:A1, DATE(2014, 1, 1)+ROW($1:$365)-1))=0), 2)<6))))) and remember to press Shift + Ctrl + Enter.
2. In above formulas, 2014,1,1 is the start date of your needed date range, 365 is the number of days in your date range, you can change them as you need.
3. You cannot put the formula generating unique random weekdays in A1.
Method 2 generate random weekday within a year with VBA
If you want to randomize weekdays within a year and are familiar to VBA, you can do as below:
1. Press Alt + F11 keys to open Microsoft Visual Basic for Applications window.
2. Click Insert > Module, then paste below VBA code to the popping windows.
VBA: Randomize weekday within a year.
Dim RandomizedYet As Boolean 'Updateby20150108 Function RandomizeDates(pYear As Long) Dim i As Long Dim DaysInYear As Long Dim xIndex As Long Dim RndIndex As Long Dim Temp As Date Dim Weekdays() As Variant If Not RandomizedYet Then RandomizedYet = True Randomize End If DaysInYear = DateSerial(pYear + 1, 1, 1) - DateSerial(pYear, 1, 1) ReDim Weekdays(1 To DaysInYear) For i = 1 To DaysInYear If Weekday(DateSerial(pYear, 1, i), vbMonday) < 6 Then xIndex = xIndex + 1 Weekdays(xIndex) = DateSerial(pYear, 1, i) End If Next ReDim Preserve Weekdays(1 To xIndex) For i = xIndex To 1 Step - 1 RndIndex = Int(i * Rnd + 1) Temp = Weekdays(RndIndex) Weekdays(RndIndex) = Weekdays(i) Weekdays(i) = Temp Next RandomizeDates = Application.WorksheetFunction.Transpose(Weekdays) End Function
3. Save the code and close the window to go back your worksheet, and type this formula=RandomizeDates(2014) into a cell, then press Enter key and drag the fill handle to a range you need. See screenshot:
|Kutools for Excel, with more than 300 handy functions, makes your jobs more easier.|
After free installing Kutools for Excel, please do as below:
1. Select a range you want to generate weekdays or weekend dates, then click Kutools > Insert Random Data. See screenshot:
2. In the Insert Random Data dialog, click Date tab, and then calendar button to select a start date and end date you want, then check Workday or Weekend option you need, if you want to generate unique date, you can check Unique option. See screenshot:
3. Click Ok to apply and close the dialog, then in the selection, the unique weekend dates are generated.
Tip: If you are allowed to insert duplicates data, just uncheck Unique Values in the dialog
With Insert Random data function, you also can randomize integer, time, string and custom list. Click here for more information.