在“工程”的“引用”中选择“microsoft excel 11.0”,"microsoft excel 11.0"看版本而定
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Private Sub Command1_Click()
On Error Resume Next
'后台进程运行excel程序,并得到该工作簿
Set xlBook = xlApp.Workbooks.Open("f:/1.xls")
xlApp.Visible = False
'获得该工作簿的“sheet1”表
Set xlSheet = xlBook.Sheets("sheet1")
xlSheet.Select
'抽取b2格子的数据并赋值给text1.text
text1.Text = xlSheet.Range("b2")
'释放资源,顺序到过来的,表》》簿》》应用程序
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
本文介绍了一种使用VBA从指定路径打开Excel文件的方法,通过后台运行Excel程序并隐藏界面,然后选择特定的工作表(sheet1),从B2单元格读取数据并将其赋值给text1.text,最后按顺序释放资源。
2800

被折叠的 条评论
为什么被折叠?



