我有一个Excel文件,其中有前四个值,然后是三个整数。我需要将这三个整数组合成一个单元格,并且该单元格需要是它们的平均值。所以来自:
- Gender |Age |Country |Score 1|Score 2|Score 3 - male |15 |NL |1 |4 |1 | - male |23 |NL |3 |3 |2 | - female |34 |UK |4 |3 |3 | - male |53 |USA |5 |4 |4 |
为:
- Gender |Age |Country |Score - male |15 |NL |2 - male |23 |NL |3 - female |34 |UK |3 - male |53 |USA |4
答案 0 :(得分:1)
以下宏将执行您想要的操作。您可以直接从工作表中调用vba函数,但是当您想要替换单元格时,函数没有意义。我将循环的参数放在变量中,以便更容易修改以满足您的需求。
如果你需要四舍五入,那么在
中进行舍入 avg=avg/3
代码行。
Sub ConsolidatetoAvg()
Dim StartingRow As Integer
StartingRow = 2
Dim EndingRow As Integer
EndingRow = 5
Dim StartingColumn As Integer
StartingColumn = 4
Dim index As Integer
Dim x As Integer
Dim avg As Double
For index = StartingRow To EndingRow
avg = 0
For x = StartingColumn To StartingColumn + 3
avg = avg + Cells(index, x)
Cells(index, x) = ""
Next
avg = avg / 3
Cells(index, StartingColumn) = avg
Next
End Sub
答案 1 :(得分:0)
同时假设- Gender
在A1:
=ROUND(AVERAGE(D2:F2),0)
在G2中并复制到适合。然后选择ColumnG,复制,选择特定粘贴,值,删除列D:F并在D1中输入Score
。