1

I have a formula in my VBA code in which I would like to embed a variable. I defined an integer but am unsure how to add it to my formula.

 Dim Formula as Variant
 Dim Stability as Integer
 Stability = 0.5
 Formula = Application.WorksheetFunction.AverageIfs(range("C:C"), range("C:C"), ">=10", range("C:C"), "<=15", range("F:F"), "<=Stability")

At the end of the formula I want to include my Stability variable. What is the correct syntax for doing so?

3
  • May i ask the purpose of your code? why don't you just construct the formula and stick it to the cell value so excel will calculate for you? like .range("A2).Formula = "=Average('C:C')" ? Commented Aug 28, 2014 at 18:54
  • 1
    Note that an Integer is a, we'll, integer, and cannot hold a floating point value. Use Double instead. Commented Aug 28, 2014 at 19:12
  • I tried declaring it as a double but it still errors. Commented Aug 29, 2014 at 15:06

1 Answer 1

2

try

Formula = ..., "<=" & Stability)
Sign up to request clarification or add additional context in comments.

3 Comments

I get an error when doing that. "Unable to get the AverageIfs property of the WorksheetFunction class.
@MalusPuer make sure you understand how =AVERAGEIFS works
The code works when I have .5 where stability is in the formula. It just wont let me put a variable there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.