Hallo Leute,
ich bin bei VBA noch recht unbeleckt. Ich möchte zufällig Werte aus zwei Listen (zwei Spalten) auswählen und in zwei neue Spalten einfügen. Ich habe dazu schon ein Code gefunden.
StandardModule: basMain
Sub Zufall()
Dim rngAct As Range, rngPart As Range, rngFind As Range
Dim iCol As Integer, iTmp As Integer
Dim iRow As Integer, iCount As Integer
Application.ScreenUpdating = False
Randomize
If IsEmpty(Cells(1, 3)) Then
iRow = 1
Else
iRow = Cells(Rows.Count, 3).End(xlUp).Row + 1
End If
iCount = WorksheetFunction.CountA(Columns(1))
For iCol = 1 To 2
Set rngPart = Range(Cells(1, iCol + 2), Cells(iCount, iCol + 2))
iTmp = Int((iCount * Rnd) + 1)
Set rngFind = rngPart.Find(Cells(iTmp, iCol), _
lookat:=xlWhole, LookIn:=xlValues)
While Not rngFind Is Nothing
iTmp = Int((iCount * Rnd) + 1)
Set rngFind = rngPart.Find(Cells(iTmp, iCol), _
lookat:=xlWhole, LookIn:=xlValues)
Wend
Cells(iRow, iCol + 2) = Cells(iTmp, iCol)
Next iCol
End Sub
Bei mir wird der Code nur einmal ausgeführt. Ziel ist aber damit große Mengen von Wertepaaren zu erstellen. Die Kombination sollen quasi eine Monte Carlo Simulation darstellen mit ca. 1000 x 1400 Wertepaare.
Ich habe versucht die Mehtode einfach mehrfach aufzurufen, aber das hat leider nicht geklappt. Auch mit For-Schleifen im Code kam ich nicht zum Ziel. Bin für jeden Tipp dankbar.
|