excel - VBA:对每行最右边的单元格求和?
问题描述
我有一个类似于下面的数据集。
我只想平均最右边一列中的数据。
C4
我的范围从行或列 开始,没有设定的结束。
**1**
1 **2**
1 2 **3**
1 **2**
**1**
我已经将以下内容放在一起,它可以为单个固定列执行我需要它执行的操作,但我不知道如何将其扩展为始终使用最右侧列中的值。
Dim Sum, Count As Integer
Count = 0
Sum = 0
Application.ScreenUpdating = False
Range("C4").Select
Do While ActiveCell.Value <> ""
Sum = Sum + ActiveCell.Value
Count = Count + 1
ActiveCell.Offset(1, 0).Activate
Loop
Range("O1").Value = Sum / Count
谢谢你。
解决方案
这将循环行。
MATCH 将返回最后一列的列号,其中包含一个数字。
然后我们在该列中获取该行的数字并将其添加到数组中。
然后在循环之后我们平均数组。
Sub aver()
With Worksheets("Sheet4") 'Change to your sheet
Dim lastrw As Long
lastrw = .Cells(.Rows.Count, 3).End(xlUp).Row
Dim num() As Variant
ReDim num(1 To lastrw) As Variant
Dim i As Long
For i = 4 To lastrw
Dim j As Long
j = Application.Match(1E+99, Rows(i), 1)
num(i) = .Cells(i, j).Value
Next i
.Range("O1").Value = Application.Average(num)
End With
End Sub
推荐阅读
- c# - 如何根据其中一项属性使用 Linq 获取独特的项目
- c - 警告 #3180:无法识别的 OpenMP #pragma。已检查其他答案,但没有一个与我的问题相对应
- angular - 如何在 Angular 中修复此发布请求
- jquery - 使用 xml 请求将文件上传到数据库
- c# - c#组合和多重继承设计
- rabbitmq - rabbitmq-server-3.8.9 无法启动服务器
- python - Python 中的 JSON 错误:json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)
- php - PHP 成员函数 null
- javascript - 获取数组集合中的对象数量 JAVASCRIPT
- reactjs - 'ReactPlayer' 不能用作 JSX 组件