济南华福国际公寓:vb与Excel~~~~~

来源:百度文库 编辑:神马品牌网 时间:2024/04/30 04:02:25
在vb程序中操作EXCEL表常用命令有哪些~~~?

6月5日 22:27 1,新建工程,点菜单“工程\引用——Microsoft Excel xx Object Library” 'xx为您的Office版本号。
2,点菜单“工程\部件——Microsoft Windwos common Controls 6.0"
3, 在窗体中放入text1、Command1、Label1、ProgressBar1
4,将下列代码copy到代码窗体中,并将DataFile.xls改为你要处理的文件名。
5,将要处理的文件copy到程序所在的文件夹中,
6,运行:
Option Explicit

Private Sub Command1_Click()
If Command1.Caption = "开始" Then
Dim j As Integer
Dim i As Integer
Dim n As Long '定义变量,计数
Dim mTotal As Long '定义变量,总数
Dim mAvg As Single '定义变量,平均数
Dim mData(365, 24) As Single '定义数组365行,24列

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False '隐藏Excel应用程序
Dim xlsheet As Excel.Worksheet
Dim xlBook As Excel.Workbook

Set xlBook = xlApp.Workbooks.Open(App.Path & "\DataFile.xls") '打开要处理数据的工作簿
Set xlsheet = xlBook.Worksheets(1) '假定数据放在Sheet1中
ProgressBar1.Min = 0
ProgressBar1.Max = 8760
For i = 1 To 365
For j = 1 To 24
If xlsheet.Cells(i, j) <> 0 Then '如果单元格为空,不计入总数
mData(i, j) = xlsheet.Cells(i, j) '将Excel表中365行24列的数据赋值给mData()数组
mTotal = mTotal + mData(i, j)
n = n + 1 '统计共有数据的个数
End If
ProgressBar1.Value = n
Label1.Caption = "已处理数据: " & n
Form1.Caption = "正在处理数据,请稍等......"
Next j
Next i

mAvg = mTotal / n

Form1.Caption = "处理完毕!"

Text1.Text = "共有" & n & "个数据,平均值为: " & mAvg

Set xlsheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Command1.Caption = "结束"
ElseIf Command1.Caption = "结束" Then
End
End If
End Sub

Private Sub Form_Load()
Text1.Text = ""
Label1.Caption = ""
Command1.Caption = "开始"
End Sub

附件:调用Excel数据.rar