Macro Help

Dan Wilson

Well-known Member
Joined
Feb 5, 2006
Messages
546
Office Version
  1. 365
Platform
  1. Windows
Good day. Grab a cold one, this will take a while to explain. I have a Worksheet titled “Random Picks” with a Macro that creates 40 individual random numbers and guarantees that none of the 40 numbers are duplicates. In another Workbook titled “50-69” there is a worksheet that lists 3500 songs that are in my music folder. That same worksheet contains a Column that tracks the last time each song was used in my weekly podcast. This is done so that no songs are repeated in a 6-week time period. On the “Random Picks” Worksheet there is also a list of the 40 random numbers in Cells B8 thru B47. Cell N8 is changed each week to contain the most recent Podcast Number. At this point each of the values found in Cells B8 thru B47 are examined by comparing the value found with the value found in N8 with a value of 6 subtracted from the most recent Podcast. For example, the value in N8 may be 90, thus the number being compared to the values in B8 thru B47 would be 84. If the value found in B8 thru B47 is less than the comparison number, the Cell in K8 thru K47 would be left blank. If the value found is 84 or higher, the word “bad” is placed in the Cell in Column K for that comparison. Cell N11 then counts the word “bad” found in Cells K8 thru K47. On the “Random Picks” worksheet there is a Macro that causes the random numbers to be generated in the first place. I would like to alter that Macro so that when it executes the random number generation continuously until Cell N11 contains a zero. I have tried to make a test Macro, but I’m not having much luck. Any help will be appreciated.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
It might help if you posted the code you're using. Otherwise, it's impossible for anyone to see what changes might be needed.

At its simplest you might use something like:
VBA Code:
Sub Demo()
With Sheets("Random Picks")
  Do While .Range("N11").Value <> 0
    Application.Calculate
  Loop
End With
End Sub
replacing 'Application.Calculate' with a call to the sub that actually does the random number generation.
 
Upvote 0
It might help if you posted the code you're using. Otherwise, it's impossible for anyone to see what changes might be needed.

At its simplest you might use something like:
VBA Code:
Sub Demo()
With Sheets("Random Picks")
  Do While .Range("N11").Value <> 0
    Application.Calculate
  Loop
End With
End Sub
replacing 'Application.Calculate' with a call to the sub that actually does the random number generation.

Good day Macropod and thank you for responding. Let's try a different approach. Copied below is the formula that creates a random number between 1 and 3009 in each of the Cells from Row 3 (A3 thru AN3) of the worksheet "Random Picks". The formula guarantees that are no duplicates in the 40 numbers chosen. There are currently 3009 songs in my music folder and that number is extracted from another workbook where the total count is stored and automatically updated.

=TAKE(SORTBY(SEQUENCE(1,C1),RANDARRAY(1,C1)),,40)

Below are the two Macros that monitor and copy the numbers selected above. The "Copy_Random_Numbers" Macro copies the numbers found in Row 3 into the spaces in Row 4. After using the Macros, I discovered that there is no need to run the "Get_Random_Numbers" Macro as running the "Copy_Random_numbers" Macro causes everything in Row 3 to be changed. Cell AO3 labels Row 3 as "Actual" numbers found and Cell AO4 labels Row 4 as "Hold".

Sub Get_Random_Numbers()
'
' Copy_Random_Numbers Macro
' get random numbers and check for bad
'
'
Calculate
Range("A6").Select
End Sub


Sub Copy_Random_Numbers()
'
' Copy_Random_Numbers Macro
' copy random numbers into hold
'
'
Range("A3:AN3").Select
Selection.Copy
Range("A4:AN4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A6").Select
Application.CutCopyMode = False
End Sub

Here is an explanation of the lower part of the "Random Picks" worksheet. The area covers rows 8 thru 47 and Columns A thru N. Column A numbers the song selections from 1 to 40. Column B contains a LOOKUP function that pulls the Podcast Number of the last time that song was played from another Workbook. Columns C thru J are merged to show the song title accessed by an INDEX function. to another Workbook. Cell N8 contains the most recent Podcast number that is manually changed weekly. Cells K8 thru K47 contain the formula
=IF(B8>$N$8-10,"bad","")
where the 8 in B8 is incremented in each row to determine if each next song has been played in the last 10 Podcasts. Cell N11 then uses the formula
=COUNTIF(K8:K47,"bad")
to count the number of "bad" found. The goal of all this is to repeat the "Copy_Random_Numbers" Macro until Cell N11 contains a zero, meaning that all of the songs chosen have not been used in the most recent 10 Podcasts. I hope this helps. I tried using your suggestion, but could not get it to work. Thank you for any help.
 
Upvote 0
@Dan Wilson
Below is an example of how you might wish to approach this in a way that does not require iterations to eliminate the 'bad' tunes?
Maybe copy the below to a blank workbook and test the basic principle?
I only have the picks and tunes data in separate sheets but they could easily be in separate workbooks.
Each run of the code should provide a valid play list.

VBA Code:
Sub PickTunes()

Dim lsttr As Long
Dim tunes As Worksheet
Dim randrng As Range

Set tunes = Sheets("50-69")  'Tunes data

lsttr = tunes.Range("A" & Rows.Count).End(xlUp).Row 'last tune row
Set randrng = tunes.Range("F2:F" & lsttr)

randrng.Formula = "=IF(B2>'Random Picks'!$C$3-10,99999,RAND())"
randrng = randrng.Value

End Sub


TunePicker.xlsm
ABCD
1
2
3This PodCast#57
4
5
6Playlist
7Tune #Title
81Title30
92Title5
103Title10
114Title48
125Title28
136Title51
147Title16
158Title4
169Title40
1710Title59
1811Title56
1912Title58
2013Title11
2114Title18
2215Title24
2316Title34
2417Title9
2518Title27
2619Title19
2720Title20
2821Title32
2922Title29
3023Title55
3124Title25
3225Title54
3326Title12
3427Title26
3528Title23
3629Title8
3730Title6
3831Title1
3932Title44
4033Title57
4134Title2
4235Title17
4336Title31
4437Title35
4538Title3
4639Title22
4740Title60
48
Random Picks
Cell Formulas
RangeFormula
B8:B47B8=INDEX('50-69'!A2:A5000,MATCH(A8:A47,'50-69'!G2:G5000,0))
Dynamic array formulas.


TunePicker.xlsm
ABCDEFG
1TitleLast PlayedCode generated Random #Rank
2Title1140.76064902631
3Title2150.78590337134
4Title3160.84916210738
5Title4170.1450295938
6Title5180.0132544112
7Title6190.73829781430
8Title7200.93680682543
9Title8210.73223910529
10Title9420.53193093317
11Title10230.0258639263
12Title11240.31380320213
13Title12250.69240982526
14Title13260.94803508645
15Title14659999999999
16Title15280.91415592541
17Title16290.0934901537
18Title17300.81031744235
19Title18310.34739561914
20Title19320.55426188619
21Title20330.55672959320
22Title21340.92446695642
23Title22350.89330456339
24Title23360.72679403628
25Title24370.39614641315
26Title25380.64308052524
27Title26390.70550992827
28Title27400.55000383718
29Title28410.0785504525
30Title29160.59807047522
31Title30430.00607181
32Title31440.82615192136
33Title32450.57846064521
34Title33460.94309090344
35Title34470.47469888416
36Title35170.84346947837
37Title36499999999999
38Title37509999999999
39Title38519999999999
40Title39629999999999
41Title40220.1719466299
42Title41609999999999
43Title42599999999999
44Title43589999999999
45Title44120.7750571232
46Title45569999999999
47Title46559999999999
48Title47549999999999
49Title48250.0420891824
50Title49529999999999
51Title50519999999999
52Title51130.0927844646
53Title52499999999999
54Title53489999999999
55Title54470.6709144225
56Title55100.62707534423
57Title56450.20409937511
58Title57440.7799145633
59Title58430.26055967412
60Title59420.20206632710
61Title60410.91355114440
62
50-69
Cell Formulas
RangeFormula
G2:G5000G2=IFERROR(IF(F2:F5000=99999,99999,RANK(F2:F5000,F$2:F$5000,1)),"")
Dynamic array formulas.


HTH
 
Upvote 0
@Dan Wilson
Below is an example of how you might wish to approach this in a way that does not require iterations to eliminate the 'bad' tunes?
Maybe copy the below to a blank workbook and test the basic principle?
I only have the picks and tunes data in separate sheets but they could easily be in separate workbooks.
Each run of the code should provide a valid play list.

VBA Code:
Sub PickTunes()

Dim lsttr As Long
Dim tunes As Worksheet
Dim randrng As Range

Set tunes = Sheets("50-69")  'Tunes data

lsttr = tunes.Range("A" & Rows.Count).End(xlUp).Row 'last tune row
Set randrng = tunes.Range("F2:F" & lsttr)

randrng.Formula = "=IF(B2>'Random Picks'!$C$3-10,99999,RAND())"
randrng = randrng.Value

End Sub

[/QUOTE]
Good Snakehips and thank you for responding. I’m not sure what happened, but your response leads me to believe that you think I want to check every song and remove those that come up “bad”. The Macros that are now in place work fine. All I want to do is modify the Macro so that if the count of “bad” is more than zero, to recall the Macro over and over until the count is zero. I’m sorry for the confusion. I must have rewritten my response several times until I thought I covered all the details.

EDIT: Moderator removed reply from quote.
 
Last edited by a moderator:
Upvote 0
^^^^ Your response appears to have got absorbed into the quote above but I have managed to read it.
Apologies if I have misinterpreted. I had imagined that you were looking to compile a random list of 40 tunes that had not been broadcast during the last 10 podcasts?
 
Upvote 0
Good day again Snakehips and thank you for responding. I am confused. Your response "you were looking to compile a random list of 40 tunes that had not been broadcast during the last 10 podcasts" is correct. However, after looking at your solution (which I don't understand), I realized that my explanation of the problem was missing something. I am not experienced enough with Excel to completely understand the steps you wrote. I'm sure that it would work, but my level of expertise with Excel does not convince me that I would understand what is going on. I appreciate the effort you put into your response and I'm sorry that I don't understand it. At this point I am executing the "Copy Random Numbers" Macro over and over until the "bad" count is zero. Since all of that works, it seems that the easiest fix is to alter the Macro so that if the "bad" count is not zero, the Macro will run again until the count is zero. I wish I had the knowledge and experience that people such as yourself have. Thank you and I hope that you understand.
 
Upvote 0
@Dan Wilson Hi Dan, give this alternative code a try.

VBA Code:
Sub Copy_Random_Numbers()
'
' Copy_Random_Numbers Macro
' copy random numbers into hold
' repeat until zero "bad'
'
Application.ScreenUpdating = False  'disable screen updating to aid speed and visuals

Msg = "Sorry - Tr1ed But Failed"
For try = 1 To 3000   'set top limit of trys in order to avoid any danger of infinite looping

    Range("A4:AN4") = Range("A3:AN3").Value 'copy latest randoms
    If Range("N11") = 0 Then
        Msg = "Done"
        GoTo Done 'quit loop if 'bad' count is zero
    End If
Next try   'otherwise try again
    
Range("A6").Select

'Point of exit
Done:
Application.ScreenUpdating = True  'reset screen updating
MsgBox Msg
End Sub
 
Upvote 0
Good day again Snakehips and CONGRATULATIONS! Your recent response allowed me to understand what is happening and with a couple modifications, it now works! Below is the Macro as modified. I especially l like the ScreenUpdating function. I had a terrible time making i t work until I realized that Cell N11 should have been N14 as I swapped Cells N11 and N14. There really is no reason to run 3000 trys, so I cut it down to 300. Thank you for sticking with this one. I appreciate the extra effort and your patience.

Sub Copy_Random_Numbers()
'
' Copy_Random_Numbers Macro
' Copy random numbers and check for bad - stop on zero count
'
Application.ScreenUpdating = false
msg = "Sorry - failed"
For try = 1 To 3000
'
Range("a4:an4") = Range("a3:an3").Value
If Range("N14") = 0 Then
msg = "Done"
GoTo done
End If
Next try
Range("a6").Select
done:
Application.ScreenUpdating = True
msg = "Sorry - failed"
MsgBox msg
'
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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