|
Sub 查询()
Dim cnn As Object, rs
Dim Sql As String, arr, i%, j%, m%, brr(1 To 999, 1 To 2), jg, sl
Set cnn = CreateObject("ADODB.CONNECTION")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open "Provider=Microsoft.ACE.OleDb.12.0;Extended Properties='Excel 12.0;HDR=YES'; Data Source=" & ThisWorkbook.FullName
Sql = "select distinct 名称 from [Sheet2$a1:c16] "
arr = cnn.Execute(Sql).getrows
For i = 0 To UBound(arr, 2)
Sql = "select * from [Sheet2$a1:c16] "
rs.Filter = "名称='" & arr(0, i) & "'"
rs.Open Sql, cnn, 1, 1
m = m + 1
sl = 0
brr(m, 1) = rs.Fields("名称")
For j = 1 To rs.RecordCount
If j = 1 Then
jg = rs.Fields("数量") * rs.Fields("当期价格")
brr(m, 2) = jg
sl = rs.Fields("数量")
Else
jg = brr(m, 2) + (rs.Fields("数量") - sl) * rs.Fields("当期价格")
brr(m, 2) = jg
sl = rs.Fields("数量")
End If
rs.MoveNext
Next
rs.Close
Next
With Sheet1
.[a2:b99] = ""
.[a2].Resize(m, 2) = brr
End With
Set cnn = Nothing
Set rs = Nothing
End Sub
|
评分
-
1
查看全部评分
-
|