帮我生产一份EXCEl数据自动统计的vba代码
时间: 2023-05-16 20:06:21 浏览: 89
好的,以下是一份简单的 VBA 代码,可以自动统计 Excel 数据:
Sub DataStatistics()
Dim lastRow As Long
Dim i As Long
Dim total As Double
Dim average As Double
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
total = total + Cells(i, 2).Value
Next i
average = total / (lastRow - 1)
Range("D2").Value = total
Range("D3").Value = average
End Sub
这份代码会计算第二列数据的总和和平均值,并将结果分别放在 D2 和 D3 单元格中。如果你需要统计其它列的数据,只需要修改代码中的第二列 (Cells(i, 2)) 即可。
相关问题
写一个Excel数据查询的vba代码
好的,以下是一个基本的Excel数据查询的VBA代码示例,仅供参考:
Sub QueryData()
Dim ws As Worksheet '定义工作簿对象
Set ws = ThisWorkbook.Sheets("Sheet1") '设置要查询的工作簿
Dim dataRange As Range '定义数据范围
Set dataRange = ws.Range("A1:B10") '设置数据范围,假设是A1到B10的数据
Dim searchValue As String '定义要查询的关键字
searchValue = InputBox("请输入要查询的关键字:") '通过输入框输入要查询的关键字
Dim resultRange As Range '定义查询结果范围
Set resultRange = dataRange.Find(searchValue) '通过Find方法查找关键字匹配的单元格
If Not resultRange Is Nothing Then '如果找到了匹配
MsgBox "已找到关键字" & searchValue & vbCrLf & "数据在" & resultRange.Address
Else '如果未找到匹配
MsgBox "未找到关键字" & searchValue
End If
End Sub
注意,这只是一个基础的示例,你需要根据具体需求进行修改和扩展。
帮我写一个 Excel 颜色求和的vba 宏代码
当然可以,以下是一个简单的 Excel 颜色求和的 VBA 宏代码:
```VBA
Sub SumColor()
Dim cell As Range
Dim sum As Integer
For Each cell In Selection
If cell.Interior.Color = RGB(255, 255, 0) Then '黄色
sum = sum + cell.Value
End If
Next cell
MsgBox "选中区域中黄色单元格的和为" & sum
End Sub
```
这段代码的功能是对选中区域中黄色单元格进行求和,并通过消息框显示结果。你可以根据自己的需要修改颜色和消息框的提示文字。