Help with VBA to "shuffle cells" for a fixture list (edit to control variables)

JDoc27

Board Regular
Joined
Aug 1, 2012
Messages
60
Hi,

I have a VBA that will happily shuffle cells within a column, I am looking to expand on this and have it shuffle across rows of 2 also as I need it to create fixtures from a list on a regular basis, therefore I need Home and Away teams changing on a weekly basis.

This is the current code I have (I realise I cannot use the FOR control again as it won't let me, I hopefully need the workaround):


Private Sub CommandButton1_Click()
Call Shuffle
End Sub


Sub Shuffle()
Dim rng As Range
Dim num_rows As Integer
Dim num_cols As Integer
Dim temp() As Object
Dim row As Integer
Dim col As Integer
Dim swap_row As Integer
Dim temp_object As String



Set rng = Application.Selection
num_rows = rng.Rows.Count
num_cols = rng.Columns.Count

If ((num_rows < 2) Or (num_cols < 1)) Then
MsgBox "You must select at least 2 rows and 1 column."
Exit Sub
End If


For row = 1 To num_rows - 1

swap_row = row + CInt(Int((num_rows - row + 1) * Rnd()))


If (row <> swap_row) Then
For col = 1 To num_cols
temp_object = rng(row, col)
rng(row, col) = rng(swap_row, col)
rng(swap_row, col) = temp_object

Next col
End If
Next row
End Sub
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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