Private Sub producePlan() Call killEXCEL() @#调用杀死EXCEL进程过程
@#以下代码判断用户填写信息是否完整,如不完整则提示信息并退出. If chkFormal.Checked = False And chkSubjoin.Checked = False Then MsgBox("是正式计划还是增补计划??", MsgBoxStyle.Critical, "请先选择计划性质") Exit Sub End If If txtDay.Text = "2004年月" Then MsgBox("什么月份的生产计划???", MsgBoxStyle.Critical, "请填写计划时间") Exit Sub End If If txt703.Text = Nothing Or txt909.Text = Nothing Or txt931.Text = Nothing Or txt932.Text = Nothing Then MsgBox("请填写计划台数!", MsgBoxStyle.Critical, "计划台数填写不全") Exit Sub End If If chkFormal.Checked = True And chkSubjoin.Checked = True Then MsgBox("正式和增补两者只能选一!", MsgBoxStyle.Critical, "请重新选择计划性质") Exit Sub End If
@#以下代码即是计算各自制件的数量,用中文来命名是免去代码注释 Dim 涂氟龙面板703 As Integer = CType(txt703.Text, Integer) Dim 钛金面板909 As Integer = CType(txt909.Text, Integer) Dim 油磨不锈钢面板931 As Integer = CType(txt931.Text, Integer) Dim 油磨不锈钢面板932 As Integer = CType(txt932.Text, Integer) Dim 底盘24 As Integer = 涂氟龙面板703 Dim 底盘22 As Integer = 钛金面板909 Dim 底盘41A As Integer = 油磨不锈钢面板931 Dim 底盘41B As Integer = 油磨不锈钢面板931 Dim 水盘25 As Integer = 涂氟龙面板703 Dim 水盘24 As Integer = 涂氟龙面板703 Dim 水盘22 As Integer = 钛金面板909 * 2 Dim 中心支架2 As Integer = 涂氟龙面板703 + 钛金面板909 Dim 长支架931 As Integer = (油磨不锈钢面板931 + 油磨不锈钢面板932) * 2 Dim 支架931U As Integer = 油磨不锈钢面板931 * 2 Dim 支架932U As Integer = 油磨不锈钢面板932 * 2 Dim 磁头抱攀 As Integer = (钛金面板909 + 油磨不锈钢面板931 + 油磨不锈钢面板932) * 2 Dim 电池抱攀 As Integer = (涂氟龙面板703 + 钛金面板909 + 油磨不锈钢面板931 + 油磨不锈钢面板932) * 2 Dim 三通抱攀 As Integer = 电池抱攀 / 2 Dim 炉头垫片 As Integer = 电池抱攀 * 3
@#定义一个数组,方便在EXCEL中循环写入数字,也可以放在EXCEL的VBA中实现 Dim allNum() As Integer = _ {涂氟龙面板703, 钛金面板909, 油磨不锈钢面板931, 油磨不锈钢面板932, _ 底盘24, 底盘22, 底盘41A, 底盘41B, _ 水盘25, 水盘24, 水盘22, _ 中心支架2, 长支架931, 支架931U, 支架932U, _ 磁头抱攀, 电池抱攀, 三通抱攀, 炉头垫片}
Dim excelApp As New Excel.Application Dim excelBook As Excel.Workbook @#自制件生产计划.xls Dim excelbook2004 As Excel.Workbook @#2004自制件生产计划.xls Dim excelWorksheet As Excel.Worksheet Dim planProperty As String @#计划性质,是正式计划还是增补计划
Try @#建议用try方式捕捉错误,处理错误
excelBook = excelApp.Workbooks.Open(Application.StartupPath & "\自制件生产计划.xls")
excelbook2004 = excelApp.Workbooks.Open(Application.StartupPath & "\2004年自制件生产计划.xls") excelWorksheet = CType(excelBook.Worksheets("样表"), Excel.Worksheet) excelWorksheet.Copy(After:=excelbook2004.Sheets("sheet1")) @#把样表copy到<2004年自制件生产计划>workbook中sheet1的后面
excelApp.Visible = True @#设置工作薄为可视
If chkFormal.Checked = True Then planProperty = "正式" ElseIf chkSubjoin.Checked = True Then planProperty = "增补" End If
With excelbook2004.ActiveSheet @#用with 简化代码 .Range("D1").Value = txtDay.Text @#计划时间 .Range("C2").Value = "laoban公司" & txtDay.Text & planProperty & "采购计划" @#计划依据 .Range("C25").Value = Now.Date.Today.ToShortDateString @#这就是制表日期 .Range("F2").Value = txtNO.Text @#计划编号
End With For i As Integer = 0 To 18 @#共19种自制件 excelbook2004.ActiveSheet.cells(4 + i, 4) = allNum(i) @#4+i是行号,第二个4是列号 Next @#循环把各自制件数填入<2004年自制件生产计划>中的活动工作表相应位置
Catch ex As Exception @#捕捉错误,并回收资源,显示错误 excelBook = Nothing excelbook2004 = Nothing excelWorksheet = Nothing excelApp = Nothing GC.Collect(0) MsgBox(ex.ToString) @#显示错误信息,以查找定位 Exit Sub @#出错就退出 Finally @#这里的代码一定会被执行到 excelBook = Nothing excelbook2004 = Nothing excelWorksheet = Nothing excelApp = Nothing GC.Collect(0) End Try MsgBox("已排好自制件生产计划,请查看")
excelBook = Nothing excelbook2004 = Nothing excelWorksheet = Nothing excelApp = Nothing GC.Collect(0)
End Sub |