Excel VBA Rnd Not Actually Random

0 votes

I currently have a macro that when I click a button, it "randomly" gives me a number in a msgbox:

dim number as double
number= Int(8 * Rnd + 1) - 1
MsgBox number

The numbers aren't really random, though, which is the problem. As an illustration, let's imagine that after starting the macro and pressing the button twice, I receive the digits 5 and 2. I get the same two numbers, 5, and 2, whether I close the macro, reopen it, or click the button twice.

It's been years since I used VB.net, so I don't quite remember how to do it. I also wouldn't know how to use it in Excel VBA. However, I do know that there was a technique to truly make it spew out random numbers each time without repeating the "sequence" in VB.net.

Oct 27, 2022 in Others by Kithuzzz
• 38,000 points
632 views

1 answer to this question.

0 votes

 Initialize the random number function.

Sub test()
Dim number As Double
Randomize
number = Int(8 * Rnd + 1) - 1
MsgBox number
End Sub

I hope this solves your problem.

answered Oct 27, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Excel VBA userform paste text in expanded format (not just to one cell)

It should work if you simply supply ...READ MORE

answered Jan 21, 2023 in Others by narikkadan
• 63,600 points
637 views
0 votes
1 answer

How do I make my subs() not change inputdata in Vba excel

Use the ByVal keyword to pass on ...READ MORE

answered Feb 5, 2023 in Others by narikkadan
• 63,600 points
543 views
0 votes
1 answer

Excel VBA code not running using F5 only F8

Change your code like this: Dim LastColumn As ...READ MORE

answered Mar 21, 2023 in Others by Kithuzzz
• 38,000 points
536 views
0 votes
1 answer

Excel VBA SQL Query on not saved Workbook

What can I do to get the ...READ MORE

answered Apr 7, 2023 in Others by Kithuzzz
• 38,000 points
611 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,212 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,664 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
908 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,053 views
0 votes
1 answer

Excel VBA is not recording macro properly

The "awkward code" is an R1C1 notation ...READ MORE

answered Nov 15, 2022 in Others by narikkadan
• 63,600 points
1,355 views
0 votes
1 answer

Delimiters in Excel VBA Regex Patterns, filter something but not others

Change your code to this: Function RemoveTags(ByVal Value ...READ MORE

answered Jan 12, 2023 in Others by narikkadan
• 63,600 points
672 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP