|
在每日计划的Sheet1工作表中添加Worksheet_Calculate事件如下:
Private Sub Worksheet_Calculate()
If js = True Then Exit Sub '当js为true时退出该事件
js = True
s.try Array("tryM"), , Array("FinnallyM") '在最后需把js设定为false,所以此处使用了try-catch-finnally函数
End Sub
再插入一个模块写入以下代码:
Public js As Boolean '这个变量用于控制是否执行Worksheet_Calculate事件
Public Function s() As Object '这一句必须,用于引用SqlCel函数
Set s = Application.COMAddIns("SqlCelAddIn").Object
End Function
Sub tryM()
On Error GoTo line '这一句必须用于错误处理
Dim rn As Range, i As Integer, j As Integer
If ActiveCell.Column = 3 Then Set rn = ActiveCell.Offset(0, -1)
If ActiveCell.Column = 2 Then rn = ActiveCell.Offset(0, -1)
If rn Is Nothing Then Exit Sub
Dim qax1 As Variant, qax2 As Variant, qax3 As Variant, qaxrow As Variant
Set qax1 = s.exceltoqax(ThisWorkbook.path & "\20002394.xls", "半成品出入库传票$E4:W17") '将20002394.xls的部分数据读入qax1数据集
Set qax2 = s.exceltoqax(ThisWorkbook.path & "\20002365.xls", "半成品出入库传票$E4:W17") '将20002365.xls的部分数据读入qax2数据集
'*****************此处循环用于将qax2数据集的数据合并到qax1中
For i = 0 To s.qaxrows(qax2) - 1
Set qaxrow = s.newrow(qax1)
For j = 0 To s.qaxcols(qax1) - 1
Set qaxrow = s.setrowcell(qaxrow, j, s.getcell(qax2, i, j))
Next j
Set qax1 = s.addrow(qax1, qaxrow)
Next i
Set qax3 = s.qaxselect(qax1, "产品CODE='" & rn.Value & "'") '筛选qax1数据集
If s.qaxrows(qax2) > 0 Then
rn.Offset(0, 1).Value = s.getcell(qax2, 0, 6) '从数据集中取出数据并赋值给单元格
rn.Offset(0, 3).Value = s.getcell(qax2, 0, 18)
End If
Exit Sub
line: '这一句必须用于错误处理
s.setErr Err '这一句必须用于错误处理
End Sub
Sub FinnallyM() '这一句非必须用于最终将js设定为false
js = False
End Sub
以上代码需安装sqlcelfuncs插件才能正常运行。 |
|