Average in VBA in Excel

Look closely at what you’re trying to do in this code:

l = Cells(i, 12).Value
m = Cells(k, 12).Value

Cells(j, 20).Value = [Average (l : m)]

You’re assigning a “long” value to each of l and m and then calling them in the average function as if they were references to a cell.

I suggest that you add a range variable, assign the location of the data you want to average and use that as your function argument.

Something like:

Dim myRange as Range

'Loop code here
Set myRange = Range("L" & i & ":L" & k)
Cells(j, 20).Value = Application.WorksheetFunction.Average(myRange)

Leave a Comment

tech