Moderatori: Anthony47, Flash30005
Sub sort()
Dim Dest As String, K As Long, I As Long, Primo As String, Secondo As String
Dim LastB As Long
'
Dest = "F3:G6" '<< L' area in cui si formeranno le tre coppie
Range(Dest).ClearContents
LastB = Cells(Rows.Count, 2).End(xlUp).Row
For I = 1 To 3
rePr:
K = K + 1
If K > 10000 Then MsgBox ("Non risolto, abortito"): Exit For
Primo = Range("B4").Offset(Rnd() * (LastB - 4), 0)
If Application.WorksheetFunction.CountIf(Range(Dest), Primo) > 0 Then GoTo rePr
Range(Dest).Cells(1, 1).Offset(I, 0) = Primo
reSec:
K = K + 1
If K > 10000 Then MsgBox ("Non risolto, abortito"): Exit For
Secondo = Range("B4").Offset(Rnd() * (LastB - 4), 0)
If Application.WorksheetFunction.CountIf(Range(Dest), Secondo) > 0 Then GoTo reSec
If Left(Secondo, 1) = "%" And Left(Primo, 1) = "%" Then GoTo reSec
If Left(Secondo, 1) = "*" And Left(Primo, 1) = "*" Then GoTo reSec
Range(Dest).Cells(1, 1).Offset(I, 1) = Secondo
DoEvents
Next I
End Sub
Dim SorgBase As Range, DestBase As Range, LastP As String
Dim I As Long, Playrs As Long, J As Long, DLock As Long
Sub sort2()
Dim P1 As String, P2 As String, P3 As String, P4 As String
'
Set SorgBase = Sheets("ISCRITTI").Range("B4") '<<< L'area dove comincia l' elenco dei PLAYERS
Set DestBase = Sheets("SORTEGGI").Range("A4") '<<< L'area dove comincia l' elenco delle partite
Playrs = Range(SorgBase, SorgBase.End(xlDown)).Rows.Count
DLock = 0
reJ:
DestBase.Resize(Playrs, 9).ClearContents
DLock = DLock + 1
DoEvents
For J = 1 To 3
DoEvents
For I = 1 To Int(Playrs / 4)
ReP1: If DLock > 10 And DLock Mod 500 = 1 Then GoTo reJ Else If DLock > 10000 Then GoTo AbortA
P1 = SorgBase.Offset(Int(Rnd() * Playrs), 0).Value
If Not Buono(P1, 1) Then GoTo ReP1 Else DestBase.Offset(2 * (I - 1), 3 * (J - 1)).Value = P1
ReP2: If DLock > 10 And DLock Mod 500 = 1 Then GoTo reJ Else If DLock > 10000 Then GoTo AbortA
P2 = SorgBase.Offset(Int(Rnd() * Playrs), 0).Value
If Not Buono(P2, 2) Then GoTo ReP2 Else DestBase.Offset(2 * (I - 1) + 1, 3 * (J - 1)).Value = P2
ReP3: If DLock > 10 And DLock Mod 500 = 1 Then GoTo reJ Else If DLock > 10000 Then GoTo AbortA
P3 = SorgBase.Offset(Int(Rnd() * Playrs), 0).Value
If Not Buono(P3, 1) Then GoTo ReP3 Else DestBase.Offset(2 * (I - 1), 3 * (J - 1) + 1).Value = P3
ReP4: If DLock > 10 And DLock Mod 500 = 1 Then GoTo reJ Else If DLock > 10000 Then GoTo AbortA
P4 = SorgBase.Offset(Int(Rnd() * Playrs), 0).Value
If Not Buono(P4, 2) Then GoTo ReP4 Else DestBase.Offset(2 * (I - 1) + 1, 3 * (J - 1) + 1).Value = P4
Next I
Next J
MsgBox ("Completato")
Exit Sub
'
AbortA:
MsgBox ("Tentativo fallito")
End Sub
Function Buono(ByVal PLYR As String, ByVal CC As Long) As Boolean
Dim myPres As Long
'
Buono = False: DLock = DLock + 1
'If DLock > 1000 Then Stop
DoEvents
myPres = Application.WorksheetFunction.CountIf(DestBase.Offset(0, 3 * (J - 1)).Resize(Playrs / 2, 2), PLYR)
If myPres > 0 Then GoTo ExitA
If Left(PLYR, 1) = "%" And Left(LastP, 1) = "%" Then GoTo ExitA
If Left(PLYR, 1) = "*" And Left(LastP, 1) = "*" Then GoTo ExitA
If J > 1 And CC = 2 Then
If CkPair(PLYR, LastP) = False Then GoTo ExitA
End If
Buono = True: DLock = 0
If CC = 1 Then LastP = PLYR
ExitA:
'If DLock > [J1] Then [J1] = DLock
End Function
Function CkPair(ByVal PP As String, ByVal SP As String) As Boolean
Dim myI As Long, myJ As Long, myK As Long, CPP As Long, CSP As Long
CkPair = False
DoEvents
For myJ = 0 To J - 1
For myI = 0 To Int(Playrs / 4) * 2 Step 2
For myK = 0 To 1
DoEvents
CPP = Application.WorksheetFunction.CountIf(DestBase.Offset(myI, 3 * myJ).Resize(2, 1), PP)
CSP = Application.WorksheetFunction.CountIf(DestBase.Offset(myI, 3 * myJ).Resize(2, 1), SP)
If (CPP + CSP) = 2 Then GoTo ExitA
Next myK
Next myI
Next myJ
CkPair = True
ExitA:
End Function
For Each Cell In Range(SorgBase, SorgBase.End(xlDown))
If Len(Cell.Value) > 0 Then Playrs = Playrs + 1
Next Cell
Torna a Applicazioni Office Windows
sorteggi casuali evitando però certi tipi di abbinamento Autore: cesfri |
Forum: Applicazioni Office Windows Risposte: 2 |
Excel Macro spostare celle random su stessa riga Autore: btano |
Forum: Applicazioni Office Windows Risposte: 3 |
Visitano il forum: Nessuno e 18 ospiti