VB调用Excel简化表格处理
发表于:2007-07-14来源:作者:点击数:
标签:
---- VB程序中直接调用Excel的格式如下: Private Sub menudy_Click(Index As Integer) \这是一个打印菜单 Select Case Index Case 0 On Error GoTo kung aaa = Shell(“D:Program FilesMicrosoft OfficeOfficeEXCEL.EXE c:cnglcngl.xls, 1) Case 1 On Error
---- VB程序中直接调用Excel的格式如下:
Private Sub menudy_Click(Index As Integer)
\这是一个打印菜单
Select Case Index
Case 0
On Error GoTo kung
aaa = Shell(“D:Program FilesMicrosoft
OfficeOfficeEXCEL.EXE c:cnglcngl.xls", 1)
Case 1
On Error GoTo kong \作为空表处理
aaa = Shell(“D:Program FilesMicrosoft
OfficeOfficeEXCEL.EXE c:cnglcngly.xls", 1)
Case 3
Case 4
Case 5
End Select
Exit Sub
kung:
aaa = Shell(“EXCEL.EXE c:cnglcngl.xls", 1)
Exit Sub
kong:
aaa = Shell(“EXCEL.EXE c:cnglcngly.xls", 1)
End Sub
----由于Word97和Excel97等高版本的Office组件都有和VB 的接口:VB 编辑器,所以我们可以直接利用 VB 编辑器编写我们需要的程序。步骤如下:进入Excel97后,用鼠标点击菜单栏中的“工具”按钮,选择“宏”,再选择“VB 编辑器”。
----“VB编辑器”的编辑界面非常类似于“Visual Basic"的界面。我们可以从它的控件“工程资源管理器”中插入需要的“添加用户窗体、添加模块、添加类模块”,并且从“属性窗口”中加入属性。
----例如,我们可以制定一个打印窗体,通过程序来实现数据定位和循环调用打印过程。
Public panduan As Boolean
Private Sub CommandButton1_Click()
change
If panduan Then
Unload Me
Else
TextBox1.Text = “"
End If
End Sub
Sub change()
If Not judgeday(TextBox1.Text) Then GoTo error
panduan = True
Dim sql As String
Dim db As Database
Dim rs As Recordset
sql = “SELECT * From 数据表"
sql = sql + “ WHERE (((数据表.日期)=
#" + TextBox1.Text + “#))"
Setdb =OpenDatabase(Application.ThisWorkbook.Path
+“cngl.mdb")
Set rs = db.OpenRecordset(sql, dbOpenDynaset)
If rs.EOF Then
MsgBox (“此日期无数据")
Exit Sub
End If
daima1 = rs.Fields(“代码")
Sheet1.Range(“e5").Value = rs.Fields(“日期")
Sheet1.Range(“f7").Value = rs.Fields(“数据表记录")
Sheet1.Range(“d13").Value = rs.Fields(“整数100")
Sheet1.Range(“d15").Value = rs.Fields(“整数50")
Sheet1.Range(“d17").Value = rs.Fields(“整数10")
Sheet1.Range(“d19").Value = rs.Fields(“整数5")
Sheet1.Range(“d21").Value = rs.Fields(“整数2")
Sheet1.Range(“d23").Value = rs.Fields(“整数1")
Sheet1.Range(“h13").Value = rs.Fields(“其他100")
Sheet1.Range(“h15").Value = rs.Fields(“其他50")
Sheet1.Range(“h17").Value = rs.Fields(“其他10")
Sheet1.Range(“h19").Value = rs.Fields(“其他5")
Sheet1.Range(“h21").Value = rs.Fields(“其他2")
Sheet1.Range(“h23").Value = rs.Fields(“其他1")
Sheet1.Range(“d37").Value = Sheet1.Range
(“d13").Value * 100 + Sheet1.Range(“d15").
Value * 50 + Sheet1.Range(“d17").Value * 10 +
Sheet1.Range(“d19").Value * 5 + Sheet1.Range
(“d21").Value * 2 + Sheet1.Range(“d23").Value
Sheet1.Range(“h37").Value = Sheet1.Range
(“h13").Value * 100 + Sheet1.Range(“h15").
Value * 50 + Sheet1.Range(“h17").Value * 10 +
Sheet1.Range(“h19").Value * 5 + Sheet1.Range
(“h21").Value * 2 + Sheet1.Range(“h23").Value
Dim sql1 As String
Dim db1 As Database
Dim rs1 As Recordset
sql1 = “SELECT * From 代码字典"
sql1 = sql1 + “WHERE (((代码字典.代码)=
" & daima1 & “))"
Setdb1 =OpenDatabase(Application.ThisWorkbook.Path
+ “cngl.mdb")
Set rs1 = db1.OpenRecordset(sql1, dbOpenDynaset)
Sheet1.Range(“h41").Value = rs1.Fields(“代码字典名称")
Exit Sub
error:
MsgBox (“日期输入错误")
panduan = False
End Sub
Private Sub TextBox1_Change()
End Sub
Private Sub UserForm_Activate()
dyxjkc.Top = 30
dyxjkc.Left = 230
End Sub
----具体的表格画线可以由Excel自动完成,因此简化了VB 编程的步骤,并且节约调试程序和画线定位的时间。
原文转自:http://www.ltesting.net