Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Finding Average using VBA

Hi

For some reason I get an error message at the following line:

WorksheetFunction.Average(Cells(myCount, 2), Cells(myCount, 3))

below is the full code:

Sub Average()
Dim myCount As Integer, LastRow As Integer

LastRow = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1). _
    End(xlUp).Row
   
For myCount = 2 To LastRow
With Worksheets("Sheet2")
    .Cells(myCount, 6).Value _
    WorksheetFunction.Average(Cells(myCount, 2), Cells(myCount, 3))
End With
Next myCount
End Sub

Could someone explain to me why.

Thanks

Greg
0
greg_c
Asked:
greg_c
  • 2
1 Solution
 
Dushan De SilvaCommented:
0
 
greg_cAuthor Commented:
Run-time error '438':

Object doesn't support this property or method.
0
 
Patrick MatthewsCommented:
Two things wrong:

1) Missing an equal sign
2) Missing a qualifying reference for your Cells calls.

Fixing both:

With Worksheets("Sheet2")
    .Cells(myCount, 6).Value = Application.Average(.Cells(myCount, 2), .Cells(myCount, 3))
End With

Open in new window

0
 
greg_cAuthor Commented:
Perfect.  Thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now