Logo Hardware.com.br
Sielxm3d
Sielxm3d Novo Membro Registrado
3 Mensagens 0 Curtidas

Números Aleatórios no Excel sem repetição com erro.

#1 Por Sielxm3d 25/02/2016 - 01:17
Olá pessoal, estou tentando criar uma planilha que gera 38 grupos de 6 números aleatórios,
cada grupo em uma linha separada, quero que não haja repetição nas 6 dezenas, no entanto
até onde cheguei é que consigo fazer os números aleatórios porém quando vai escrever na planilha
não sei porque motivo se repete alguns, o que não quero, na hora de pular para a próxima célula se repete um anterior já processado.

Os números são pegados aleatoriamente de 1 a 6 pela posição de um array, criei uma espécie de índice que vai de
1 a 16 com os valores possíveis que quero colocar nas linhas que o código vai gerar, esses valores se encontram nas células C5:R5. O array torna impossível a repetição, lembrando que
a repetição que ocorre é por um descuido de erro meu em algum lugar do código, para você ter ciência disso basta discomentar
o código da caixa de mensagem localizado no código Randomize() logo após "Loop".

Vejam o código:

Sub Randomize()
Dim intRandom As Integer
Dim intArrayPos As Integer
ReDim Array1(0 To 5)
intArrayPos = 0
Do Until intArrayPos > UBound(Array1)
intRandom = Int(16 * Rnd) + 1
On Error Resume Next
If Not CBool((WorksheetFunction.Match(intRandom, Array1, 0))) Then
Array1(intArrayPos) = intRandom
intArrayPos = intArrayPos + 1
End If
On Error GoTo 0
For I = 1 To 6
If I = 1 Then
If Array1(0) = 1 Then
ActiveCell = (Range("C5"))
ElseIf Array1(0) = 2 Then
ActiveCell = (Range("D5"))
ElseIf Array1(0) = 3 Then
ActiveCell = (Range("E5"))
ElseIf Array1(0) = 4 Then
ActiveCell = (Range("F5"))
ElseIf Array1(0) = 5 Then
ActiveCell = (Range("G5"))
ElseIf Array1(0) = 6 Then
ActiveCell = (Range("H5"))
ElseIf Array1(0) = 7 Then
ActiveCell = (Range("I5"))
ElseIf Array1(0) = 8 Then
ActiveCell = (Range("J5"))
ElseIf Array1(0) = 9 Then
ActiveCell = (Range("K5"))
ElseIf Array1(0) = 10 Then
ActiveCell = (Range("L5"))
ElseIf Array1(0) = 11 Then
ActiveCell = (Range("M5"))
ElseIf Array1(0) = 12 Then
ActiveCell = (Range("N5"))
ElseIf Array1(0) = 13 Then
ActiveCell = (Range("O5"))
ElseIf Array1(0) = 14 Then
ActiveCell = (Range("P5"))
ElseIf Array1(0) = 15 Then
ActiveCell = (Range("Q5"))
ElseIf Array1(0) = 16 Then
ActiveCell = (Range("R5"))
End If
ElseIf I = 2 Then
'Para o valor da segunda célula
If Array1(1) = 1 Then
ActiveCell = (Range("C5"))
ElseIf Array1(1) = 2 Then
ActiveCell = (Range("D5"))
ElseIf Array1(1) = 3 Then
ActiveCell = (Range("E5"))
ElseIf Array1(1) = 4 Then
ActiveCell = (Range("F5"))
ElseIf Array1(1) = 5 Then
ActiveCell = (Range("G5"))
ElseIf Array1(1) = 6 Then
ActiveCell = (Range("H5"))
ElseIf Array1(1) = 7 Then
ActiveCell = (Range("I5"))
ElseIf Array1(1) = 8 Then
ActiveCell = (Range("J5"))
ElseIf Array1(1) = 9 Then
ActiveCell = (Range("K5"))
ElseIf Array1(1) = 10 Then
ActiveCell = (Range("L5"))
ElseIf Array1(1) = 11 Then
ActiveCell = (Range("M5"))
ElseIf Array1(1) = 12 Then
ActiveCell = (Range("N5"))
ElseIf Array1(1) = 13 Then
ActiveCell = (Range("O5"))
ElseIf Array1(1) = 14 Then
ActiveCell = (Range("P5"))
ElseIf Array1(1) = 15 Then
ActiveCell = (Range("Q5"))
ElseIf Array1(1) = 16 Then
ActiveCell = (Range("R5"))
End If
ElseIf I = 3 Then
'Para o valor da terceira célula
If Array1(2) = 1 Then
ActiveCell = (Range("C5"))
ElseIf Array1(2) = 2 Then
ActiveCell = (Range("D5"))
ElseIf Array1(2) = 3 Then
ActiveCell = (Range("E5"))
ElseIf Array1(2) = 4 Then
ActiveCell = (Range("F5"))
ElseIf Array1(2) = 5 Then
ActiveCell = (Range("G5"))
ElseIf Array1(2) = 6 Then
ActiveCell = (Range("H5"))
ElseIf Array1(2) = 7 Then
ActiveCell = (Range("I5"))
ElseIf Array1(2) = 8 Then
ActiveCell = (Range("J5"))
ElseIf Array1(2) = 9 Then
ActiveCell = (Range("K5"))
ElseIf Array1(2) = 10 Then
ActiveCell = (Range("L5"))
ElseIf Array1(2) = 11 Then
ActiveCell = (Range("M5"))
ElseIf Array1(2) = 12 Then
ActiveCell = (Range("N5"))
ElseIf Array1(2) = 13 Then
ActiveCell = (Range("O5"))
ElseIf Array1(2) = 14 Then
ActiveCell = (Range("P5"))
ElseIf Array1(2) = 15 Then
ActiveCell = (Range("Q5"))
ElseIf Array1(2) = 16 Then
ActiveCell = (Range("R5"))
End If
ElseIf I = 4 Then
'Para o valor da quarta célula
If Array1(3) = 1 Then
ActiveCell = (Range("C5"))
ElseIf Array1(3) = 2 Then
ActiveCell = (Range("D5"))
ElseIf Array1(3) = 3 Then
ActiveCell = (Range("E5"))
ElseIf Array1(3) = 4 Then
ActiveCell = (Range("F5"))
ElseIf Array1(3) = 5 Then
ActiveCell = (Range("G5"))
ElseIf Array1(3) = 6 Then
ActiveCell = (Range("H5"))
ElseIf Array1(3) = 7 Then
ActiveCell = (Range("I5"))
ElseIf Array1(3) = 8 Then
ActiveCell = (Range("J5"))
ElseIf Array1(3) = 9 Then
ActiveCell = (Range("K5"))
ElseIf Array1(3) = 10 Then
ActiveCell = (Range("L5"))
ElseIf Array1(3) = 11 Then
ActiveCell = (Range("M5"))
ElseIf Array1(3) = 12 Then
ActiveCell = (Range("N5"))
ElseIf Array1(3) = 13 Then
ActiveCell = (Range("O5"))
ElseIf Array1(3) = 14 Then
ActiveCell = (Range("P5"))
ElseIf Array1(3) = 15 Then
ActiveCell = (Range("Q5"))
ElseIf Array1(3) = 16 Then
ActiveCell = (Range("R5"))
End If
ElseIf I = 5 Then
'Valor para a quinta célula
If Array1(4) = 1 Then
ActiveCell = (Range("C5"))
ElseIf Array1(4) = 2 Then
ActiveCell = (Range("D5"))
ElseIf Array1(4) = 3 Then
ActiveCell = (Range("E5"))
ElseIf Array1(4) = 4 Then
ActiveCell = (Range("F5"))
ElseIf Array1(4) = 5 Then
ActiveCell = (Range("G5"))
ElseIf Array1(4) = 6 Then
ActiveCell = (Range("H5"))
ElseIf Array1(4) = 7 Then
ActiveCell = (Range("I5"))
ElseIf Array1(4) = 8 Then
ActiveCell = (Range("J5"))
ElseIf Array1(4) = 9 Then
ActiveCell = (Range("K5"))
ElseIf Array1(4) = 10 Then
ActiveCell = (Range("L5"))
ElseIf Array1(4) = 11 Then
ActiveCell = (Range("M5"))
ElseIf Array1(4) = 12 Then
ActiveCell = (Range("N5"))
ElseIf Array1(4) = 13 Then
ActiveCell = (Range("O5"))
ElseIf Array1(4) = 14 Then
ActiveCell = (Range("P5"))
ElseIf Array1(4) = 15 Then
ActiveCell = (Range("Q5"))
ElseIf Array1(4) = 16 Then
ActiveCell = (Range("R5"))
End If
ElseIf I = 6 Then
If Array1(5) = 1 Then
ActiveCell = (Range("C5"))
ElseIf Array1(5) = 2 Then
ActiveCell = (Range("D5"))
ElseIf Array1(5) = 3 Then
ActiveCell = (Range("E5"))
ElseIf Array1(5) = 4 Then
ActiveCell = (Range("F5"))
ElseIf Array1(5) = 5 Then
ActiveCell = (Range("G5"))
ElseIf Array1(5) = 6 Then
ActiveCell = (Range("H5"))
ElseIf Array1(5) = 7 Then
ActiveCell = (Range("I5"))
ElseIf Array1(5) = 8 Then
ActiveCell = (Range("J5"))
ElseIf Array1(5) = 9 Then
ActiveCell = (Range("K5"))
ElseIf Array1(5) = 10 Then
ActiveCell = (Range("L5"))
ElseIf Array1(5) = 11 Then
ActiveCell = (Range("M5"))
ElseIf Array1(5) = 12 Then
ActiveCell = (Range("N5"))
ElseIf Array1(5) = 13 Then
ActiveCell = (Range("O5"))
ElseIf Array1(5) = 14 Then
ActiveCell = (Range("P5"))
ElseIf Array1(5) = 15 Then
ActiveCell = (Range("Q5"))
ElseIf Array1(5) = 16 Then
ActiveCell = (Range("R5"))
End If

End If
Next
ActiveCell.Offset(0, 1).Select
Loop
'MsgBox (Array1(0) & ";" & Array1(1) & ";" & Array1(2) & ";" & Array1(3) & ";" & Array1(4) & ";" & Array1(5))
End Sub
Sub ProximaLinha()
Dim Linhas As Integer
Linhas = 1
Range("C8").Select
For Linhas = 1 To 38
Randomize
ActiveCell.Offset(1, -6).Select
Next
End Sub
.

Anexos

osvaldomp
osvaldomp Geek Registrado
753 Mensagens 558 Curtidas
#2 Por osvaldomp
25/02/2016 - 09:47
Sielxm3d disse:
Olá pessoal, estou tentando criar uma planilha que gera 38 grupos de 6 números aleatórios, cada grupo em uma linha separada, quero que não haja repetição nas 6 dezenas,


Veja se este código te ajuda.
O código irá colocar o resultado no intervalo 'C7:H44'.
Sub GeraDezenas()
Dim i As Integer, ii As Integer
Randomize
For ii = 7 To 44
For i = 3 To 8
Cells(ii, i) = Cells(5, Int((16) * Rnd + 3))
Do Until Application.CountIf _
(Range(Cells(ii, 3), Cells(ii, i)), Cells(ii, i)) = 1
Cells(ii, i) = Cells(5, Int((16) * Rnd + 3))
Loop
Next i
Next ii
End Sub
Osvaldo
© 1999-2024 Hardware.com.br. Todos os direitos reservados.
Imagem do Modal