比如
Dim st As Date = Date.Now '用于计算时间
Dim s As String = e.Form.Controls("月份").Value
If s <> "" Then
Tables("月报表").StopRedraw() '停止重绘
DataTables("月报表").DataRows.Clear
Dim dt_in As List(Of DataRow)
dt_in = DataTables("入仓").Select("型号 is not null", "型号,规格,月份") '排序取出 入仓 全部数据
Dim r As Row
Dim sum_curr_money, sum_prev_money As Double
Dim sum_curr_count, sum_prev_count As Integer
r = Tables("月报表").AddNew
r("型号") = dt_in(0)("型号")
r("规格") = dt_in(0)("规格")
If dt_in(0)("月份") < s Then '之前月
sum_prev_count = sum_prev_count + dt_in(0)("入仓数量")
sum_prev_money = sum_prev_money + dt_in(0)("入仓金额")
Else If dt_in(0)("月份") = s Then '本月份
sum_curr_money = sum_curr_money + dt_in(0)("入仓金额")
sum_curr_count = sum_curr_count + dt_in(0)("入仓数量")
End If
For i As Integer = 1 To dt_in.Count - 1 '写入型号和规格,同时统计入仓数量和金额
If dt_in(i)("型号") <> dt_in(i-1)("型号") OrElse dt_in(i)("规格") <> dt_in(i-1)("规格") Then '不同产品
r("本月入仓_数量") = sum_curr_count
r("本月入仓_金额") = sum_curr_money
r("上月结存_数量") = sum_prev_count '暂存
r("上月结存_金额") = sum_prev_money '暂存
r = Tables("月报表").AddNew '插入新数据
r("型号") = dt_in(i)("型号")
r("规格") = dt_in(i)("规格")
sum_curr_money = 0
sum_curr_count = 0
sum_prev_count = 0
sum_prev_money = 0
End If
If dt_in(i)("月份") < s Then '之前月
sum_prev_count = sum_prev_count + dt_in(i)("入仓数量")
sum_prev_money = sum_prev_money + dt_in(i)("入仓金额")
Else If dt_in(i)("月份") = s Then '本月份
sum_curr_money = sum_curr_money + dt_in(i)("入仓金额")
sum_curr_count = sum_curr_count + dt_in(i)("入仓数量")
End If
Next
r("本月入仓_数量") = sum_curr_count
r("本月入仓_金额") = sum_curr_money
r("上月结存_数量") = sum_prev_count '暂存
r("上月结存_金额") = sum_prev_money '暂存
MessageBox.Show("复制型号和规格耗时: " & (Date.Now - st).TotalSeconds & "秒")
st = Date.Now '用于计算时间
For Each dr As DataRow In DataTables("月报表").DataRows '填充统计数据
sum_curr_count = 0 '累计本月出仓数量
sum_prev_count = 0 '累计之前出仓数量
Dim drs_out As List(Of DataRow)
Dim filter As String = "型号 = '" & dr("型号") & "' And 规格 = '" & dr("规格") & "'"
drs_out = DataTables("出仓").Select(filter, "月份") '排序取出 出仓 全部数据
For Each dr_out As DataRow In drs_out
If dr_out("月份") < s Then
sum_prev_count = sum_prev_count + dr_out("出仓数量")
Else If dr_out("月份") = s Then
sum_curr_count = sum_curr_count + dr_out("出仓数量")
Else
Exit For
End If
Next
Dim qcsl As Double = DataTables("初始库存").compute("sum(数量)", "型号 = '" & dr("型号") & "' And 规格 = '" & dr("规格") & "'")
Dim sum_in_prev_count As Integer = dr("上月结存_数量") '从表中取出暂存数据
Dim sum_in_prev_money As Double = dr("上月结存_金额") '从表中取出暂存数据
dr("本月出仓_数量") = sum_curr_count
dr("上月结存_数量") = sum_in_prev_count - sum_prev_count + qcsl
dr("上月结存_金额") = sum_in_prev_money / sum_in_prev_count * dr("上月结存_数量")
dr("本月加权平均单价") = (dr("上月结存_金额") + dr("本月入仓_金额"))/(dr("上月结存_数量") + dr("本月入仓_数量"))
dr("本月结存_数量") = dr("上月结存_数量") + dr("本月入仓_数量") - dr("本月出仓_数量")
dr("本月结存_金额") = dr("本月加权平均单价") * dr("本月结存_数量")
dr("本月出仓_金额") = dr("本月加权平均单价") * dr("本月出仓_数量")
Next
Tables("月报表").ResumeRedraw()
End If
MessageBox.Show("统计和写入耗时: " & (Date.Now - st).TotalSeconds & "秒")