Hi, I am new to VBA but have some familiarity with programming.
I am trying to average a set of values based on the date the values were entered for each unique date. I am keeping a count and running total as my program looks through the data for each date that is the same as the previous (then dividing the "running total" by "count") to get the average). I am then trying to output this info in 2 columns - date and average value.
I think the main problem is that I am trying to store the running total in a cell and then do math on it (based on what I could find on the error code given and other forums). But I can't seem to store the cell value in a variable either so I don't know the best practice or work around for this. Full code below, but I think the main problem is these two lines:
Cells(k, 12).Value = Cells(k, 12).Value + Cells(i, 6).Value
...
Cells(k, 12).Value = Cells(k, 12).Value / count
Where k and i are iterative variables for the row.
Private Sub CommandButton1_Click()
Dim i As Integer
Dim k As Integer
Dim count As Integer
i = 2
k = 2
count = 0
Do While Cells(i, 1).Value <> ""
If count = 0 Then
'initial value for a new date
Cells(k, 11).Value = Cells(i, 1).Value 'Column 1=date stamps, column 6=value; column 11=date, column 12=average
Cells(k, 12).Value = Cells(i, 6).Value
count = 1
Else
i = i + 1
'if date is different, begin adding values from column 6 and store them in column 12
If Cells(i, 1).Value = Cells(k, 11).Value Then
Cells(k, 12).Value = Cells(k, 12).Value + Cells(i, 6).Value
count = count + 1
Else
'if date is same, calculate average, and increment the place to store date (for average)
Cells(k, 12).Value = Cells(k, 12).Value / count
count = 0
k = k + 1
End If
End If
Loop
End Sub
Thanks!
Bookmarks