Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 5599|回复: 3

[已解决]求助如何求和ListView1某例的值。谢谢

[复制链接]
发表于 2014-11-9 10:01 | 显示全部楼层 |阅读模式
本帖最后由 qinhuan66 于 2014-11-9 13:48 编辑

求助如何求和ListView1某例的值。谢谢
求助5.rar (48.19 KB, 下载次数: 48)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-11-9 10:14 | 显示全部楼层
Private Sub CommandButton1_Click()
    ListView1.ListItems.Clear
    If TextBox1 & TextBox2 & TextBox3 & TextBox4 & TextBox5 & TextBox6 = "" Then
        MsgBox "请至少输入一个查询条件"
        Exit Sub
    End If
    With Sheets("数据库")
        For RowIndex = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            If (.Cells(RowIndex, 1) Like "*" & TextBox1 & "*" Or TextBox1 = "") _
               And (.Cells(RowIndex, 2) Like TextBox2 + "*" Or TextBox2 = "") _
               And (.Cells(RowIndex, 3) Like UCase(TextBox3) + "*" Or TextBox3 = "") _
               And (.Cells(RowIndex, 4) Like TextBox4 + "*" Or TextBox4 = "") _
               And (.Cells(RowIndex, 5) Like TextBox5 + "*" Or TextBox5 = "") _
               And (.Cells(RowIndex, 13) Like TextBox6 + "*" Or TextBox6 = "") Then
                Set lst = ListView1.ListItems.Add(, , .Cells(RowIndex, 1))
                lst.SubItems(1) = .Cells(RowIndex, 2)
                lst.SubItems(2) = .Cells(RowIndex, 3)
                lst.SubItems(3) = .Cells(RowIndex, 4)
                lst.SubItems(4) = .Cells(RowIndex, 5)
                lst.SubItems(5) = .Cells(RowIndex, 6)
                lst.SubItems(6) = Format(.Cells(RowIndex, 7), "#,##0.00")
                lst.SubItems(7) = Format(.Cells(RowIndex, 8), "#,##0.00")
                lst.SubItems(8) = Format(.Cells(RowIndex, 9), "#,##0.00")
                lst.SubItems(9) = Format(.Cells(RowIndex, 10), "#,##0.00")
                lst.SubItems(10) = Format(.Cells(RowIndex, 11), "#,##0.00")
                lst.SubItems(11) = Format(.Cells(RowIndex, 12), "#,##0.00")
                lst.SubItems(12) = .Cells(RowIndex, 13)
                lst.SubItems(13) = .Cells(RowIndex, 14)
                lst.SubItems(14) = RowIndex
                End If
                total = total + .Cells(RowIndex, 7)
        Next
        Label9.Caption = "共找到 " & ListView1.ListItems.Count & " 条记录"
        Label10.Caption = "总费用: " & Format(total, "#,##0.00")
        Set dic = Nothing
    End With
End Sub
只需要在这里加一行就行了。

评分

参与人数 1 +3 收起 理由
qinhuan66 + 3 很给力!谢谢

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-11-9 13:42 | 显示全部楼层
xdragon 发表于 2014-11-9 10:14
Private Sub CommandButton1_Click()
    ListView1.ListItems.Clear
    If TextBox1 & TextBox2 & Text ...

可以了,但是好像合计的并不是ListView1显示的所有合计。而是数据库工作表的所有值。
回复

使用道具 举报

发表于 2014-11-9 13:43 | 显示全部楼层    本楼为最佳答案   
Private Sub CommandButton1_Click()
    ListView1.ListItems.Clear
    If TextBox1 & TextBox2 & TextBox3 & TextBox4 & TextBox5 & TextBox6 = "" Then
        MsgBox "请至少输入一个查询条件"
        Exit Sub
    End If
    With Sheets("数据库")
        For RowIndex = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
            If (.Cells(RowIndex, 1) Like "*" & TextBox1 & "*" Or TextBox1 = "") _
               And (.Cells(RowIndex, 2) Like TextBox2 + "*" Or TextBox2 = "") _
               And (.Cells(RowIndex, 3) Like UCase(TextBox3) + "*" Or TextBox3 = "") _
               And (.Cells(RowIndex, 4) Like TextBox4 + "*" Or TextBox4 = "") _
               And (.Cells(RowIndex, 5) Like TextBox5 + "*" Or TextBox5 = "") _
               And (.Cells(RowIndex, 13) Like TextBox6 + "*" Or TextBox6 = "") Then
                Set lst = ListView1.ListItems.Add(, , .Cells(RowIndex, 1))
                lst.SubItems(1) = .Cells(RowIndex, 2)
                lst.SubItems(2) = .Cells(RowIndex, 3)
                lst.SubItems(3) = .Cells(RowIndex, 4)
                lst.SubItems(4) = .Cells(RowIndex, 5)
                lst.SubItems(5) = .Cells(RowIndex, 6)
                lst.SubItems(6) = Format(.Cells(RowIndex, 7), "#,##0.00")
                lst.SubItems(7) = Format(.Cells(RowIndex, 8), "#,##0.00")
                lst.SubItems(8) = Format(.Cells(RowIndex, 9), "#,##0.00")
                lst.SubItems(9) = Format(.Cells(RowIndex, 10), "#,##0.00")
                lst.SubItems(10) = Format(.Cells(RowIndex, 11), "#,##0.00")
                lst.SubItems(11) = Format(.Cells(RowIndex, 12), "#,##0.00")
                lst.SubItems(12) = .Cells(RowIndex, 13)
                lst.SubItems(13) = .Cells(RowIndex, 14)
                lst.SubItems(14) = RowIndex
                total = total + .Cells(RowIndex, 7)
                End If
        Next
        Label9.Caption = "共找到 " & ListView1.ListItems.Count & " 条记录"
        Label10.Caption = "总费用: " & Format(total, "#,##0.00")
        Set dic = Nothing
    End With
End Sub
粘错位置了,重新改一下

评分

参与人数 1 +3 收起 理由
qinhuan66 + 3 很给力!谢谢

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-12 15:12 , Processed in 0.271208 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表