这样吗?
For Each dr As DataRow In DataTables("库存V成本").DataRows
If dr.IsNull("日期") OrElse dr.IsNull("品名编号") Then
Continue For
End If
Dim d As Date = dr("日期")
Dim y As Integer = d.Year
Dim m As Integer = d.Month
Dim d1 As Date = new Date(y,m,1)
Dim d2 As Date = new Date(y,m,Date.DaysInMonth(y,m))
Dim s1 As String = "品名编号 = '" & dr("品名编号") & "' And 日期 >= #" & d1 & "# And 日期 <= #" & d2 & "# And 部门 Like '入库*'"
Dim s2 As String = "品名编号 = '" & dr("品名编号") & "' And 日期 >= #" & d1 & "# And 日期 <= #" & d2 & "# And 部门 Like '出库*'"
Dim s3 As String = "品名编号 = '" & dr("品名编号") & "' And 日期 >= #" & d1 & "# And 日期 <= #" & d2 & "#"
dr("账面库房数量_本月入库") = DataTables("出入库").Compute("Sum(核算数量)",s1)
dr("账面库房数量_本月出库") = DataTables("出入库").Compute("Sum(核算数量)",s2)
Dim dr2 As DataRow = DataTables("盘点").Find("品名编号 = '" & dr("品名编号") & "' And 日期 <= #" & d1 & "#","日期 Desc")
If dr2 IsNot Nothing Then
dr("账面库房数量_上月库房完好") = dr2("实盘数量_库房_完好数量")
dr("结算_上月库外完好数量")=dr2("实盘数量_本月库外完好")
Else
dr("账面库房数量_上月库房完好") = Nothing
dr("结算_上月库外完好数量") = Nothing
End If
dr("账面库房数量_月末库存") = dr("账面库房数量_本月入库") + dr("账面库房数量_上月库房完好") - dr("账面库房数量_本月出库")
dr("结算_结算数量") = dr("结算_库外领用累计")-DataTables("盘点").Compute("Sum(实盘数量_本月库外完好)",s3)
Next