error object required to use function average

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,433
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
I try using average function but it gives object error in this line
VBA Code:
Set myRangeC1 = Worksheets("Sheet1").Range("G2").Value
the column B contains values and range G2 also values and the formula should be like this average(B2/G2) and the result should be in column C
any idea to fix it ,please?
VBA Code:
Sub Test()

        Dim nb_rows As Long
        Dim myRangeC, myRangeC1 As Range
        Dim i As Long

        nb_rows = Range("b" & Rows.Count).End(xlUp).Row

        'Change Sheet1 to your sheet name
        Set myRangeC = Worksheets("Sheet1").Range("B2:B" & nb_rows)
        Set myRangeC1 = Worksheets("Sheet1").Range("G2").Value

        For i = 2 To nb_rows
                Range("C" & i).Value = Application.WorksheetFunction.Average(myRangeC / myRangeC1)
        Next i

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
@Fluff you right but , I note there is a little simple when shows the value . for instance if the B2 =542, G2=3 based on the formula in OP =181
and if use just B2/G2 . it will give 180.6667
 
Upvote 0
@Joe4 forgive me , but why you want picture, the column B and cells G2 are number and the result in column C
 
Upvote 0
The average of 180.6667 is 180.6667 but it will show as 181 if you have the cell set to display no decimal places.
 
Upvote 0
@abdelfattah, not sure what you want to achieve, but afaik worksheet functions cannot handle Range objects within VBA in this way.
See whether this is what you're after.

VBA Code:
Sub abdelfattah()

        Dim nb_rows As Long
        Dim myRangeC As Range
        Dim i As Long

        nb_rows = Range("b" & Rows.Count).End(xlUp).Row

        Set myRangeC = Worksheets("Sheet1").Range("B2:B" & nb_rows)
        
        For i = 2 To nb_rows
                Range("C" & i).Value = Excel.Application.Evaluate("=Average(" & myRangeC.Address & "/" & Range("G" & i).Value & ")")
        Next i
End Sub
 
Upvote 0
No idea ...
the column B contains values and range G2 also values and the formula should be like this average(B2/G2) and the result should be in column C

... so you were supposed to have numbers in column B. The code I provided puts the results in column C as requested.
 
Upvote 0
Can you please explain exactly what you want the code to do?
 
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,480
Members
449,455
Latest member
jesski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top