急求在EXCEL实现某字段自动筛选统计的宏代码,谢谢拉!!!!

2025-01-07 00:00:24
推荐回答(3个)
回答1:

增加一张表命名为"查询表",原表命名为"统计表",将按钮放在"查询表中",在"查询"表的第三行输入字段名称(列标题).并在"查询表中"将以下的宏指定给按钮.
Sub 查询()

Dim i As Integer
Dim n As String
lastrow = Worksheets("统计表").UsedRange.SpecialCells(xlCellTypeLastCell).Row
n = InputBox(prompt:="请输入部门名称")
Worksheets("查询表").select
Worksheets("查询表").Range("a4:e200") = ""
For i = 4 To lastrow

If Worksheets("统计表").Cells(i, 4) = n Then
Worksheets("查询表").Cells(i, 1) = Worksheets("统计表").Cells(i, 1)
Worksheets("查询表").Cells(i, 2) = Worksheets("统计表").Cells(i, 2)
Worksheets("查询表").Cells(i, 3) = Worksheets("统计表").Cells(i, 3)
Worksheets("查询表").Cells(i, 4) = Worksheets("统计表").Cells(i, 4)
Worksheets("查询表").Cells(i, 5) = Worksheets("统计表").Cells(i, 5)

End If
Next
For i = [a1000 ].End(xlUp).Row To 4 Step -1
If Worksheets("查询表").Cells(i, 1) = "" Then
Rows(i).Delete
End If
Next

last = Worksheets("查询表").UsedRange.SpecialCells(xlCellTypeLastCell).Row

'写入合计
Worksheets("查询表").Cells(last + 1, 1) = "合计"

'工资合计
Worksheets("查询表").Cells(last + 1, 2) = WorksheetFunction.Sum(Range(Cells(4, 2), Cells(last, 2)))
'资金合计
Worksheets("查询表").Cells(last + 1, 3) = WorksheetFunction.Sum(Range(Cells(4, 3), Cells(last, 3)))

End Sub
以上代码已测试通过.

回答2:

Sub chiefzjh()
Dim Dep$, i%
Dep = InputBox("请输入要查找的部门名称")
For i = [d65536].End(3).Row To 4 Step -1
If Cells(i, 4).Value <> Dep Then Rows(i).Delete
Next i
i = [b65536].End(3).Row + 1
Cells(i, 1).Value = "合计"
Cells(i, 2).Value = WorksheetFunction.Sum(Range(Cells(4, 2), Cells(i - 1, 2)))
Cells(i, 3).Value = WorksheetFunction.Sum(Range(Cells(4, 3), Cells(i - 1, 3)))
End Sub

回答3:

你是想点击一个按钮就实现是不/?最简单录制宏,单击工具-宏-录制新宏。然后把你说的那系事件在从头至尾做一次。做完了后,单击工具-宏-停止录制,ok了但必须增加一张表命名为"查询表",原表命名为"统计表",将按钮放在"查询表中",在"查询"表的第三行输入字段名称(列标题).并在"查询表中"将以下的宏指定给按钮.