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
|