Private Sub CommandButton1_Click()
'Dichiaro le variabili
Dim StringheTotali As Integer, NumerixStringa As Integer, NCambiamenti As Integer, DistanzaHamming As Integer
Dim StringaRiferimento As Integer
Dim X As Integer, Y As Integer, Z As Integer, DadoRighe As Integer, DadoColonne As Integer
Dim QntaCambiamenti As Integer
Dim OperatoreArray()
Dim CelleCasuale As Range
Dim Contatoreloop As Integer
Dim PrimaCella As Range
Dim SecondaCella As Range
'LT chiedo conferma
If MsgBox("Cancello i Loop precedenti?", vbQuestion + vbYesNo, "CANCELLO?") = vbNo Then Exit Sub
'LT cancello loop precedenti
If Range("D21") <> "" Then
Range(Range("AA21"), Range("A100").End(xlDown).Offset(0, 3).End(xlUp).Offset(0, -2)).Clear
End If
'LT inizializzo prima cella
Set PrimaCella = Range("C6")
Set SecondaCella = PrimaCella
'Imposto dei riferimenti nel caso si aggiungessero stringhe o numeri alle stringhe
StringaRiferimento = Range("B3")
StringheTotali = Range("C7").End(xlDown).Row - 6
NumerixStringa = Range("D6").End(xlToRight).Column - 4
NCambiamenti = Range("M3")
'imposto dei check non esaustivi per controllare almeno che i campi "select row" e "n. cambiamenti loop"
'non siano lasciati vuoti.
If NCambiamenti = 0 Then MsgBox ("Hai dimenticato di inserire il numero di cambiamenti"), vbCritical + vbOKOnly, "Check N. Cambiamenti": Exit Sub
If StringaRiferimento = 0 Then MsgBox ("Hai dimenticato di inserire la stringa di riferimento"), vbCritical + vbOKOnly, "Check Riga riferimento": Exit Sub
Contatoreloop = 0
'Verifico per ogni stringa il minore tra la distanza di hamming e il n. di cambiamenti richiesto per poi caricarli in un array
rielabora:
'LT copia Valori
Range(PrimaCella.Offset(0, 1), PrimaCella.Offset(10, 21)).Copy
'LT Seleziona dove incollarli con passo 10+5
Set SecondaCella = SecondaCella.Offset(15, 0)
'LT Incolla
ActiveSheet.Paste Destination:=SecondaCella.Offset(0, 1)
Application.CutCopyMode = False
'LT scrive il numero di loop
SecondaCella = "Loop " & Format(Contatoreloop, "00")
For X = 1 To StringheTotali
ReDim Preserve OperatoreArray(StringheTotali)
If cells(6 + X, 14) > NCambiamenti Then
OperatoreArray(X) = NCambiamenti
Else
OperatoreArray(X) = cells(6 + X, 14)
End If
Next
For Z = 1 To StringheTotali
For Y = 1 To OperatoreArray(Z)
If Y <> StringaRiferimento Then
DadoColonne = Application.WorksheetFunction.RandBetween(1, NumerixStringa)
Set CelleCasuale = cells(6 + Z, 3 + DadoColonne)
CelleCasuale.Select
cells(3, 1) = DadoColonne
cells(4, 1) = Z
Call ControlloDoppioni
If CelleCasuale <> cells(6 + StringaRiferimento, 3 + DadoColonne) Then
CelleCasuale = cells(6 + StringaRiferimento, 3 + DadoColonne)
CelleCasuale.Select
Else
'Niente
End If
Else
'Niente
End If
Next
Dim k As Integer
Dim LoopCol As Integer
Dim LoopRig As Integer
k = cells(3, 2)
For LoopCol = 1 To 10
For LoopRig = 1 To 10
If LoopRig <> k Then
If cells(LoopRig + 6, LoopCol + 3) = cells(k + 6, LoopCol + 3) Then
cells(LoopRig + 6, LoopCol + 14) = 0
ElseIf cells(LoopRig + 6, LoopCol + 3) <> cells(k + 6, LoopCol + 3) Then
cells(LoopRig + 6, LoopCol + 14) = 1
End If
ElseIf LoopRig = k Then
End If
Next LoopRig
Next LoopCol
Next
Contatoreloop = Contatoreloop + 1
'imposto limite di Loop Tot
If Contatoreloop = cells(3, 9) Then GoTo terminaprocesso
If Application.WorksheetFunction.Sum(Range("N7:N16")) > 1 Then 'da qui si pu impostare il numero minimo di stringhe con Dist. Hamming =0, per bloccare i loop
GoTo rielabora
Else
terminaprocesso:
Range("K3") = Contatoreloop
MsgBox ("FINITO !")
End If
Set CelleCasuale = Nothing
Erase OperatoreArray
End Sub |