HERBERS Excel-Forum - die Beispiele

Thema: Reihenfolge einer Zahlenreihe nach dem Zufallsprinzip variieren

Home

Gruppe

Allgemein

Problem

Wie kann ich die Reihenfolge der Werte eines ausgewählten Bereiches in den Nebenspalten nach dem Zufallsprinzip 150 mal variieren?

Lösung
Geben Sie den nachfolgenden Code in ein Standardmodul ein und weisen Sie ihn einer Schaltfläche zu.
StandardModule: basMain

Sub Zufall()
   Dim iStart As Integer, iEnd As Integer
   Dim iCol As Integer, iRow As Integer
   Dim iZufall As Integer, iCount As Integer
   Randomize
   Range("A1").CurrentRegion.Columns(1).Select
   iCount = Selection.Cells(Selection.Rows.Count)
   iStart = Selection.Row
   iEnd = iStart + iCount - 1
   iZufall = Int((Selection.Rows.Count * Rnd) + 1)
   For iCol = Selection.Column To Selection.Column + 150
      For iRow = iStart To iEnd
         Do Until IsEmpty(Cells _
            (Selection.Row + iZufall - 1, iCol + 1))
            iZufall = Int((iCount * Rnd) + 1)
         Loop
         Cells(Selection.Row + iZufall - 1, iCol + 1) = _
            Cells(iRow, iCol)
      Next iRow
   Next iCol
   Range("A1").Select
End Sub