1

I get this error: "Run-time error '1004'"" Unable to get the Sum property of the Worksheetfunction class

I have a code that looks into multiple comboboxes in a userform and sums their values that the user selects. The code was working fine with only numbers in the combobox list. I recently added one text option into the list and I called it "Not applicable". However, once I run the code I get the above error.

I believe the error is because I introduced the text option, and the sum function in VBA wouldn't recognize it.

Is there anyway to solve this issue?

Thanks

Here is the code I used:

Dim totalscore As Double
totalscore = WorksheetFunction.Sum(frmQA.ComboBox3.Value, frmQA.ComboBox4.Value, frmQA.ComboBox5.Value, frmQA.ComboBox6.Value, frmQA.ComboBox7.Value, frmQA.ComboBox8.Value, frmQA.ComboBox9.Value)

3 Answers 3

3

Assuming the text option is available in ComboBox3 adjust this part of the sum formula:

frmQA.ComboBox3.Value

To this:

Iif(IsNumeric(frmQA.ComboBox3.Value),frmQA.ComboBox3.Value,0)

If they all have the text option you can do this:

totalscore = totalscore + Iif(IsNumeric(frmQA.ComboBox3.Value),frmQA.ComboBox3.Value,0)
totalscore = totalscore + Iif(IsNumeric(frmQA.ComboBox4.Value),frmQA.ComboBox4.Value,0)
...
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Scott thanks your iif(Isnumeric) command was very helpful here is the code that I used
Dim totalscore As Double totalscore = WorksheetFunction.Sum(IIf(IsNumeric(frmQA.ComboBox3.Value), frmQA.ComboBox3.Value, 0), IIf(IsNumeric(frmQA.ComboBox4.Value), frmQA.ComboBox4.Value, 0), IIf(IsNumeric(frmQA.ComboBox5.Value), frmQA.ComboBox5.Value, 0), IIf(IsNumeric(frmQA.ComboBox6.Value), frmQA.ComboBox6.Value, 0), IIf(IsNumeric(frmQA.ComboBox7.Value), frmQA.ComboBox7.Value, 0), IIf(IsNumeric(frmQA.ComboBox8.Value), frmQA.ComboBox8.Value, 0), IIf(IsNumeric(frmQA.ComboBox9.Value), frmQA.ComboBox9.Value, 0))
glad it worked. please mark as answered so others know going forward.
how to do that?
there's a grey check mark right below the downvote arrow (also grey). click that @exlover
0

It may be better to verify whether the entry made, by the user, is a text or numeric value before processing is done.

From a cursory glance at the code - I can surmise that the "text value" may go along the lines of '123 (see apostrophe mark).It's possible, but that would be a conscientious effort by the end user.

So,therefore IMHO, it would be better to run a validation check before the calculation (as suggested by Scott Holtzman).

"Not Applicable" - either use the number zero (0) in the code or somehow get your VBA to map "Not Applicable" and change this entry to zero(0) value.

Pseudo code example:

IF frmQA.ComboBox3.Value = "NOT Applicable" then

        frmQA.ComboBox3.Value = 0
END IF

The above suggestion can be placed in a worksheet event, perhaps

Best of luck

Regards

RDFS

Comments

0

Here is the answer modified as suggested by Scott

Dim totalscore As Double
totalscore = WorksheetFunction.Sum(IIf(IsNumeric(frmQA.ComboBox3.Value), frmQA.ComboBox3.Value, 0), IIf(IsNumeric(frmQA.ComboBox4.Value), frmQA.ComboBox4.Value, 0), IIf(IsNumeric(frmQA.ComboBox5.Value), frmQA.ComboBox5.Value, 0), IIf(IsNumeric(frmQA.ComboBox6.Value), frmQA.ComboBox6.Value, 0), IIf(IsNumeric(frmQA.ComboBox7.Value), frmQA.ComboBox7.Value, 0), IIf(IsNumeric(frmQA.ComboBox8.Value), frmQA.ComboBox8.Value, 0), IIf(IsNumeric(frmQA.ComboBox9.Value), frmQA.ComboBox9.Value, 0))

Comments

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.