Problems with Random sorting macro

LuukRost

New Member
Joined
Dec 8, 2016
Messages
6
Hi,

I work in a school and i have to make an excelsheet that assigns the names from the students into cells on a new tab to randomly assign them to seats when taking tests or exams.

So i have one tab called "students", in this tab there is a row from A3 to A36 for the first class of students.
In the second tab called "sorting" i have a mock setup of the class layout so 4 tables wide and 7 tables long with the range B3 to E9.

What i want to do is make a macro button that takes the names of the students in tab "students" and randomly sorts them into the class layout in the tab "sorting".

I have gone so far as to make the macro working but it removes the references to the student cells in the "students" tab.

I am pretty ok with some light coding but this is a bit challenging, hence the post.

Can anyone help me with this?

Thanks in advance!

This is the code:

Sub Rechthoek1_Klikken()
Dim v As Variant, t As Variant, x As Long
Dim RwCell1 As Long, RwCell2 As Long, ColCell1 As Long, ColCell2 As Long
Dim Rw As Long, Col As Long, Iterations As Long
v = Selection.Value
Rw = Selection.Rows.Count
Col = Selection.Columns.Count
Iterations = Rw * Col
For x = Iterations To 1 Step -1


RwCell1 = Int(1 + Rw * Rnd)
RwCell2 = Int(1 + Rw * Rnd)
ColCell1 = Int(1 + Col * Rnd)
ColCell2 = Int(1 + Col * Rnd)
If Selection.Rows.Count = 1 Then
If ColCell1 <> ColCell2 Then
t = v(RwCell1, ColCell1)
v(RwCell1, ColCell1) = v(RwCell2, ColCell2)
v(RwCell2, ColCell2) = t
End If
ElseIf Selection.Columns.Count = 1 Then
If RwCell1 <> RwCell2 Then
t = v(RwCell1, ColCell1)
v(RwCell1, ColCell1) = v(RwCell2, ColCell2)
v(RwCell2, ColCell2) = t
End If
Else
If RwCell1 <> RwCell2 And ColCell1 <> ColCell2 Then
t = v(RwCell1, ColCell1)
v(RwCell1, ColCell1) = v(RwCell2, ColCell2)
v(RwCell2, ColCell2) = t
End If
End If
Next x
Selection.Value = v
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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