老师上课Vb随机点名程序,用excel读入学生名单
这个程序的重点就是读Excel表格,如果能读出来人员名单,下面就用随机数就很好写了
我测试代码
Dim ExcelApp As Object
Dim ExcelBook As Object
Dim ExcelSheet As Object
Dim n As Long
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelBook = ExcelApp.Workbooks.Open("c:\0.xls")
Set ExcelSheet = ExcelBook.Worksheets(1)
n = 1
Do
List1.AddItem ExcelSheet.Range("D" & n).Value
n = n + 1
Loop Until ExcelSheet.Range("D" & n).Value = ""
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelApp = Nothing
我的人员名称是在D列,同理,如果还想要其它列(学号),在后后加上就行了
为了操作excel,还引用了"Microsoft Excel 11.0 Object Library"对像