error object required to use function average

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You only use the "Set" command when setting objects, like ranges.

If you only want the value, drop the word "Set", i.e.
VBA Code:
myRangeC1 = Worksheets("Sheet1").Range("G2").Value

If you want to set it to be a range variable, drop the ".Value", i.e.
VBA Code:
Set myRangeC1 = Worksheets("Sheet1").Range("G2")
 
Upvote 0
What are you trying to do? average(B2/G2)makes no sense as you would be averaging a single number.
 
Upvote 0
When you hit "Debug", which line of code does it highlight?

Can you show us what the data that is in column B and cell G2 look like, and explain, in plain English, exactly how you want this to work.
 
Upvote 0
@Fluff After dividing a number by a number then should calculate average the result after dividing .
 
Upvote 0
Can you show us what the data that is in column B and cell G2 look like, and explain, in plain English, exactly how you want this to work.
I have ever mentioned in OP . they contain values(numbers).
 
Upvote 0
But after you have divided B2 by G2, you only have one value, so there is nothing to average.
If B2 is 20 & G2 is 10 B2/G2 is 2 & the average of 2 is 2
 
Upvote 0
I want to see your actual data and your expected result, so we can see what you are working with and clearly how you want this to work.
As they say, pictures say a thousand words (and your descriptions so far haven't been too helpful to Fluff or I in figuring out exactly how you want this to work).
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

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