Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
#58192
Boa tarde pessoal,

Estou a tentar fazer uma macro para fazer um sorteio sem números repetidos num intervalo, pesquisei e encontrei uma mas não estou conseguindo meter a funcionar, podem me ajudar malta :D
Em anexo fica a minha panilha com a macro que encontrei na net :D
Obrigado ;)
Você não está autorizado a ver ou baixar esse anexo.
#58200
Experimente:
Código: Selecionar todos
Sub GeraNúmeros()
 Dim i As Integer
  Application.ScreenUpdating = False
  Range(Cells(6, 3), Cells(6, 3).End(2)) = ""
  Randomize
  For i = 1 To [C4]
   Cells(6, i + 2) = Int(([E2] * Rnd) + [C2])
    Do Until Application.CountIf _
     (Range(Cells(6, 3), Cells(6, i + 2)), Cells(6, i + 2)) = 1
     Cells(6, i + 2) = Int(([E2] * Rnd) + [C2])
    Loop
  Next i
  Cells(6, 3).Resize(, [C4]).Sort Key1:=Cells(6, 3), Order1:=xlAscending, Orientation:=xlLeftToRight
End Sub
obs. o resultado será inserido em linha, a partir de C6; antes de testar desfaça a mesclagem em C6:E11
#58231
ricardosilvercup escreveu:... mas repete números ...
A qual macro você está se referindo?

O código que passei gera uma série de números sem repetição.
#58234
Utilize o código abaixo.
Código: Selecionar todos
Sub GeraNúmerosV2()
  If Application.CountA(Range("6:6")) >= [E2] - [C2] + 1 Then Exit Sub
  Randomize
   Cells(6, Columns.Count).End(1)(1, 2) = Int(([E2] * Rnd) + [C2])
    Do Until Application.CountIf _
     (Range(Cells(6, 1), Cells(6, Columns.Count).End(1)), Cells(6, Columns.Count).End(1)) = 1
     Cells(6, Columns.Count).End(1) = Int(([E2] * Rnd) + [C2])
    Loop
End Sub
obs.
1. preencha C2 e E2 conforme desejado
2. se você quer sortear um número de cada vez então não faz sentido a existência do campo "quantidade de numeros a sortear" que você colocou na sua planilha
3. o resultado será colocado em linha a partir de B6; antes de testar desfaça a mesclagem em C6:E11
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord