【问题标题】:Visual Basic - Select Random Excel Sheet and Random CellVisual Basic - 选择随机 Excel 工作表和随机单元格
【发布时间】:2012-11-29 21:07:43
【问题描述】:

我已经有一段时间没有使用 Visual Basic 进行编码了,所以我忘记了在使用 MS Excel 时所知道的大部分内容。我实际上是在 MS Excel 2007 的开发人员选项卡中进行编码。

我有一个现有的工作簿,其中包含一个名为“MySheet”的工作表。在此工作表中是具有文本值的单元格区域,单元格范围为 A1:A10。我想单击一个按钮并从该范围内的任何随机单元格中选择文本。然后文本将显示在消息框中。这是我到目前为止所拥有的。但这绝对行不通。请问有什么帮助吗?谢谢!

Private Sub myButton_Click()
Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
Dim myCell As Range
Dim rndText As String
Dim rndIndex as Integer

rndIndex = **random number...not sure how**
rndText = ""

xlsheet = xl.Workbook.Sheets("MySheet")
myCell = xlsheet.Cells(rndIndex, 1)
rndText = myCell.Value
MsgBox (rndText)

End Sub

【问题讨论】:

    标签: vb.net excel random cell worksheet


    【解决方案1】:

    您绝对是在正确的轨道上。要在 .NET 中获取随机数,方法如下:

    Dim rand = new Random()
    rndIndex = rand.Next()
    
    'Or you can do this and set a minimum and maximum value for the random number
    rndIndex = rand.Next(0, 100)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多