【发布时间】:2021-10-09 02:25:04
【问题描述】:
我一直在努力寻找一种方法来为列中的第一个空白单元格添加平均值(该列中将有多个空白单元格,因为它被分成几组,但我希望在之后循环它它填补了第一个空白,它将为下一个空白做同样的事情。我希望它在显示的图像中平均 C2:C6,但单元格范围是动态的,列数也是如此。我试图让它找到下一个空白行然后平均高于它,但显然我不擅长代码。第二个空白是标准错误,但我希望编辑代码以将平均值应用于标准错误。此外,这段代码的大部分来自 StackExchange 上的 Ambie (Reference Link)
Example of the sheet I am working with
Sub MeanSEM01()
Dim nextrow As Long
nextrow = Cells(Rows.Count, "A").End(xlUp).Row + 1
Dim lastCol As Long, i As Long
Dim rng As Range
'Find the last column.
'Assumes the relevant column is the last one with data in row 5.
With Sheet1
lastCol = .Cells(nextrow - 1, .Columns.Count).End(xlToLeft).Column
End With
'Iterate the columns from 1 (ie "A") to the last.
For i = 1 To lastCol
With ActiveSheet
'Define the data range for this column.
'Assumes last cell from bottom of sheet is the end of data.
Set rng = .Range(.Cells(nextrow - 1, i), .Cells(.Rows.Count, i).End(xlUp))
'Write the average to the cell above.
.Cells(nextrow, i) = WorksheetFunction.Average(rng)
End With
Next
End Sub
我还检查了即时窗口,发现我的范围定义肯定是关闭的,这就是它在平均步骤中失败的原因。我也尝试从工作表函数更改为公式 Range(.Cells(nextrow, i)).Formula = "=Average(rng)" 但我认为我的范围是我的问题。
【问题讨论】:
-
我还检查了即时窗口,发现我的范围定义肯定是关闭的,这就是它在平均步骤失败的原因。我也尝试从工作表函数更改为公式
Range(.Cells(nextrow, i)).Formula = "=Average(rng)",但我认为我的范围是我的问题