Logo Hardware.com.br
Otneba62
Otneba62 Cyber Highlander Registrado
16.6K Mensagens 2K Curtidas

VB - cálculo de 6 números

#1 Por Otneba62 30/04/2007 - 07:28
Fiz uma macro para calcular 6 números diferentes entre 1 e 49, conforme exemplo a seguir. Acontece que os números podem sair repetidos o que evito com uma fórmula no Excel.
Há alguma maneira de calcular os 6 números de uma só vez e não permitindo que sejam repetidos.
+++++++++++++++++++++++


Sub loto6nrs()

'Cálculo de 6 numeros

Dim MyValue
Randomize ' Initialize random-number generator.

'1º numero

MyValue = Int((49 * Rnd) + 1) ' Generate random value between 1 and 49.


Range("A2").Select
ActiveCell.FormulaR1C1 = MyValue

'++++++

'2º numero

MyValue = Int((49 * Rnd) + 1)


Range("A3").Select
ActiveCell.FormulaR1C1 = MyValue

'++++++

'3º numero

MyValue = Int((49 * Rnd) + 1)


Range("A4").Select
ActiveCell.FormulaR1C1 = MyValue


'++++++

'4º numero

MyValue = Int((49 * Rnd) + 1)


Range("A5").Select
ActiveCell.FormulaR1C1 = MyValue


'++++++

'5º numero

MyValue = Int((49 * Rnd) + 1)


Range("A6").Select
ActiveCell.FormulaR1C1 = MyValue

'++++++

'6º numero

MyValue = Int((49 * Rnd) + 1)


Range("A7").Select
ActiveCell.FormulaR1C1 = MyValue


End Sub

+++++++++
Flashcooler
Flashcooler Geek Registrado
2K Mensagens 74 Curtidas
#2 Por Flashcooler
30/04/2007 - 09:35
Otneba62 disse:
Fiz uma macro para calcular 6 números diferentes entre 1 e 49, conforme ...Range("A7").Select
ActiveCell.FormulaR1C1 = MyValue
End Sub

Você pode tentar colocar um IF, para fazer uma condição...em que se o numero que ele calcular for igual aos anteriores ele recalcula...
for i = 1 to 6
if MyValue = Int((49 * Rnd) + i) = MyValue = Int(49 * Rnd) then
MyValue = Int((49 * Rnd) + i)
else
Range("A4").Select
ActiveCell.FormulaR1C1 = MyValue
end if
next i

Algo mais ou menos assim, é só uma idéia, agora eu não consigo faze-lo pois estou no trabalho, mas de noite se tu não tiver conseguido, eu te mando o código.

t mais
Society, you are a crazy breed I Hope you are not lonely without me... mostrando_lingua.png

When you want more than you have You think you need... !!

Flashcooler
Flashcooler Geek Registrado
2K Mensagens 74 Curtidas
#5 Por Flashcooler
12/05/2007 - 00:29
Cara coloca mais essa rotina e põe ela para ser executada logo após a sua rotina de numeros, pois se ela achar algum numero repetido ela faz novamente a rotina.


Sub Igual()
Dim i As Integer
Dim j As Integer
For i = 2 To 7
For j = i + 1 To 7
If Range("A" & i).Value = Range("A" & j).Value Then
loto6nrs

CommandButton1.Activate
Else
End If
Next j
Next i
End Sub


T mais
Society, you are a crazy breed I Hope you are not lonely without me... mostrando_lingua.png

When you want more than you have You think you need... !!

© 1999-2024 Hardware.com.br. Todos os direitos reservados.
Imagem do Modal