用vb来解答 求代码
求大神帮助
在Sheet2中,利用数据库函数及已设置的条件区域,计算以下情况的结果,并将结果保存相应的单元格中。
a. 计算:商标为
上海,瓦数小于100的白炽灯的平均单价;
b. 计算:产品为白炽灯,其瓦数大于等于80且小于等于100的数量。
'//引用Microsoft Data Oblject ActiveX 2.5
'//模块里面写函数,例如我写查询远程SQL数据库
'1.先初始化
For I = 3 To 600 Step 1
Sheet3.Cells(I, 1) = ""
Sheet3.Cells(I, 2) = ""
Sheet3.Cells(I, 3) = ""
Sheet3.Cells(I, 4) = ""
Sheet3.Cells(I, 5) = ""
Sheet3.Cells(I, 6) = ""
Sheet3.Cells(I, 7) = ""
Sheet3.Cells(I, 8) = ""
Sheet3.Cells(I, 9) = ""
Sheet3.Cells(I, 10) = ""
Sheet3.Cells(I, 11) = ""
Sheet3.Cells(I, 12) = ""
Sheet3.Cells(I, 13) = ""
Next I
'//再连接数据库
Dim cnn As ADODB.Connection
Dim 卖斗rs As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
'//当然也可以连接自身表格,语句网上大把的
cnn.ConnectionString = "Driver={SQL Server};Server=192.168.1.200;UID=sa;PWD=5332852;DataBase=SmartSchedule"
cnn.Open
'//在这里按条件筛选
rs.Open "Select * from 尾欠信息 where 核对时间='" + Str(Sheet3.Cells(1, 8)) + "' order by 批次", cnn
'//从除列名外位置,填充数据
Dim n As Integer
n = 2
Do While Not rs.EOF
Sheet3.Cells(n + 1, 1) = n - 1
Sheet3.Cells(n + 1, 2) = rs.Fields("批次")
Sheet3.Cells(n + 1, 3) = rs.Fields("下单数量")
Sheet3.Cells(n + 1, 4) = rs.Fields("特征")
Sheet3.Cells(n + 1, 戚卖5) = rs.Fields("下单日期")
Sheet3.Cells(n + 高配逗1, 6) = rs.Fields("需求日期")
Sheet3.Cells(n + 1, 7) = rs.Fields("尾欠")
Sheet3.Cells(n + 1, 8) = rs.Fields("备注")
Sheet3.Cells(n + 1, 9) = rs.Fields("核对结果")
Sheet3.Cells(n + 1, 10) = rs.Fields("异常说明")
Sheet3.Cells(n + 1, 11) = rs.Fields("异常时间")
Sheet3.Cells(n + 1, 12) = rs.Fields("相关解决人")
Sheet3.Cells(n + 1, 13) = rs.Fields("核对时间")
rs.MoveNext
n = n + 1
Loop
a. AVERAGEIFS(E2:E17,D2:D17,'上海',A2:A17,'白炽灯冲行',B2:B17,'<100')
b. SUMPRODUCT((A2:A17='白炽散圆哗灯')*(B2:B17>=80)*(B2:B17<腔歼=100))