vba 在 a 到 b 之间生成一个随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41949682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:42:41  来源:igfitidea点击:

Generate a random number between a to b

excelvbabuttonrandom

提问by Austin Howard

I have knowledge of the RANDBETWEEN() function inside the cells of Excel. This only returns one (1) value. I would like to assign this to a button to return a different value every time the button is hit.

我了解 Excel 单元格内的 RANDBETWEEN() 函数。这仅返回一 (1) 个值。我想将它分配给一个按钮,以便在每次点击按钮时返回不同的值。

I can create the button, I have the cell. How can I link the two?

我可以创建按钮,我有单元格。我怎样才能把两者联系起来?

Long story short, every time a button is clicked a cell (lets say cell a1) gets a different value between values a to b.

长话短说,每次单击按钮时,单元格(假设单元格 a1)都会在值 a 到 b 之间获得不同的值。

采纳答案by Michael

If you're still using the RANDBETWEEN function and you just want it to generate a new value on command, just press F9 to force Excel to recalculate all formulas. RANDBETWEEN will then generate a new random number. (If it doesn't change value, it's just because it generated the same random number by chance. Keep pressing F9 to get new random numbers.)

如果您仍在使用 RANDBETWEEN 函数并且您只是希望它根据命令生成一个新值,只需按 F9 强制 Excel 重新计算所有公式。RANDBETWEEN 然后将生成一个新的随机数。(如果它没有改变值,那只是因为它偶然产生了相同的随机数。继续按F9以获得新的随机数。)

Note that changing any formula on the spreadsheet will also cause the RANDBETWEEN function to generate a new value.

请注意,更改电子表格上的任何公式也会导致 RANDBETWEEN 函数生成新值。

If you want a new random number to be generated ONLY when you command it to, then you do need to create a macro, which can be easily called through a button. I'm assuming you're not familiar with macros at all, so the following is a simple solution.

如果您只想在您发出命令时生成一个新的随机数,那么您确实需要创建一个宏,该宏可以通过按钮轻松调用。我假设您根本不熟悉宏,因此以下是一个简单的解决方案。

The following website explains how to create a simple button that will run a macro. (Note that this specific method only works with the ActiveX button, not the Forms button.)

以下网站解释了如何创建一个将运行宏的简单按钮。(请注意,此特定方法仅适用于 ActiveX 按钮,不适用于 Forms 按钮。)

https://www.tutorialspoint.com/vba/vba_excel_macros.htm

https://www.tutorialspoint.com/vba/vba_excel_macros.htm

You will just need to replace the code in their example (MsgBox "Hi") with the following code:

您只需要使用以下代码替换他们示例中的代码 (MsgBox "Hi"):

Range("A1") = WorksheetFunction.RandBetween(a,b)

Replace a and b with the numbers you want to get a random number between. You can do this a few ways:

将 a 和 b 替换为您想要在两者之间获得随机数的数字。您可以通过以下几种方式做到这一点:

If you always want it between the same two numbers (e.g. 1 and 10), you can hard-code them into the macro:

如果您总是希望在相同的两个数字(例如 1 和 10)之间使用它,您可以将它们硬编码到宏中:

Range("A1") = WorksheetFunction.RandBetween(1,10)

If you want it between two numbers recorded on the same spreadsheet (e.g. in cells B1 and B2), you can refer to the cells:

如果您希望在同一电子表格中记录的两个数字之间(例如在单元格 B1 和 B2 中),您可以参考单元格:

Range("A1") = WorksheetFunction.RandBetween(Range("B1"),Range("B2"))

Or you can ask for the numbers in popups:

或者您可以在弹出窗口中询问数字:

a = InputBox("Enter a")
b = InputBox("Enter b")
Range("A1") = WorksheetFunction.RandBetween(a, b)

Note that the website misses a couple of important steps you must perform before the button will be able to used:

请注意,该网站遗漏了您必须执行的几个重要步骤,才能使用该按钮:

  1. Close the VBA window (not actually necessary, but there's no need to leave it open)

  2. Exit Design Mode. On the Developer tab in Excel, the Design Mode button should still be highlighted. Click it to un-highlight it. You can now click your button.

  1. 关闭 VBA 窗口(实际上不是必需的,但没有必要让它保持打开状态)

  2. 退出设计模式。在 Excel 的“开发工具”选项卡上,“设计模式”按钮仍应突出显示。单击它以取消突出显示它。您现在可以单击您的按钮。

Note that if you want to change the code for the button again, you need to click Design Mode again to highlight it again, then you can double click the button to view the code again.

注意,如果要再次修改按钮的代码,需要再次点击Design Mode再次高亮,然后双击按钮再次查看代码。

回答by Mukul Varshney

Try below

试试下面

Private Sub CreateRandom()

  'random number between 11 and 100
  Debug.Print NewRandom(11, 100)

End Sub

Private Function NewRandom(ByVal FromLimit As Integer, ByVal ToLimit As Integer) As Integer

    Randomize
    randomNumber = Int((ToLimit - FromLimit) * Rnd) + FromLimit
    NewRandom = randomNumber

End Function

Private Sub CommandButton1_Click()

  'random number between 30 and 100
  Sheets("Sheet2").Range("A1").Value = NewRandom(30, 100)

End Sub

回答by Maheshwaran

MI Consol:

MI控制台:

Sheets:
Private Sub CommandButton1_Click()
Call p
End Sub

Private Sub CommandButton3_Click()
Call save
End Sub

Private Sub CommandButton4_Click()
Call EMail
End Sub

Private Sub CommandButton5_Click()
Call iSMS
End Sub

Private Sub CommandButton6_Click()
Call continue
End Sub

Private Sub CommandButton7_Click()
Call Continue1
End Sub