0

It has been a while since I've coded in Visual Basic, so I forgot much of what I knew when working with MS Excel. I am actually coding within the developer tab of MS Excel 2007.

I have an existing workbook that contains a sheet called "MySheet". In this sheet is a range of cells with text values, and the cells rangefrom A1:A10. I would like to click a button and select the text from any random cell within this range. The text would then be displayed in a message box. Here is what I have so far. This definitely doesn't work though. Any help please? Thanks!

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
4

1 回答 1

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)
于 2012-11-29T21:35:05.813 回答