|
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("c1:c2")) Is Nothing Then
Range("b3:c100").Clear
'引用MICROSOFT ACTIVEX DATA OBJECTS 2.XLIBRARY
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
NF = Sheet2.Cells(1, 3): GYS = Sheet2.Cells(2, 3)
cn.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;EXTENDED PROPERTIES=EXCEL 8.0;DATA SOURCE=" & ThisWorkbook.FullName
Sql = "select 客户,金额CNY from(select * from [源数据$A2:E] WHERE 年份= " & NF & " AND 供应商='" & GYS & "' ORDER BY 金额CNY DESC)"
n = Sheet2.Range("b65536").End(xlUp).Row
rs.Open Sql, cn, 3, 2
X = rs.RecordCount
With Sheet2
If X <= 5 Then
.Range("B" & n + 2).Resize(, 2) = Array("客户", "金额CNY")
.Range("b" & n + 3).CopyFromRecordset cn.Execute(Sql)
SQLL = "SELECT SUM(金额CNY) FROM (select 客户,金额CNY from(select * from [源数据$A2:E] WHERE 年份= " & NF & " AND 供应商='" & GYS & "' ORDER BY 金额CNY DESC))"
.Cells(n + 1 + 8, 2) = "Total"
.Range("C" & n + 1 + 8).CopyFromRecordset cn.Execute(SQLL)
Else
.Cells(n + 2, 2) = "客户": .Cells(n + 2, 3) = "金额CNY"
SQLL = "select 客户,金额CNY from(select TOP 5 * from [源数据$A2:E] WHERE 年份= " & NF & " AND 供应商='" & GYS & "' ORDER BY 金额CNY DESC)"
.Range("B" & n + 3).CopyFromRecordset cn.Execute(SQLL)
.Cells(n + 1 + 7, 2) = "OTHERS"
.Cells(n + 1 + 8, 2) = "Total"
SQLLL = "select SUM(金额CNY) from(select * from [源数据$A2:E] WHERE 年份= " & NF & " AND 供应商='" & GYS & "' ORDER BY 金额CNY DESC)"
.Range("C" & n + 1 + 8).CopyFromRecordset cn.Execute(SQLLL)
SQLLLL = "select sum(金额CNY) from(select TOP 5 * from [源数据$A2:E] WHERE 年份= " & NF & " AND 供应商='" & GYS & "' ORDER BY 金额CNY DESC)"
.Cells(n + 1 + 7, 3).CopyFromRecordset cn.Execute(SQLLLL)
.Cells(n + 1 + 7, 3) = .Cells(n + 1 + 8, 3) - .Cells(n + 1 + 7, 3)
End If
End With
End If
End Sub
|
|