Averageifs with an integer variable as criteria

nmk34

New Member
Joined
Apr 12, 2022
Messages
40
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
in excel worksheet i use for example:
=(AVERAGEIFS(B4:B262078,C4:C262078,">"&S2,C4:C262078,"<"&N2,B4:B262078,">"&1130,B4:B262078,"<"&1149))
then in vba i use:
Public SelectedProductMin SelectedProductMax as integers 'IN ANOTHER MODULE

of5minago = WorksheetFunction.AverageIfs(Worksheets("sheet1").Range("B4:B262078"), _
Worksheets("sheet1").Range("C4:C262078"), ">" & Worksheets("sheet1").Range("S2"), _
Worksheets("sheet1").Range("C4:C262078"), "<" & Worksheets("sheet1").Range("N2"), _
Worksheets("sheet1").Range("B4:B262078"), ">" & SelectedProductMin, Worksheets("sheet1").Range("B4:B262078"), "<" & SelectedProductMax)

the above code works in excel vba when i use numbers instead of SelectedProductMin and SelectedProductMax . what i want to do replace 1130 and 1149 with integer variables like above. i declareed the variables public in another module. these two integer variables are based on selected index value from a listview control. i keep getting an error. if use constants it works but not variables. Please help, i could not find any thread anywhere on this
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Aside from the fact you need:

Code:
Public SelectedProductMin As Integer, SelectedProductMax as integer

your code is fine - assuming you assign those two variables a value (otherwise they default to 0)
 
Upvote 0
of5minago = WorksheetFunction.AverageIfs(Worksheets("sheet1").Range("B4:B262078"), _
Worksheets("sheet1").Range("C4:C262078"), ">" & Worksheets("sheet1").Range("S2"), _
Worksheets("sheet1").Range("C4:C262078"), "<" & Worksheets("sheet1").Range("N2"), _
Worksheets("sheet1").Range("B4:B262078"), ">" & SelectedProductMin, Worksheets("sheet1").Range("B4:B262078"), "<" & SelectedProductMax)
code halts when i run it at this code above. if i move my mouse over SelectedProductMin i see a value of 216 and move the mouse over SelectedProductMax and i see a value of 228.
the error code:
image attached
 

Attachments

  • Capture111.PNG
    Capture111.PNG
    35.4 KB · Views: 6
Upvote 0
You'd get that error if nothing matches your criteria (just as AVERAGEIFS would return an error as a formula).
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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