Modifying a Randomised Alphabet Worksheet

dpbarry

Board Regular
Joined
Feb 10, 2003
Messages
214
Office Version
  1. 2016
Platform
  1. Windows
Hi Folks.

I have a spreadsheet which randomised 29 'letters' - 'A to Z' plus 'Mac', 'Mc' and O'.

1638353061045.png



As it stands, it it works great but now our Principal wants the Mac, Mc and O' taken out. If I remove the relevant columns, this happens. I get spaces in the Randomised list

1638353230987.png


I've tried modifying the VB Code:

Option Explicit

Private Sub Shuffle_Click()
Dim Letters(1 To 29) As String
Dim Mask As String
Dim Counter, RandNumber As Integer

For Counter = 1 To 29
Letters(Counter) = Cells(3, Counter + 1)
Next

Mask = "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]"
Randomize

For Counter = 1 To 29 'Randomise Letters Array Values
RandNumber = Int((Len(Mask) - 1 + 1) * Rnd + 1)
Cells(7, Counter + 1) = Letters(Asc(Mid(Mask, RandNumber, 1)) - 64)
Mask = Left(Mask, RandNumber - 1) & Right(Mask, Len(Mask) - RandNumber)
Next
End Sub

to:

Option Explicit

Private Sub Shuffle_Click()
Dim Letters(1 To 26) As String
Dim Mask As String
Dim Counter, RandNumber As Integer

For Counter = 1 To 26
Letters(Counter) = Cells(3, Counter + 1)
Next

Mask = "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]"
Randomize

For Counter = 1 To 26 'Randomise Letters Array Values
RandNumber = Int((Len(Mask) - 1 + 1) * Rnd + 1)
Cells(7, Counter + 1) = Letters(Asc(Mid(Mask, RandNumber, 1)) - 64)
Mask = Left(Mask, RandNumber - 1) & Right(Mask, Len(Mask) - RandNumber)
Next
End Sub
but I end up with a error

1638353433218.png


and when I debug, ths is where the error occurs

1638353391891.png


I didn't write the code so I'm not sure what is happening and would appreciate any assistance as to how to rectify the error

Regards

Declan
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
change

Mask = "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]" to

Mask = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

should work
 
Upvote 0
In second code remove last 3 signs from mask into:
VBA Code:
Mask = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
and should be ok.
 
Upvote 0
Solution
Thank You AlanY and KOKOSEK.

That did the job. Could you explain what removing [\] does to the code? Its just to get an understanding of what is happening.

Regards
 
Upvote 0
Thank You AlanY and KOKOSEK.

That did the job. Could you explain what removing [\] does to the code? Its just to get an understanding of what is happening.

Regards
you're welcome.

it just reduce the 26+3 to 26 letters
 
Upvote 0
Ascii codes for [\] are 91,92 and 93, after minus 64 gives 27,28 and 29. In row:

VBA Code:
Cells(7, Counter + 1) = Letters(Asc(Mid(Mask, RandNumber, 1)) - 64)

you want to put element 27,28 or 29 into cell but Letters array has only 26 elements.

VBA Code:
For Counter = 1 To 26
Letters(Counter) = Cells(3, Counter + 1)
Next
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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