
Option Explicit
Sub Azzera_Distanza()
'Dichiaro le variabili
Dim StringheTotali As Integer, NumerixStringa As Integer, NCambiamenti As Integer, DistanzaHamming As Integer
Dim StringaRiferimento As Integer
Dim X As Integer, DadoRighe As Integer, DadoColonne As Integer
Dim CellaCasuale As Range
'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
'Imposto etichetta nel caso la generazione del numero casuale che sarà utilizzato come riferimento di riga della
'cella casuale sia uguale alla riga della stringa di riferimento (non vogliamo cambiare le celle della stringa di
'riferimento)
lanciadadorighe:
'genero il numero di riga casuale
DadoRighe = Application.WorksheetFunction.RandBetween(1, StringheTotali)
If DadoRighe = StringaRiferimento Then GoTo lanciadadorighe 'se è uguale alla riga della stringa genera ancora
'genero il numero di colonna casuale
DadoColonne = Application.WorksheetFunction.RandBetween(1, NumerixStringa)
'imposto la cella
Set CellaCasuale = Cells(6 + DadoRighe, 3 + DadoColonne)
CellaCasuale.Select 'la seleziono solo per comodità quando eseguo il codice passo passo posso vedere la cella selezionata
'Faccio diventare la cella uguale a quella di riferimento posta sulla stessa colonna.
CellaCasuale = Cells(6 + StringaRiferimento, 3 + DadoColonne)
'ricalcolo le distanze di hamming (ci servirà più tardi per tenere aggiornate le distanze e confrontarle con gli n.cambiamenti)
Call calcola_distanze
End Sub
|
Option Explicit
Option Base 1
Sub Azzera_Distanza()
'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
'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:
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)
If CelleCasuale <> Cells(6 + StringaRiferimento, 3 + DadoColonne) Then
CelleCasuale = Cells(6 + StringaRiferimento, 3 + DadoColonne)
Else
'Niente
End If
Else
'Niente
End If
Next
Call calcola_distanze_nomsg
Next
Contatoreloop = Contatoreloop + 1
If Application.WorksheetFunction.Sum(Range("N7:N16")) <> 10 Then
GoTo rielabora
Else
Range("K3") = Contatoreloop
MsgBox ("FINITO !")
End If
Set CelleCasuale = Nothing
Erase OperatoreArray
End Sub
Sub calcola_distanze_nomsg()
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
End Sub |
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 RangeStringa As Range
'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:
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
Set RangeStringa = Range(Cells(6 + Z, 4), Cells(6 + Z, 13))
If CelleCasuale <> Cells(6 + StringaRiferimento, 3 + DadoColonne) Then
valoreriferimento = Cells(6 + StringaRiferimento, 3 + DadoColonne)
Set celladoppia = RangeStringa.Find(what:=valoreriferimento)
If Not celladoppia Is Nothing Then
celladoppia.Value = CelleCasuale.Value
End If
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 per bloccare i loop
GoTo rielabora
Else
terminaprocesso:
Range("K3") = Contatoreloop
MsgBox ("FINITO !")
End If
Set CelleCasuale = Nothing
Erase OperatoreArray
End Sub
Private Sub CommandButton2_Click()
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
MsgBox ("Calcoli effettuati")
End Sub |
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
'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:
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
Public Sub ControlloDoppioni()
Dim A As Integer
For A = 4 To 13
If A <> 3 + cells(3, 1) Then 'controlla che la colonna selezionata non venga inclusa nel loop
If cells(6 + cells(4, 1), A) = cells(cells(3, 2) + 6, 3 + cells(3, 1)) Then
cells(6 + cells(4, 1), A) = cells(6 + cells(4, 1), 3 + cells(3, 1))
End If
End If
Next A
End Sub
|
