[英]VBA: Summing the rightmost cell per row?
我有一個數據集,看起來與下面類似。
我只想平均最右邊一欄中的數據。
我的范圍從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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.