|
VBA万岁 发表于 2014-5-26 15:37
加上如下代码即可:
c = c + Val(brr(i, 6))
Next i
即:
Option Explicit
Private Sub ComboBox1_Change()
Dim sql, brr, i&, c As Long, ITM
sql = "select 月,日, 项目,材料名称,单位,数量 from [Sheet1$A4:I] where 编号=" & ComboBox1.Value
brr = SqlToArr(sql)
UserForm1.ListView1.ListItems.Clear
For i = 1 To UBound(brr)
'添加记录
Set ITM = ListView1.ListItems.Add()
'添加内容
ITM.Text = brr(i, 1)
ITM.SubItems(1) = brr(i, 2)
ITM.SubItems(2) = brr(i, 3)
ITM.SubItems(3) = brr(i, 4)
ITM.SubItems(4) = brr(i, 5)
ITM.SubItems(5) = brr(i, 6)
c = c + Val(brr(i, 6)) Next i
Set ITM = ListView1.ListItems.Add()
ITM.Text = "汇总"
ITM.SubItems(5) = cEnd Sub
Private Sub UserForm_Initialize()
Dim ITM As ListItem, sql$
With ListView1
.ColumnHeaders.Add , , "月", .Width / 16, lvwColumnLeft '居左--第一列只能居左
.ColumnHeaders.Add , , "日", .Width / 16, lvwColumnCenter
.ColumnHeaders.Add , , "项目名称", .Width / 4, lvwColumnCenter
.ColumnHeaders.Add , , "材料名称", .Width / 4, lvwColumnCenter
.ColumnHeaders.Add , , "单位", .Width / 9, lvwColumnRight '居右
.ColumnHeaders.Add , , "数量", .Width / 5, lvwColumnRight '居右
.View = lvwReport '设置显示格式为报表格式
.Gridlines = True '显示网格线
.FullRowSelect = True
End With
sql = "select distinct 编号 from [Sheet1$A4:I] where not 编号 is null "
ComboBox1.List = SqlToArr(sql)
End Sub
Function SqlToArr(ByVal sql$) '查询结果到数组
Dim cnn As Object 'New ADODB.Connection
Dim rs As Object, arr 'New ADODB.Recordset
Set cnn = CreateObject("adodb.connection")
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties =Excel 12.0;Data Source =" & ThisWorkbook.FullName
'On Error Resume Next
Set rs = cnn.Execute(sql)
SqlToArr = Application.Transpose(rs.GetRows) '转置为excle格式的行列
'Set cnn = Nothing: Set rs = Nothing
End Function
|
|