'方法,在菜单里选择[工程] -- [引用],在窗口里勾选 Microsoft Excel XX.X Object Library
'其中,XX.X取决于你安装的Office的版本号
'在你的显示按钮中加入以下代码。
'注:"地址"中加入你的excel文件地址,如"C:\1.XLS",要带双引号
'Sheet1也可以改为你的工作表的名字,要带双引号
Dim excel_App As Excel.Application
Dim excel_Book As Excel.Workbook
Dim excel_sheet As Excel.Worksheet
Dim CarWeight
'启动Excel
Set excel_App = CreateObject("Excel.Application")'引用程序对象实例
'设置Excel为不可见
excel_App.Visible = False
'打开文件
Set excel_Book = excel_App.Workbooks.Open("地址")'工作簿实例
Set excel_sheet = excel_Book.Worksheets("Sheet1")'数据表实例
'这里需要先将你的车号存入valuesearch变量才能搜索
'如 valuesearch = Form1.TextBox1.text
'进行搜索,存入carweight变量
CarWeight = Application.WorksheetFunction.VLookup(valuesearch, Range("C:C"), 2, False)
'然后将CarWeight赋值给你的皮重文本框
'如Form1.TextBox2.Text = CarWeight
'''''''''''''''''''''''''''''''''''
'关闭Excel文件
Set excel_sheet = Nothing
Set excel_Book = Nothing
excel_App.Quit
Set excel_App = Nothing
1.部件-添加-可插入对象--选择excel 表格就可以在窗体添加该表格控件
2.工程-引用-Microsoft Excel 11.0 Object Library
下面给个简单的例子
Private Sub Command1_Click()
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim AppExcel As Object
Set xlExcel = CreateObject("Excel.Application")
xlExcel.Workbooks.Open "C:\test.xls"
Set xlBook = xlExcel.Workbooks("test.xls")
Set xlSheet = xlBook.Worksheets(1)
Text1.Text = xlSheet.Cells(1, 1).Value
Text1.Text = Text1.Text &"ADD"
xlSheet.Cells(1, 1).Value = Text1.Text
Application.DisplayAlerts = False
xlBook.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)